Skip navigation
Currently Being Moderated

Use Timer Event or KeyBoard Event in Same Frame

Sep 6, 2012 5:22 PM

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,

 
Replies
  • kglad
    72,212 posts
    Jul 21, 2002
    Currently Being Moderated
    Sep 6, 2012 6:45 PM   in reply to saratogacoach

    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

    }

     
    |
    Mark as:
  • kglad
    72,212 posts
    Jul 21, 2002
    Currently Being Moderated
    Sep 7, 2012 7:42 AM   in reply to saratogacoach

    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.

     
    |
    Mark as:
  • kglad
    72,212 posts
    Jul 21, 2002
    Currently Being Moderated
    Sep 7, 2012 8:41 AM   in reply to saratogacoach

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

     
    |
    Mark as:
  • kglad
    72,212 posts
    Jul 21, 2002
    Currently Being Moderated
    Sep 7, 2012 9:21 AM   in reply to saratogacoach

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 7, 2012 10:25 AM   in reply to kglad

    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=overv iew

    
     
    |
    Mark as:
  • kglad
    72,212 posts
    Jul 21, 2002
    Currently Being Moderated
    Sep 7, 2012 11:11 AM   in reply to Ned Murphy

    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.

     
    |
    Mark as:
  • kglad
    72,212 posts
    Jul 21, 2002
    Currently Being Moderated
    Sep 7, 2012 11:17 AM   in reply to saratogacoach

    @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();

    }

     
    |
    Mark as:
  • kglad
    72,212 posts
    Jul 21, 2002
    Currently Being Moderated
    Sep 7, 2012 12:36 PM   in reply to saratogacoach

    you're welcome.

     
    |
    Mark as:
  • kglad
    72,212 posts
    Jul 21, 2002
    Currently Being Moderated
    Sep 7, 2012 7:28 PM   in reply to saratogacoach

    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)?

     
    |
    Mark as:
  • kglad
    72,212 posts
    Jul 21, 2002
    Currently Being Moderated
    Sep 7, 2012 10:54 PM   in reply to kglad

    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;

        }

    }

     
    |
    Mark as:
  • kglad
    72,212 posts
    Jul 21, 2002
    Currently Being Moderated
    Sep 8, 2012 7:15 AM   in reply to saratogacoach

    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. 

     
    |
    Mark as:
  • kglad
    72,212 posts
    Jul 21, 2002
    Currently Being Moderated
    Sep 8, 2012 7:46 AM   in reply to saratogacoach

    you're welcome.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points