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:
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?
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);
}
}
}
}
