A Flash Developer Resource Site

Results 1 to 20 of 20

Thread: [F8] Walking problem

  1. #1
    Member
    Join Date
    Jun 2004
    Posts
    46

    [F8] Walking problem

    When I walk left and right it works fine, but when I walk up and down it doesn't play the up and down walking animations... it just shows the first frame of the up and down animations.

    here is the code... is there anything wrong?

    Code:
    onClipEvent (enterFrame) {
    	if (Key.isDown(Key.LEFT)) {
    		
    		this.gotoAndStop("left");
    		
    	}
    	if (Key.isDown(Key.RIGHT)) {
    		
    		this.gotoAndStop("right");
    		
    	} else if (not (Key.isDown(Key.LEFT))) {
    	
    		this.gotoAndStop(1);
    	}
    		if (Key.isDown(Key.UP)) {
    		
    		this.gotoAndStop("walkup");
    		
    	}
    	if (Key.isDown(Key.DOWN)) {
    		
    		this.gotoAndStop("walkdown");
    		
    	}
    }

  2. #2
    Member
    Join Date
    Jun 2004
    Posts
    46
    still haven't figured it out :/

  3. #3
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967
    First, why are you using gotoAndStop instead of gotoAndPlay?

    Second, realize that code in the enter frame is happening EVERY FRAME. Your frames per second in your project is set by you, but it is probably between 15 and 30 times per second.

    This code is running 15 to 30 times per seconds. What this means if when you hold down the right key you are spamming your movie to move to the frame labeled "right" 15 to 30 times per second.

    You would be better off using gotoAndPlay to direct the movie clip to a walking animation when the key is frist pressed, and then not run that code anymore until the key is released. You could set variables like this.moveright=true that is set when the right key is pressed. Then put that code inside an if statement so it only checks it if it is false. If(this.moveright==false){code checks if the right key is pressed to make it true}.

    Just like the code you put in checks when a key is pressed, there is also methods that check when a key is released (not at flash, don't know the name) you set your variables that are tracking when the character is animating to false when the key in question is released (so that the code that checks for pressing that key starts to be checked again).

    Then you need to decide which animation to play when the user is moving both up and right, what you want to happen when the user holds left and right at the same time, etc...

    It takes time to get used to the enterframe handler and the speed at which all that code is constantly run.

  4. #4
    Member
    Join Date
    Jun 2004
    Posts
    46
    There is only one frame.


    And on each of those frames where it gotoAndStops there is only one frame, which has a moveiclip, which has the walking animation. Like I said the Left and Right movement works... I don't know why the others dont.

  5. #5
    Member
    Join Date
    Jun 2004
    Posts
    46

  6. #6
    5+5=55 Schfifty Five's Avatar
    Join Date
    Jun 2006
    Posts
    698
    Why do you use:

    else if (not (Key.isDown(Key.LEFT)))?

    By the way, I like the sprites/backgrounds, where'd you get em?

  7. #7
    Member
    Join Date
    Jun 2004
    Posts
    46
    Quote Originally Posted by Schfifty Five
    Why do you use:

    else if (not (Key.isDown(Key.LEFT)))?

    By the way, I like the sprites/backgrounds, where'd you get em?
    They are from a game called Ultima Online... www.uo.com


    search google for UO Character Viewer(for characters) or/and InsideUO(for objects)


    The background is a screenshot I took while playing the game

  8. #8
    Member
    Join Date
    Jun 2004
    Posts
    46
    Quote Originally Posted by Schfifty Five
    Why do you use:

    else if (not (Key.isDown(Key.LEFT)))?
    I really don't know... but if I don't have it when I release any of the arrow keys, it keeps playing the animation and won't stop. With it, when I release any of the arrow keys, it goes back to frame one, which I want.




    I just tried taking it out, and it would play the Up and Down animations, but not any of the diaganol ones. And when I release any of the arrow keys, ti keeps playing the animation.

  9. #9
    5+5=55 Schfifty Five's Avatar
    Join Date
    Jun 2006
    Posts
    698
    Here I rewrote it for you:

    Code:
    onClipEvent (enterFrame) {
    	LEFT = 0;
    	RIGHT = 1;
    	DOWN = 2;
    	UP = 3;
    	keys = [false, false, false, false];
    	if (Key.isDown(Key.LEFT)) {
    		keys[LEFT] = true;
    	}
    	if (Key.isDown(Key.RIGHT)) {
    		keys[RIGHT] = true;
    	}
    	if (Key.isDown(Key.DOWN)) {
    		keys[DOWN] = true;
    	}
    	if (Key.isDown(Key.UP)) {
    		keys[UP] = true;
    	}
    	if (keys[LEFT] && !keys[RIGHT] && !keys[DOWN] && !keys[UP]) {
    		this.gotoAndStop("left");
    	}
    	if (!keys[LEFT] && keys[RIGHT] && !keys[DOWN] && !keys[UP]) {
    		this.gotoAndStop("right");
    	}
    	if (!keys[LEFT] && !keys[RIGHT] && keys[DOWN] && !keys[UP]) {
    		this.gotoAndStop("walkdown");
    	}
    	if (!keys[LEFT] && !keys[RIGHT] && !keys[DOWN] && keys[UP]) {
    		this.gotoAndStop("walkup");
    	}
    	if (keys[LEFT] && !keys[RIGHT] && keys[DOWN] && !keys[UP]) {
    		this.gotoAndStop("downleft");
    	}
    	if (!keys[LEFT] && keys[RIGHT] && keys[DOWN] && !keys[UP]) {
    		this.gotoAndStop("downright");
    	}
    	if (keys[LEFT] && !keys[RIGHT] && !keys[DOWN] && keys[UP]) {
    		this.gotoAndStop("upleft");
    	}
    	if (!keys[LEFT] && keys[RIGHT] && !keys[DOWN] && keys[UP]) {
    		this.gotoAndStop("upright");
    	}
    	if (!keys[LEFT] && !keys[RIGHT] && !keys[DOWN] && !keys[UP]) {
    		this.gotoAndStop(1);
    	}
    }
    onClipEvent (enterFrame) {
    	if (this.hitTest(_root.wallblocker)) {
    		_x = _x-10;
    	}
    	if (this.hitTest(_root.wallblocker)) {
    		_y = _y-10;
    	}
    }

  10. #10
    5+5=55 Schfifty Five's Avatar
    Join Date
    Jun 2006
    Posts
    698
    By the way, where's the code that actually makes him move? I couldn't find it lol...

    And what's this for:

    onClipEvent (enterFrame) {
    if (this.hitTest(_root.wallblocker)) {
    _x = _x-10;
    }
    if (this.hitTest(_root.wallblocker)) {
    _y = _y-10;
    }
    }

  11. #11
    Member
    Join Date
    Jun 2004
    Posts
    46
    Quote Originally Posted by Schfifty Five
    By the way, where's the code that actually makes him move? I couldn't find it lol...

    And what's this for:

    onClipEvent (enterFrame) {
    if (this.hitTest(_root.wallblocker)) {
    _x = _x-10;
    }
    if (this.hitTest(_root.wallblocker)) {
    _y = _y-10;
    }
    }

    Its on the background, the background moves.


    That code is nothing, my plan didn't work... I tried making it so the guy couldn't run into that building but I couldn't figure it out so I just made the boundaries smaller so he can't reach the building.

  12. #12
    Member
    Join Date
    Jun 2004
    Posts
    46
    Thank you guys so much for your help.

  13. #13
    5+5=55 Schfifty Five's Avatar
    Join Date
    Jun 2006
    Posts
    698
    No problem

    Did my code work ok?

  14. #14
    Member
    Join Date
    Jun 2004
    Posts
    46
    Quote Originally Posted by Schfifty Five
    No problem

    Did my code work ok?

    works great

  15. #15
    Member
    Join Date
    Jun 2004
    Posts
    46
    Could you explain this to me though?

    LEFT = 0;
    RIGHT = 1;
    DOWN = 2;
    UP = 3;
    keys = [false, false, false, false];

  16. #16
    5+5=55 Schfifty Five's Avatar
    Join Date
    Jun 2006
    Posts
    698
    Quote Originally Posted by vilord
    Could you explain this to me though?

    LEFT = 0;
    RIGHT = 1;
    DOWN = 2;
    UP = 3;
    keys = [false, false, false, false];

    Those are just used for reference so I could remember which spot in the array each key was stored at.

    When I used:

    if (Key.isDown(Key.LEFT)) {
    keys[LEFT] = true;
    }
    if (Key.isDown(Key.RIGHT)) {
    keys[RIGHT] = true;
    }
    if (Key.isDown(Key.DOWN)) {
    keys[DOWN] = true;
    }
    if (Key.isDown(Key.UP)) {
    keys[UP] = true;
    }

    It was a lot easier to remember than:

    if (Key.isDown(Key.LEFT)) {
    keys[0] = true;
    }
    if (Key.isDown(Key.RIGHT)) {
    keys[1] = true;
    }
    if (Key.isDown(Key.DOWN)) {
    keys[2] = true;
    }
    if (Key.isDown(Key.UP)) {
    keys[3] = true;
    }

  17. #17
    Member
    Join Date
    Jun 2004
    Posts
    46
    How does it know how to go diagonally if you don't have something like...

    if (Key.isDown(Key.DOWN)) && (Key.isDown(Key.RIGHT))) {
    keys[DOWN] = true;
    keys[RIGHT] = true;

  18. #18
    5+5=55 Schfifty Five's Avatar
    Join Date
    Jun 2006
    Posts
    698
    Ok let's say you were pressed left and down to walk left-down.

    When it gets here:
    if (Key.isDown(Key.LEFT)) {
    keys[LEFT] = true;
    }
    if (Key.isDown(Key.RIGHT)) {
    keys[RIGHT] = true;
    }
    if (Key.isDown(Key.DOWN)) {
    keys[DOWN] = true;
    }
    if (Key.isDown(Key.UP)) {
    keys[UP] = true;
    }

    it will make keys = [true,false,true,false], so that it knows that the left key (keys[0]) has been pressed and the down key (keys[2]) has been pressed.

    Then when it checks to see if it will go diagnal:

    if (keys[LEFT] && !keys[RIGHT] && keys[DOWN] && !keys[UP]) {
    this.gotoAndStop("downleft");
    }

    keys[LEFT] or keys[0] is true, and so is keys[DOWN], or keys[2].

    In pseudo code it's like:

    if (Leftkey is true and Rightkey is false and Downkey is true and Upkey is false) {
    Go to the down-left frame.
    }

  19. #19
    Member
    Join Date
    Jun 2004
    Posts
    46
    Alright, awesome, I see now... at first I didn't know what the "!". You helped me so much, thanks.

  20. #20
    5+5=55 Schfifty Five's Avatar
    Join Date
    Jun 2006
    Posts
    698
    Quote Originally Posted by vilord
    Alright, awesome, I see now... at first I didn't know what the "!". You helped me so much, thanks.
    Yeah ! is just shorter for not.

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