22 Replies Latest reply: Sep 8, 2012 7:46 AM by kglad RSS

    Use Timer Event or KeyBoard Event in Same Frame

    saratogacoach Community Member

      Hi,

       

      I'm trying to create a proof-of-concept/working model for an eLearning exercise, which I can then build on. 16 words are presented randomly (no repeats) on separate frames  for 1000ms each. The user can either react by pressing/touching a button or not pressing/touching the button. If they do not press/touch the button the frame should advance after 1000ms to the next randomly chosen frame/word (Timer Event). If they decide to press/touch the button (Mouse Event) before the automatic (Timer Event) 1000ms advance, then it should immediately advance to the next random frame/word and not wait for the remainder of the 1000ms.

       

      I can create a function on the first frame to advance randomly, use either a Timer Event listener or Mouse Event listener. But I can't figure out how to use both. Plus while the Timer Event seems to work OK on the first frame, iterating through the frames, the Mouse Event would always occur, if selected by the user, on  frames other than the first frame.

       

      (An alternative would be to use a single text box on a single frame, randomly changing the words, from the array of 16 words. But this would present a similar problem of changing after 1000ms or if the button is pressed/touched, changing immediately.)

       

      So far, with modest AS3 skills, I haven't been able to get my brain around using both of these events and on which frames.

       

      Any help would be much appreciated.

       

      Regards,

        • 1. Re: Use Timer Event or KeyBoard Event in Same Frame
          kglad MVP

          stage.addEventListener(KeyboardEvent.KEY_DOWN,keydownF);

          var t:Timer=new Timer(1000,1);

          t.addEventListener(TimerEvent.TIMER,timerF);

           

          function keydownF(e:KeyboardEvent):void{

          t.reset();

          t.start();

          // do whatever -  go to next random frame

          }

          function timerF(e:TimerEvent):void{

          t.reset();

          t.start();

          // do whatever - go to next random frame

          }

          • 2. Re: Use Timer Event or KeyBoard Event in Same Frame
            saratogacoach Community Member

            Thanks, kglad, for this.

             

            Having difficulty figuring out how to get it working. My code is a mess. Lots of errors on debugging.

             

            Maybe someone can take a look at the 5-frame FLA posted here: http://elearningprojects.com/P1new.Fla

             

            How to get it working. (It's in CS6: please let me know if it needs to be saved in CS5, CS5.5, re-uploaded, to be opened).

             

            Regards,

            • 3. Re: Use Timer Event or KeyBoard Event in Same Frame
              kglad MVP

              on the right side of this page can you see, "How to fix error#1009-5008"?

               

              if yes, click that and start fixing those errors.  if not, please attach a screenshot of the right side of this page.

              • 4. Re: Use Timer Event or KeyBoard Event in Same Frame
                saratogacoach Community Member

                I'm afraid, even with my trying to correct, re-work the script, I have a host of errors. Too many to copy here but here is a sample:

                 

                Scene 1, Layer 'Layer 1', Frame 5, Line 131021: Duplicate function definition.

                 

                Scene 1, Layer 'Layer 1', Frame 5, Line 3

                1151: A conflict exists with definition tm in namespace internal.

                 

                I've tried correcting these, for example by renaming 2 variables (t) to t and tm. But, no joy. Error list (14) remains.

                 

                What I've tried to do is set up the functions and event listeners in all 5 frames. The keyboard press listener and function needs to be available in every frame (after frame 1-start frame).

                 

                User enters frame 1, presses start button>goes to next random frame (no repeats). There, user either presses button (or spacebar) in which case advances immediately to next random frame, or waits for time out of 1000ms after which advances to next random frame. This is repeated until all 16 frames (each with a stimulus word) are completed. (Scoring these plus user feedback is another feature to be added later.)

                 

                So, I've been adding the listeners (timer, keypress) and their functions on each frame. Same array of random frame numbers must be used in every function to avoid repeats. Getting conflicts. Trying it on every frame leads to 14 errors.

                 

                Not sure how else to design or implement this.

                • 5. Re: Use Timer Event or KeyBoard Event in Same Frame
                  kglad MVP

                  on the right side of this page can you see, "How to fix error#1009-5008"?

                  • 6. Re: Use Timer Event or KeyBoard Event in Same Frame
                    saratogacoach Community Member

                    screenshot1.jpg

                    No, I can't see "How to fix error#1009-5008" on my browser page.

                    • 7. Re: Use Timer Event or KeyBoard Event in Same Frame
                      kglad MVP

                      thank you for the screenshot.

                       

                      can you take one showing the right side of the page (just like the previous one) but showing the top of the page, too.

                       

                      in the mean time here's the document that shows you how to fix those errors:  http://forums.adobe.com/docs/DOC-2571

                      • 9. Re: Use Timer Event or KeyBoard Event in Same Frame
                        Ned Murphy MVP

                        The Discussions section doesn't have the link you are talking about.  It is found in the Overview section... http://forums.adobe.com/community/flash/flash_actionscript3?view=overview

                        • 10. Re: Use Timer Event or KeyBoard Event in Same Frame
                          saratogacoach Community Member

                          Hi,

                           

                          OK gave up on trying to use multiple frames. Instead am trying a dynamic text box (wordT) into which a word from an array can be randomly inserted.

                           

                          stop();

                          //_startBtn.addEventListener(MouseEvent.CLICK, randomWord);

                           

                          function randomWord(){

                                  var words:Array = new Array("1", "2", "3", "4", "5", "6", "7");

                                  wordT.text = words[randomNumber(words.length-1)];

                          }

                           

                          function randomNumber(max){

                                  return(Math.round(Math.random()*max));

                          }

                           

                          stage.addEventListener(KeyboardEvent.KEY_DOWN,keydownF);

                          //var t:Timer=new Timer(1000,1);

                          //t.addEventListener(TimerEvent.TIMER,timerF);

                           

                          function keydownF(e:KeyboardEvent):void{

                          if (e.keyCode == Keyboard.SPACE){

                          randomWord()

                          }

                          //t.reset();

                          //t.start();

                          // do whatever -  go to next random frame

                          }

                          //function timerF(e:TimerEvent):void{

                          //t.reset();

                          //t.start();

                          // do whatever - go to next random frame

                          //}

                           

                          (1) As can be seen from the commented out script, have not yet been able to get the timer to advance the words. Just the space bar.

                           

                          (2) And, while the "words" are random, there are repeats.

                           

                          (If I can get this working, then the next challenge (3) will be to capture and score if a specific word times out (1000ms) or is or is not selected by pressing the spacebar. Ugh! )

                           

                          Any help appreciated.

                          • 11. Re: Use Timer Event or KeyBoard Event in Same Frame
                            kglad MVP

                            hi ned,

                             

                            i see the link in the overview and the all content views but not the discussions view.  i always use the discussions view and i was starting to check which view other forum users use.

                            • 12. Re: Use Timer Event or KeyBoard Event in Same Frame
                              kglad MVP

                              @sc, use:

                               

                              stop();

                              //_startBtn.addEventListener(MouseEvent.CLICK, randomWord);

                              var words:Array=new Array("1","2","3","4","5","6","7");

                               

                              stage.addEventListener(KeyboardEvent.KEY_DOWN,keydownF);

                              var t:Timer=new Timer(1000,1);

                              t.addEventListener(TimerEvent.TIMER,timerF);

                               

                              randomWord();

                               

                              function randomWord() {

                                  wordT.text=words[randomNumber(words.length)];

                                  t.reset();

                                  t.start();

                              }

                               

                              function randomNumber(max) {

                                  return Math.floor(Math.random()*max);

                              }

                               

                               

                              function keydownF(e:KeyboardEvent):void {

                                  if (e.keyCode==Keyboard.SPACE) {

                                      randomWord();

                                  }

                               

                              }

                              function timerF(e:TimerEvent):void {

                                  randomWord();

                              }

                              • 13. Re: Use Timer Event or KeyBoard Event in Same Frame
                                saratogacoach Community Member

                                Yes, that works very well. Thank you.

                                 

                                Now the next trick for me will be to see if I can get a couple of things to happen when the words either auto-advance (timer) or advance by spacebar press. Should be fun.

                                 

                                The exercise is a Go No-Go Association Task (GNAT). In this exercise, if a specific word appears and the user presses, a +1 score is recorded to a score variable, and before the word changes (100ms), a green O symbol is displayed (100ms). If the user presses and it was not the correct word, a red X is displayed (100ms) for 100ms before the next random word appears. For user no-press conditions, auto-advance would display a red X (100ms) if the user should have pressed. For user no-press conditions, auto-advance would display a green O (100ms) if the user was correct in not pressing, and a +1 is recorded and added to the score variable. So, correct scores are added, incorrect choices are not subtracted. Correct and incorrect words can be "hard-coded" in the script. (Whew!, getting weary just writing this, let alone figuring out the script.)

                                 

                                Anyway, something like that, given a tweak here and there.

                                 

                                So, I will need to "get busy." Will probably have more questions.

                                 

                                Much thanks kglad and Ned for getting me started.

                                 

                                Kind Regards,

                                • 15. Re: Use Timer Event or KeyBoard Event in Same Frame
                                  saratogacoach Community Member

                                  I have one more question pertaining to this script:

                                   

                                  how do you stop the timer and keyboard handler from exceeding the number of words (words.length)?

                                   

                                  I've tried putting for loops (like: for(var i=0; i<8; i++) {etc.) to stop the timer and keypress once it has completed displaying all 7 words (hopefully with no repeats).

                                   

                                  But nothing I've tried has worked.

                                  • 16. Re: Use Timer Event or KeyBoard Event in Same Frame
                                    kglad MVP

                                    i'm not sure that makes sense unless you ensure you display each word exactly once.  is that what you want?   ie, display each word exactly once (in random order)?

                                    • 17. Re: Use Timer Event or KeyBoard Event in Same Frame
                                      saratogacoach Community Member

                                      Yes, each word in this and a second practice "block" of word stimuli are presented once each. 16 words per block (a little hitch here: 16 drawn from a list of 18. Ugh!) In this exercise, I am replicating a tool for which results are published. Replication needs to be precise, since the statistical processing of the scores also need to be pricise in order to produce valid results. So, 2 practice blocks, no repeats.

                                       

                                      Then 2 test blocks of 70 words each block. In contrast to the practice blocks with no repeats, words in the 2 test blocks have repeats as follows: words in the  test block are randomly drawn from a list of 3 groups of 3 words (total words=9), and randmly repeated totaling 20 from group 1, 20 from group 2 and 30 from group 3. So, here, in contrast to the practice blocks, a different formula/script for the test blocks.

                                       

                                      Scoring is actually easier, since this is done by math formulas that can be done capably in AS3.

                                       

                                      So, to answer again: yes, each word in these practice blocks is displayed only once (random array of 18 of which 16 are chosen for display, other 2 not displayed. Maybe can take the first 16?).

                                       

                                      Whew!

                                      • 18. Re: Use Timer Event or KeyBoard Event in Same Frame
                                        kglad MVP

                                        use:

                                         

                                         

                                        stop();

                                        //_startBtn.addEventListener(MouseEvent.CLICK, randomWord);

                                        var words:Array=new Array("1","2","3","4","5","6","7");

                                        shuffle(words);

                                        var index:int = 0;

                                        var numToDisplay:int = 5;

                                         

                                        stage.addEventListener(KeyboardEvent.KEY_DOWN,keydownF);

                                        var t:Timer=new Timer(1000,1);

                                        t.addEventListener(TimerEvent.TIMER,timerF);

                                         

                                        randomWord();

                                         

                                        function randomWord() {


                                        if(index<numWordsToDisplay){

                                            wordT.text=words[index++];

                                            t.reset();

                                            t.start();

                                        } else {

                                        // display score

                                        }

                                        }

                                         

                                         

                                        function keydownF(e:KeyboardEvent):void {

                                            if (e.keyCode==Keyboard.SPACE) {

                                                randomWord();

                                            }

                                         

                                        }

                                        function timerF(e:TimerEvent):void {

                                            randomWord();

                                        }

                                         

                                        function shuffle(a:Array) {

                                            var i:int;

                                            var j:int;

                                            var e:*;

                                            var len:int = a.length;

                                            for (i = len-1; i>=0; i--) {

                                                j=Math.floor((i+1)*Math.random());

                                                e = a[i];

                                                a[i] = a[j];

                                                a[j] = e;

                                            }

                                        }

                                        • 19. Re: Use Timer Event or KeyBoard Event in Same Frame
                                          saratogacoach Community Member

                                          Again, much thanks! Works nicely (changed numWordsToDisplay to numToDisplay).

                                           

                                          I'll play with it some more. See if I can add scoring (if a specific target word is diplaying, and user presses spacebar, +1, stored to a target word variable, and if a specific distractor word is displayed and the user presses the spacebar, +1 is stored to a separate distractor variable).

                                           

                                          Scoring for a positive "hit" would be something like:

                                           

                                          while (wordT.text = "3")

                                           

                                          {

                                           

                                          checker();

                                           

                                          }

                                           

                                          wordT.addEventListener(KeyboardEvent.KEY_DOWN,checker);

                                          function checker(event:KeyboardEvent){

                                           

                                            

                                             if(e.keyCode==Keyboard.SPACE{

                                           

                                                 var targetHit:int++

                                             }

                                          }

                                           

                                          Scoring for a distractor "hit" would be something like:

                                           

                                          while (wordT.text = "5")

                                           

                                          {

                                           

                                          checker2();

                                           

                                          }

                                           

                                          wordT.addEventListener(KeyboardEvent.KEY_DOWN,checker2); //hopefully no conflict with checker

                                          function checker2(event:KeyboardEvent){

                                           

                                            

                                             if(e.keyCode==Keyboard.SPACE{

                                           

                                                 var distractorHit:int++

                                             }

                                          }

                                           

                                           

                                          But I think this is a great foundation for the practice blocks of 16 words, and also for a script for the 2 trial blocks of 70 words each.

                                           

                                          Interesting challenge.

                                           

                                          Again, much thanks.

                                          • 20. Re: Use Timer Event or KeyBoard Event in Same Frame
                                            kglad MVP

                                            mark that answer as correct then.

                                             

                                            this thread is pretty far removed from the original issue so, start a new thread if you have a different problem. 

                                            • 21. Re: Use Timer Event or KeyBoard Event in Same Frame
                                              saratogacoach Community Member

                                              Yes, again thanks.

                                               

                                              (Marked as correct.)