This site requires JavaScript, please enable it in your browser!
Greenfoot back
LobsterL
LobsterL wrote ...

2018/2/17

Array for animating a character

LobsterL LobsterL

2018/2/17

#
I'd like to create an array for animating an image for my character when he moves. There are a different set of images for moving right, left, up and down. and i would like for the array to be put in the if statement for my character movement.
LobsterL LobsterL

2018/2/17

#
The right images = 8 , the left image = 7, the up images = 8, the down images = 8
LobsterL LobsterL

2018/2/17

#
like there are 8 images for the right key down etc
Vercility Vercility

2018/2/17

#
And your question is..? Create the arrays and use if cases for the respective keys which set the image accordingly
LobsterL LobsterL

2018/2/17

#
Yes, that's my question i would just like an example so i can implement it into my game
Vercility Vercility

2018/2/18

#
LobsterL wrote...
Yes, that's my question i would just like an example so i can implement it into my game
GreenfootImage[] right = new GreenfootImage[8];
GreenfootImage[0] = new GreenfootImage("ImagePath");
....
...
int right = 0;
int i = 0;

public void animation () {
if(Greenfoot.iskeyDown("right"){
    if(right < 20){
       right ++;
return;
}
    right = 0;
 if(i >7) {
 i = 0;
}
actor.setImage(right[i]);
}
}
Specifics might be different, i dont use Greenfoot personally (aka IDK how the GreenfootImage class is called) Actual Values should be adjusted according to how long you want each image to display
You need to login to post a reply.