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

2017/2/2

Running animation?

N00balert N00balert

2017/2/2

#
Hi! Painfully new to Greenfoot and coding in general so would seriously appreciate any help :) I’m currently creating a side-scrolling game and I’d like for the main actor to cycle through 8 image frames whilst the player is pressing down the right arrow key so that it looks like he’s running. I literally don’t even know where to start and right now the actor is just a static image. My teacher suggested creating an array but I have no idea how to go about doing that. Any suggestions?
Super_Hippo Super_Hippo

2017/2/2

#
The array could look like this:
1
2
3
4
5
6
7
8
9
10
11
private GreenfootImage[] imgMoveRight =
{
    new GreenfootImage("right0.png"),
    new GreenfootImage("right1.png"),
    new GreenfootImage("right2.png"),
    new GreenfootImage("right3.png"),
    new GreenfootImage("right4.png"),
    new GreenfootImage("right5.png"),
    new GreenfootImage("right6.png"),
    new GreenfootImage("right7.png")
};
Then you can have an int field. Whenever the animation should run (when the right key is pressed), increase this field and set the image:
1
private int animationCounter = 0;
1
2
animationCounter = (animationCounter+1) % 8;
setImage(imgMoveRight[animationCounter]);
If you want to slow it down, you could do something like this:
1
2
animationCounter = (animationCounter+1) % 80;
if (animationCounter % 10 == 0) setImage(imgMoveRight[animationCounter/10]);
You need to login to post a reply.