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

2013/6/25

Need some help

joeschmoe joeschmoe

2013/6/25

#
So in my game when ever you have the right or left arrow key down its stopping the program and I dont know why. the code for the actors that move is:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Mover extends Actor
{
    private static final int acceleration = 2;      // down (gravity)
    private static final int speed = 7;             // running speed (sideways)
    
    private int vSpeed = 0;                         // current vertical speed
    
    /**
     * Move a bit to the right.
     */
    public void moveRight()
    {
        setLocation ( getX() + speed, getY() );
    }
    
    /**
     * Move a bit to the left.
     */
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY() );
    }
    
    /**
     * Return true if we're on firm ground that we can stand on.
     */
    public boolean onGround()
    {
        Object under = getOneObjectAtOffset(0, getImage().getHeight()/2-8, null);
        return under != null;
    }

    /**
     * Set a speed for a vertical movement. Positive values go down, negative values
     * go up.
     */
    public void setVSpeed(int speed)
    {
        vSpeed = speed;
    }
    
    /**
     * Apply gravity and fall downwards until we hit ground or the bottom of the screen.
     */
    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
        if ( atBottom() )
            gameEnd();
    }
    
    /**
     * Return true if we're at the bottom of the screen.
     */
    private boolean atBottom()
    {
        return getY() >= getWorld().getHeight() - 2;
    }
    
    /**
     * End this game (that is: stop the simuation).
     */
    private void gameEnd()
    {
        Greenfoot.stop();
    }


}
And the code for this specific actor is:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * the evolution animal needs to go from one side to the other
 */
public class Animal1 extends Mover
{
    private static final int jumpStrength = 16;
    private int level;
    private int jumped = 0;   // count how often we've jumped
    
    public Animal1()
    {
        level = 1;
    }
    
    public void act() 
    {
        checkKeys();        
        checkFall();
        checkNextLevel();
    }
    
    private void checkKeys()
    {
        if (Greenfoot.isKeyDown("left") )
        {
            setImage("pengu-left.png");
            moveLeft();
        }
        if (Greenfoot.isKeyDown("right") )
        {
            setImage("pengu-right.png");
            moveRight();
        }
        if (Greenfoot.isKeyDown("space") )
        {
            if (onGround())
                jump();
        }
    }    
    
    private void jump()
    {
        setVSpeed(-jumpStrength);
        fall();
        jumped++;
    }
    
    private void checkFall()
    {
        if (onGround()) {
            setVSpeed(0);
        }
        else {
            fall();
        }
    }

    /**
     * Check whether we should go to the next level, and if yes, start the next level.
     */
    private void checkNextLevel()
    {
        if (getX() == getWorld().getWidth()-1) {
            if (level == 1) {
                level = 2;
                getWorld().removeObject(this);

            }
            else {
                level = 1;
                getWorld().removeObject(this);

            }
        }
    }


}
Thanks
danpost danpost

2013/6/25

#
I do not see where having keys down with the code provided would cause the program to stop. Maybe you are using 'getKey' or 'isKeyDown' elsewhere in one of your other classes?
joeschmoe joeschmoe

2013/6/25

#
nope... which is why I am so confused
joeschmoe joeschmoe

2013/6/25

#
i changed the size of the world though, from the default to (950, 700, 1) if it matters
danpost danpost

2013/6/25

#
The only reason I see (in the code provided) for the scenario to stop is if the Animal1 object is near the bottom of the screen.
danpost danpost

2013/6/25

#
Comment out line 51 of the Mover class and see if it does the same thing or not.
joeschmoe joeschmoe

2013/6/25

#
i took line 50 and 51 out of the mover class and same deal. space bar works, arrows make it freeze...
Zamoht Zamoht

2013/6/25

#
Does the scenario stop or does it crash?
danpost danpost

2013/6/25

#
I used your code in a new scenario. commented out line 67 of the Mover class and, because I did not have the images, lines 29 and 34 of the Animal1 class, then ran the scenario with a new Animal1 object in the world and the arrow keys worked fine.
joeschmoe joeschmoe

2013/6/25

#
woops, looks like it was actually crashing... it seems I never add one of the animal pictures to the folder and it wasnt being found, causing a crash. Thanks for the help!
You need to login to post a reply.