A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: What's wrong with this code?

  1. #1
    Member
    Join Date
    Jul 2009
    Posts
    69

    What's wrong with this code?

    I used this code on one project and it's working just fine. Then I'm using it on another project (very slightly modified) and it's giving me syntax errors:


    stop();

    import flash.events.KeyboardEvent;
    import flash.display.MovieClip;

    stage.addEventListener(KeyboardEvent.KEY_UP, MoveOn);

    function MoveOn(event:KeyboardEvent):void
    {
    gotoAndPlay("horse"); //also tried NextFrame(); here, but didn't work either
    }

    //also tried it with and without code MoveOn(); at the end

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    What is the exact error you are getting?

  3. #3
    Member
    Join Date
    Jul 2009
    Posts
    69
    It was giving me syntax errors on a bunch of the lines. However, I just opened up a whole new file and transferred everything and it worked. I'm thinking I was working in AS 2.0 by accident. Duh.

    If I may ... here's what's not working now (I have one movie clip on each frame that each have this code on the last keyframe):
    stop();
    MovieClip(parent).canPlay = true;:


    and then this code on the second frame of the maintimeline (the first frame has the code I posted before) - and, yet, it moves me on to the next frame if I press a key before the movieclip has ended. Any ideas why? Should it be if(canPlay = true)

    stop();

    import flash.events.KeyboardEvent;
    import flash.display.MovieClip;

    stage.addEventListener(KeyboardEvent.KEY_UP, keyHandler);
    var canPlay:Boolean = false;

    function keyHandler(e:KeyboardEvent):void
    {
    if(canPlay)
    {
    nextFrame();
    canPlay = false;
    }
    }

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    If I read you right, you have at least 3 frames with the following code in them:
    Code:
    stop();
    
    import flash.events.KeyboardEvent;
    import flash.display.MovieClip;
    
    stage.addEventListener(KeyboardEvent.KEY_UP, MoveOn);
    
    function MoveOn(event:KeyboardEvent):void{
      gotoAndPlay("horse"); 
    }
    Code:
    stop();
    
    import flash.events.KeyboardEvent;
    import flash.display.MovieClip;
    
    stage.addEventListener(KeyboardEvent.KEY_UP, keyHandler);
    var canPlay:Boolean = false;
    
    function keyHandler(e:KeyboardEvent):void{
      if(canPlay){
        nextFrame();
        canPlay = false;
      }
    }
    Code:
    stop();
    MovieClip(parent).canPlay = true;:
    The event handler you added to the stage in the first frame was never removed, so it's still active and sends you to "horse" when it gets a key_up.

  5. #5
    Member
    Join Date
    Jul 2009
    Posts
    69
    Ahh, event listener.

    Well, I changed it to this on frame 1:

    stop();

    import flash.events.KeyboardEvent;
    import flash.display.MovieClip;

    stage.addEventListener(KeyboardEvent.KEY_UP, MoveOn);

    function MoveOn(event:KeyboardEvent):void
    {
    stage.removeEventListener(KeyboardEvent.KEY_UP, MoveOn);
    nextFrame();
    }
    and this on frame 2:

    stop();

    import flash.events.KeyboardEvent;
    import flash.display.MovieClip;

    stage.addEventListener(KeyboardEvent.KEY_UP, keyHandler);
    var donePlay:Boolean = false;

    function keyHandler(event:KeyboardEvent):void
    {
    if(donePlay);
    {
    stage.removeEventListener(KeyboardEvent.KEY_UP, MoveOn);
    nextFrame();
    donePlay = false;
    }
    }
    and it still lets me go from Frame 2 to Frame 3 even when the movieclip is not done playing (meaning mc gets cut off). Seems like donePlay = true; is not happening for some reason.
    Last edited by Leftyplayer; 08-13-2009 at 01:34 PM.

  6. #6
    Member
    Join Date
    Jul 2009
    Posts
    69
    Fixed! (and it's embarrassing how simple the problem was)

    Code needed to be: if(donePlay == true) (without the semicolon

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