Skip to main content
gamedev.net
🔒 Locked

[java] J2ME Animation Sequence

Started by pookster Apr 23, 2007 at 4:10 PM 1 replies 1.7k views
Original Post
pookster
pookster
Hi, I am new here so sorry if this is a re-post ( I had a go with the search but couldn't find anything) I am currently making a simple top down RPG game with scrolling tile background and such ... I would like to use Sprite.setFrameSequence / Sprite.nextFrame() to have my main character animate as he walks. At the moment my key handling is done using a switch statement which runs on each loop as follows ... private void checkUserInput() { // get the state of keys int keyState = getKeyStates(); // calculate the direction pollKeys(keyState); } private void pollKeys(int keyState) { switch(keyState) { case RIGHT_PRESSED: directionChanged(); player.setFrameSequence(rightSequence); player.moveright = true; player.movingFlag = true; break; ... } } However, this is causing the animation to jump straight back to index 0 on every loop. What is the normal way of implementing the setFrameSequence function ? I'm guessing the switch statement isn't the best way of polling the keys and is part of the problem but I'm stumped as to how to prevent the sequence being reset on each loop :( Any help / advise would be greatly appreciated ! Thanks in advance Pookster
trojanman
trojanman
Quote:
Original post by pookster
private void checkUserInput()
{
// get the state of keys
int keyState = getKeyStates();

// calculate the direction
pollKeys(keyState);
}

private void pollKeys(int keyState)
{
switch(keyState)
{
case RIGHT_PRESSED:
directionChanged();
player.setFrameSequence(rightSequence);
player.moveright = true;
player.movingFlag = true;
break;
...
}
}


However, this is causing the animation to jump straight back to index 0 on every loop.

What is the normal way of implementing the setFrameSequence function ?

Do you ever actually call Sprite.nextFrame()?

Generally you would set the sequence at the beginning of the Sprite moving and then on every update/paint loop you would call nextFrame() if animation is required.

Quote:
Original post by pookster
I'm guessing the switch statement isn't the best way of polling the keys and is part of the problem but I'm stumped as to how to prevent the sequence being reset on each loop :(

You can choose to do user input a variety of ways. One way is to do exactly as you are and poll the user states, another is to use keyPressed() and keyReleased() events to get the user input.
pookster
pookster
Thanks for the reply...

I call the nextFrame() function in the update method of the main loop (forgot to say that).

I've just realised I was being an idiot though !

I put this line in ...

if(!player.moveright) player.setFrameSequence(rightSequence);

... and it works fine now! That's stopped it from resetting the loop back to index 0.

Thanks again.

Pookster

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.