10 Replies Latest reply: May 28, 2012 9:37 AM by esdebon RSS

    Remember my Number

    Sailem12

      Hey guys,

      Maybe simple question but I dont find any solution right now...

       

      I have a number counting up from 0 to 86400.

      When I'm pressing a key, I want to get the CURRENT number to be saved into a new variable. This new variable has to be static (not counting).

      But the Basic counting have to go on.

       

      Hope you know what I mean and can help me =)

        • 1. Re: Remember my Number
          Ned Murphy MVP

          To get a key to trigger something you need to make use of a KeyboardEvent listener

           

          stage.addEventListener(KeyboardEvent.KEY_UP, assignValue);

           

          function checkText(event:KeyboardEvent):void {
               yourVar = currentCount;

          }

          • 2. Re: Remember my Number
            Sailem12 Community Member

            getting the key pressing work is not the problem, my problem is: How to get this Number.

            The Number is counting up, and anytime I press the key i get the value in a new variable

            • 3. Re: Remember my Number
              Ned Murphy MVP

              Maybe you should show your code then, because if you do not know how to assign a new variable but can deal with the keyboard end of things, there is a disconnect with what your ability appears to be and what you say it is.

              • 4. Re: Remember my Number
                Sailem12 Community Member

                Ok here is my code:

                 

                import flash.display.Sprite;

                import flash.events.TimerEvent;

                import flash.utils.Timer;

                import com.greensock.*;

                import com.greensock.easing.*;

                 

                 

                ////////////////////////DAYTIME IN SECONDS

                 

                var today:Date;

                var current:Date;

                var time:Date;

                 

                 

                var millisecond:int;

                var second:int;

                var minute:int;

                var hour:int;

                var day:int;

                var month:int;

                 

                 

                var secs:int;

                 

                 

                var timer:Timer = new Timer(500);

                          timer.addEventListener(TimerEvent.TIMER, timerHandler);

                          timer.start();

                 

                          function timerHandler(event:TimerEvent):void

                          {

                                    time = new Date();

                                    millisecond = time.getMilliseconds();

                                    second = time.getSeconds();

                                    minute = time.getMinutes();

                                    hour = time.getHours();

                                    day = time.getDate();

                                    month = time.getMonth();

                 

                                    today = new Date(2012,5,28,0,0,0,0);

                                    current = new Date(2012,month+1,day,hour,minute,second,millisecond);

                 

                 

                                    secs = (current.getTime() - today.getTime())/1000;

                                    trace(secs); //"secs" is the current daytime in seconds

                 

                 

                          stage.addEventListener(KeyboardEvent.KEY_DOWN, HerdOn);

                 

                 

                ////////////////////////EVENT

                 

                 

                          function HerdOn(event:KeyboardEvent):void

                          {

                                              var xPos:Number = -411.10;

                                              var yPos:Number = -8.05;

                                              var pixWidth:int = 0; 

                 

                                              if (event.charCode == 49) // key "1"

                                              {

                                                        trace ("start");

                 

                                                        //So, when I have pressed the key "1", here I want to have a new Variable which gives me a static number

                                                        //of "secs"

                 

                 

                                                        currentCount

                 

                                                        trace ("One is pressed");

                 

                                                        bob_mc.x = xPos;

                                                        bob_mc.y = yPos;

                                                        bob_mc.width = pixWidth;

                 

                                                        TweenLite.to(bob_mc, 15.25, {x:xPos, y:yPos, scaleX:2, ease:Linear.easeNone});

                 

                                              }

                 

                                              if (event.charCode == 50) //key "2"

                 

                                              {

                                                        trace("stop");

                 

                 

                                                        TweenLite.to(bob_mc, 15.25, {paused:true, ease:Linear.easeNone});

                 

                 

                 

                                              }

                                    }

                          }

                • 5. Re: Remember my Number
                  Ned Murphy MVP

                  I answered your question yeaterday regarding how to get the seconds (you should mark that posting as answered).  You should clean up that code so that it is not doing things that you don't need done - which appears to be most of the date functions you run. 

                   

                  Also, you should never nest function within other functions.  So you should separate the HerdOn function from being inside the timeHandler function.

                   

                  If you want another variable, then outside any function declare a new variable, and then assign the secs value to it inside your keyboard handler function.

                  • 6. Re: Remember my Number
                    Sailem12 Community Member

                    Ok I understand, maybe this is my big problem, so

                    How do I call a function or a variable in another function, even the variable is local. I thought it only works with global variables.

                    For me it's a bit cunfusing, why I put function into function.

                    Now, when I seperate the function, where I calculate the daytime in seconds, I have to call it again when I press my key, right?
                    How? (sorry for my inexperience xD)

                    • 7. Re: Remember my Number
                      esdebon Community Member

                      I think you could use a Global Array.

                       

                      var laps:Array=new Array();  //out of all functions

                       

                       

                      laps.push(value);    // inside your "HerdOn"

                       

                      Each time you press a correct key is that mark would add to the array, you would have ordered marks and delete would use:

                       

                      laps=new Array();

                      • 8. Re: Remember my Number
                        Ned Murphy MVP

                        Declare your variable outside any functions so that it is available outside of any function if you need it to be, juist like the rest of the vars you declared.  If you need to store different values at different times and want to retain them all, use an array to store them in.

                        • 9. Re: Remember my Number
                          Sailem12 Community Member

                          Thanks for your help^^ Works fine =)

                          • 10. Re: Remember my Number
                            esdebon Community Member

                            you're welcome