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

2021/11/6

How to animate Charakter

Marcel.0311 Marcel.0311

2021/11/6

#
Hey , i just started with greenfoot and made a Space Infinity Runner with a Spaceship that is only Moving in the X-axis but not on the Y . Now i wanted to ask how i could animate it with 5 photos when the game starts , even when its not moving .
danpost danpost

2021/11/6

#
Marcel.0311 wrote...
Hey , i just started with greenfoot and made a Space Infinity Runner with a Spaceship that is only Moving in the X-axis but not on the Y . Now i wanted to ask how i could animate it with 5 photos when the game starts , even when its not moving .
Use an array to hold the 5 images (in order). Add a counter field to control which image is to show and when. Let the counter field increment each act. After some number of acts (which will be the same for each image), change to the next image. This can be done using modulo arithmetic. For example, if each image is to show for 6 frames, then:
// fields
GreenfootImage[] images = new GreenfootImage[5]; //array for images (loaded in the constructor of the class or here)
int imageCounter;

// act
public void act()
{
    imageCounter = (imageCounter+1)%(5*6); // 5 images, 6 frames per image
    if (imageCounter%6 == 0) // time to change image
    {
        setImage(images[imageCounter/6]); // set new image
    }
    // other actions (moving)
}
Marcel.0311 Marcel.0311

2021/11/6

#
Ok , i somewhat understood your code but where to i have to tell what images i want to use?
danpost danpost

2021/11/7

#
Marcel.0311 wrote...
Ok , i somewhat understood your code but where to i have to tell what images i want to use?
See comment at end of line 2.
You need to login to post a reply.