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

2018/12/17

Animations

Legitimacy Legitimacy

2018/12/17

#
Hi, I'm trying to code my animation for my game but how can i make it constantly animate while other things happen? I tried to use this code:
import greenfoot.*;
public class Shark extends Actor
{
    public void act() 
    {
        sharkMovement();
        animation();
    }    

    private void animation()
    {
            setImage("shark1.png");
            Greenfoot.delay(3);
            setImage("shark2.png");
            Greenfoot.delay(3);
            setImage("shark3.png");
            Greenfoot.delay(3);
            setImage("shark4.png");
            Greenfoot.delay(3);
            setImage("shark3.png");
            Greenfoot.delay(3);
            setImage("shark2.png");
            Greenfoot.delay(3);
    }
    
    private void sharkMovement()
    {
        if (Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+3,getY());
            if (Greenfoot.isKeyDown("up"))
            {
                setRotation(45);
            }
            else if (Greenfoot.isKeyDown("down"))
            {
                setRotation(135);
            }
            else
            {
                setRotation(90);
            }
        }
        if (Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-3,getY());
            if (Greenfoot.isKeyDown("up"))
            {
                setRotation(315);
            }
            else if (Greenfoot.isKeyDown("down"))
            {
                setRotation(225);
            }
            else
            {
                setRotation(270);
            }
        }
        if (Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(),getY()-2);
            if (Greenfoot.isKeyDown("left"))
            {
                setRotation(315);
            }
            else if (Greenfoot.isKeyDown("right"))
            {
                setRotation(45);
            }
            else
            {
                setRotation(0);
            }
        }
        if (Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(),getY()+4);
            if (Greenfoot.isKeyDown("left"))
            {
                setRotation(225);
            }
            else if (Greenfoot.isKeyDown("right"))
            {
                setRotation(135);
            }
            else
            {
                setRotation(180);
            }
        }
    }
}
the issue with this code is that everything moves slowly (entire game moves slowly, animation speed is perfect) because of the "Greenfoot.delay(3);". How can I animate it without having this issue?
danpost danpost

2018/12/17

#
Legitimacy wrote...
I'm trying to code my animation for my game but how can i make it constantly animate while other things happen? I tried to use this code: << Code Omitted >> the issue with this code is that everything moves slowly (entire game moves slowly, animation speed is perfect) because of the "Greenfoot.delay(3);". How can I animate it without having this issue?
The delay method is used to "skip" act cycles; so, no action takes place during its delay. Use an int counter field to count act cycles and advance the image every 3 cycles.
Nahquin Nahquin

2018/12/18

#
    int pic=2;
        int mp=10;
    public void animation(){
        mp--;
        if (mp==0){
                if(pic==1){
                    setImage("shark1.png");
                    pic=2;
                    mp=10;
                }
                else if(pic==2){
                    setImage("shark2.png");
                    pic=3;
                    mp=10;
                }
                else if(pic==3){
                    setImage("shark3.png");
                    pic=1;
                    mp=10;
                }
            
        }
    }
Try this
danpost danpost

2018/12/18

#
Nahquin wrote...
<< Code Omitted >> Try this
Best would be to put the image set, or their numbers, in an array and use a factor of the act counter to get the index of the current image:
private int imgCounter;
private static final int[] imgNums = { 1, 2, 3, 4, 3, 2 };

public void animation()
{
    imgCounter = (imgCounter+1)%(3*imgNums.length); // run counter
    if (imgCounter%3 == 0) setImage("shark"+imgNums[imgCounter/3]+".png");
}
You need to login to post a reply.