Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / animating a sprite

Author
Message
jjeff
13
Years of Service
User Offline
Joined: 11th Mar 2011
Location:
Posted: 17th Mar 2011 20:36
Hi, i am trying to make a 2d game and i am animating a character when the user moves him. i was able to make the character animate through 2 or 4 frames repeatedly while a key was pressed. however, i also wanted to stop the animation without finishing the frames when the user stops pushing the key. i added else statements but now the character only animates when i hit the W key.

WLGfx
17
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 18th Mar 2011 05:40
Your 'else' statements keep resetting the sprite frame. You would have to code it to play animations when the direction changes and to stop the animation when the player stops. Depending on the direction the stopped frame will activate.

Warning! May contain Nuts!
jjeff
13
Years of Service
User Offline
Joined: 11th Mar 2011
Location:
Posted: 19th Mar 2011 01:46
I added the var "direction" to keep track of which key was pressed last and used that var in my logical arguments, but it still isn't working.

Quote: "
stop the animation when the player stops
"


the only way i know how to check if the player stops is by checking if a key is not pressed, but i need to keep track of the last key that was pressed so that i know which frame to set it to.

jjeff
13
Years of Service
User Offline
Joined: 11th Mar 2011
Location:
Posted: 19th Mar 2011 03:39


okay, i think i have got the dbSetSpriteFrame to set the appropriate direction when the user is not hitting a key because when i tap each key, the character faces the correct direction. However, i am still having some problems. if i hit the S key, the character starts animating like he is moving down, but if i were to hit the A key right after hitting the S key, the character starts animating like he is moving up and then turns left.
WLGfx
17
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 20th Mar 2011 00:47
By the looks of the code that's what it's meant to do. You've got the stop values set right. The only other check that might need to be put into place is to whether you're on the ground. That way you could set the sprite frame as left or right facing. (maybe the last left or right facing depending which direction was faced before going up or down.)

I've not used sprites for a long time so I'm assuming it may need to finish the last animation before starting another. There should be a command to stop the animation on direction change.

Warning! May contain Nuts!
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 21st Mar 2011 22:39
When the direction changes, you need to use dbSetSpriteFrame, to set the beginning frame of the new animation. Otherwise the sprite will cycle through all images in between the current frame and the beginning point of the new animation. dbPlaySprite in itself is not enough to set the starting point.

The best thing to do would be to introduce a "direction" variable to tell if you are walking up, down, left or right (or standing still). Also store the previous direction. Depending on keypresses, determine the new direction and compare with the previous one. In the moment when it changes, use dbSetSpriteFrame to set the beginning frame of the animation.
jjeff
13
Years of Service
User Offline
Joined: 11th Mar 2011
Location:
Posted: 25th Mar 2011 22:12
Thanks for the help! I finally got it to work exactly how i wanted it to thanks to all the great advice.

working code:

Login to post a reply

Server time is: 2024-11-19 12:43:58
Your offset time is: 2024-11-19 12:43:58