A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: gotoAndPlay(random frame label) -how?

  1. #1
    Junior Member
    Join Date
    Nov 2006
    Posts
    3

    gotoAndPlay(random frame label) -how?

    I´ve been trying to have my movie to go to a random frame label (from 2 or 3 for each letter)when a specified key is pressed. This doesn´t work, what is wrong? How to make it work? help a sleepless beginner


    var keyListener:Object = new Object();
    keyListener.onKeyDown = function() {
    if (Key.isDown(65)) {
    var Alabels:Array = new Array("a1", "a2", "a3");
    var randomA:Number = random(Alabels.length);
    gotoAndPlay(Alabels[randomA]);
    }
    Last edited by ActionMom; 11-09-2006 at 12:38 PM.

  2. #2
    Senior Member
    Join Date
    Nov 2004
    Posts
    928
    var Alabels:Array = new Array("a1", "a2", "a3");
    var keyListener:Object = new Object();
    keyListener.onKeyDown = function() {
    if (Key.isDown(65)) {

    var randomA:Number = random(Alabels.length);
    gotoAndPlay(Alabels[randomA]);

    }
    }
    Key.addListener(keyListener)

  3. #3
    Junior Member
    Join Date
    Nov 2006
    Posts
    3
    Thanks gorlummeeguvnor, my problem is solved already. I got a little help from other forum too

    I did have that key.addListener there in the end (just forgot to mention it here) , but my movie started to work when I added _root. before gotoAnd Play.

    (I dot fully understand why _root. is needed because my function is on the main timeline but)
    this works fine:

    var keyListener:Object = new Object();
    keyListener.onKeyDown = function() {
    if (Key.isDown(65)) {
    var Alabels:Array = new Array("a1", "a2", "a3");
    var randomA:Number = random(Alabels.length);
    _root.gotoAndPlay(Alabels[randomA]);
    }
    }
    Key.addListener(keyListener)

  4. #4
    Senior Member
    Join Date
    Nov 2004
    Posts
    928
    a pleasure.
    2 points of interest
    1- i think that if you haven't put in _root then flash points the gotoAndPlay to the timeline that it is on - so if its on the _root timeline then it becomes _root.gotoAndPlay()
    2- var Alabels:Array = new Array("a1", "a2", "a3"); -placing this inside the onKeyDown function creates the array every time the function is called - obviously this works but you are pointlesly creating code and a new array each time.

  5. #5
    Junior Member
    Join Date
    Nov 2006
    Posts
    3
    Thanks, right you are, it really is pointless to create those arrays again and again That _root. -thing i still don´t quite understand cause i´m pointing form the root to the root.. but this mom is just a beginner and don´t need to know everything -yet

  6. #6
    The Ancient
    Join Date
    Mar 2000
    Location
    Michigan, USA
    Posts
    213
    gotoAndPlay references the current object that contains the call. In this case, it references the keyListener object. Adding _root tells the system to apply it to the timeline, rather than the keyListener object.
    jahasloth

  7. #7
    Senior Member
    Join Date
    Nov 2004
    Posts
    928
    nice one jahasloth

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center