So I'm trying to make a game that most of this isn't in place yet, but when it's finished the player will be a wolf, trying to get through multiple levels while trying to keep a health meter up by eating small animals. The problem that I'm having the most trouble with is this; I have a ground class, but the wolf simply falls through it to the bottom of the screen, so how would I fix this?
Here's the code for the wolf,
Also, I'm having some trouble with animating the walking, as you can see I have 4 images, but with Greenfoot.delay() that delays the entire program, and obviously I don't want that, so how would I slow down the images enough for that to work?
import greenfoot.*;
/**
* Write a description of class Wolf here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Wolf3 extends Health
{
private int gravity;
private boolean onGround;
/**
* Act - do whatever the Wolf wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
onGround = true;
gravity--;
setLocation(getX(), getY() - gravity);
checkForJump();
moveAndTurn();
next();
}
public void moveAndTurn()
{
//run left
if (Greenfoot.isKeyDown(",") && Greenfoot.isKeyDown("space"))
{
move(-32);
setImage("Wolf3.png");
Greenfoot.delay(20);
move(-32);
setImage("Wolf4.png");
Greenfoot.delay(20);
move(-32);
}
//else
//{
// setImage("WolfSitting.png");
//}
// walk right
if (Greenfoot.isKeyDown("right"))
{
move(14);
setImage("Walk1.png");
Greenfoot.delay(20);
move(14);
setImage("Walk2.png");
Greenfoot.delay(20);
move(14);
setImage("Walk3.png");
Greenfoot.delay(20);
move(14);
setImage("Walk4.png");
Greenfoot.delay(20);
move(14);
}
//run right
if (Greenfoot.isKeyDown(".") && Greenfoot.isKeyDown("space"))
{
move(32);
setImage("Wolf1.png");
Greenfoot.delay(20);
move(32);
setImage("Wolf2.png");
Greenfoot.delay(20);
move(32);
}
// walk left
if (Greenfoot.isKeyDown("left"))
{
move(-14);
setImage("WalkL1.png");
Greenfoot.delay(20);
move(-14);
setImage("WalkL2.png");
Greenfoot.delay(20);
move(-14);
setImage("WalkL3.png");
Greenfoot.delay(20);
move(-14);
setImage("WalkL4.png");
Greenfoot.delay(20);
move(-14);
}
if (Greenfoot.isKeyDown("up"))
{
GreenfootSound howl = new GreenfootSound("wolf2.wav");
setImage("WolfHowl.png");
howl.play();
if (howl.isPlaying() == true)
{
Greenfoot.delay(500);
setImage("WolfSitting.png");
}
}
}
public void next()
{
Actor grass;
grass = getOneObjectAtOffset(0, 0, Grass3.class);
if (grass !=null)
{
getWorld().removeObject(this);
Greenfoot.setWorld(new Four());
}
}
public void howl()
{
Actor grass1;
grass1 = getOneObjectAtOffset(0, 0, Grass4.class);
if (grass1 !=null)
{
GreenfootSound wolf = new GreenfootSound("wolf2.wav");
setImage("WolfHowl.png");
wolf.play();
if (wolf.isPlaying() == true)
{
Greenfoot.delay(500);
setImage("WolfSitting.png");
}
}
}
//public int getHeight()
//{
// return getImage().getHeight();
//}
public void ground()
{
Actor land;
land = getOneObjectAtOffset(0, 50, Grass2.class);
if (land !=null)
{
onGround = true;
}
}
public Wolf3()
{
}
private void checkForJump()
{
if (onGround && Greenfoot.isKeyDown("space"))
{
gravity = 10;
onGround = false;
}
}
}

