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

2016/8/24

Falling issues

AuroraGamer AuroraGamer

2016/8/24

#
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,
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;
        }
    }
            
}


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?
Super_Hippo Super_Hippo

2016/8/24

#
After line 21, add this:
while (isTouching(Grass2.class)) setLocation(getX(), getY()-1);
Instead of using the 'delay' method, create an int variable and use it as a counter. Count up every act-cycle when walking and every 10 act cycles, you change the image (change the 10 to whatever fits).
AuroraGamer AuroraGamer

2016/8/24

#
Thanks, the Wolf now is on the ground, that was a big problem for me. And about the animation, I understand what you mean, I'm just not sure how exactly to do that.
Super_Hippo Super_Hippo

2016/8/25

#
This is probably not perfect, but an example how to do it for the left side. The problem you will be facing is that if you for example walk in one direction and change the direction, the image may change a few act-cycles too late. To avoid that, you could use a field to track the current direction and if the direction is changed, you set the 't_animation' to 9, so the first image is used instantly.
//at the start
private int t_animation = 0;

//line 79 ff
if (Greenfoot.isKeyDown("left"))
{
    move(-2); //check how high this number should be
    t_animation++;
    if (t_animation % 10 == 0)
    {
        setImage("WalkL"+t_animation/10+".png");
        if (t_animation == 40) t_animation = 0;
    }
}
You need to login to post a reply.