My actor can move left and right and also jump but I was wondering if anyone knew a way to allow the actor to move while it's in the air when it jumps, or to let the actor jump while it's moving left or right?
public void act()
{
moveVertically(); // rising, falling and detecting jump key
moveHorizontally(); // detecting left and right movement keys
}public class Avatar extends Actor
{
private int ySpeed;
private int apexTimer;
private SaturnGame world;
public void act()
{
int groundLevel = getWorld().getHeight() - getImage().getHeight()/1;
boolean onGround = (getY() == groundLevel);
if (!onGround)
{
if (ySpeed == 0 && apexTimer > 0) apexTimer--;
if (ySpeed == 0 && apexTimer > 0) return;
ySpeed++;
setLocation(getX(), getY()+ySpeed);
if (getY()>=groundLevel)
{
setLocation(getX(), groundLevel);
Greenfoot.getKey();
}
}
else
{
if (Greenfoot.isKeyDown("up"))
{
ySpeed = -15;
setLocation(getX(), getY()+ySpeed);
apexTimer = 30;
}
if(Greenfoot.isKeyDown("right"))
{
move (3);
setImage("resizeimage.net-output.png");
}
if(Greenfoot.isKeyDown("left"))
{
move (-3);
setImage("avatarright.png");
}
if (getOneIntersectingObject(Monster2.class) != null)
{
getWorld().removeObject(this);
}
}
}
}public void act()
{
// vertical movement
int groundLevel = getWorld().getHeight()-getImage().getHeight();
ySpeed++;
setLocation(getX(), getY()+ySpeed);
if (getY() >= groundLevel)
{
setLocation(getX(), groundLevel);
ySpeed = 0;
if (Greenfoot.isKeyDown("up"))
{
ySpeed = -15;
}
}
// horizontal movement
if (Greenfoot.isKeyDown("right"))
{
move (3);
setImage("resizeimage.net-output.png");
}
if (Greenfoot.isKeyDown("left"))
{
move (-3);
setImage("avatarright.png");
}
// death
if (getOneIntersectingObject(Monster2.class) != null)
{
getWorld().removeObject(this);
}
}