A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: (CS3)Animated mc with keydown

  1. #1
    Member
    Join Date
    Dec 2004
    Posts
    68

    (CS3)Animated mc with keydown

    Hopefully this is a small problem.

    I am working on a simple scrolling game. When the right key is pressed, the background scrolls and the character MC advances to the second frame of the man MC which contains an animation of the character walking. When the key is released, the background stops scrolling and the character returns to frame one of the man MC which he is just standing.

    Code:
    stop();
    function man_walks(e:KeyboardEvent):void {
    	if (e.keyCode==Keyboard.RIGHT) {
    		man_mc.gotoAndStop(2);
    	}
    }
    stage.addEventListener(KeyboardEvent.KEY_DOWN, man_walks);
    
    function stop_man(e:KeyboardEvent):void {
    	man_mc.gotoAndStop(1);
    }
    stage.addEventListener(KeyboardEvent.KEY_UP, stop_man);
    The problem I am having is that the walking animation does not play completely or loop. It plays for a few frames then stops.

    I think the problem is that the script (gototAndStop(2); ) repeats as long as the key is pressed, meaning it continues to return and stop on frame 2 causing the walk cycle to pause.

    Is this what is causing the walk cycle not to animate? How do I get around this?

    I’ve attached a small example showing the problem.
    Attached Files Attached Files

  2. #2
    Senior Member
    Join Date
    Mar 2008
    Posts
    301
    PHP Code:

    stop
    ();
    function 
    man_walks(e:KeyboardEvent):void {
        if (
    e.keyCode==Keyboard.RIGHT) {
            if (
    man_mc.currentFrame != 2) {
                
    man_mc.gotoAndStop(2);
            }
        }
    }
    stage.addEventListener(KeyboardEvent.KEY_DOWNman_walks);

    function 
    stop_man(e:KeyboardEvent):void {
        
    man_mc.gotoAndStop(1);
    }
    stage.addEventListener(KeyboardEvent.KEY_UPstop_man); 
    KeyboardEvent.KEY_DOWN gets called over and over again whenever a key is held down. So you keep sending the movieclip to frame 2 every few hundred milliseconds. So add an if statement to check whether the movieclip is already at frame 2 before sending it to frame 2. If it's already there, don't do anything to it.

  3. #3
    Member
    Join Date
    Dec 2004
    Posts
    68
    Thank You! It works great!

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