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

2011/12/30

Flying, want it to stop

marzukr marzukr

2011/12/30

#
I have a problem, I am making a game when you press the space bar the owl wearing a suit jumps (agent X for short) but if you hold down the spacebar agent x flys, I don't want him to fly because then the game would be to easy and I would rather have it be to hard then to easy, because easy=boring hard=fun+addicting. Here is my code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class player extends Actor
{
    public int vspeed = 0;
    public int ac = 1;
    /**
     * Act - do whatever the player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        motion();
        fall();
        onGround();
        checkfall();
    }  
    
    private void motion()
    {
        if (Greenfoot.isKeyDown("right")) {
            move(3);
        }
        if (Greenfoot.isKeyDown("left")) {
            move(-3);
        }
        if (Greenfoot.isKeyDown("space")) 
        {
            jump();
        }
    }
    public void fall()
    {
         setLocation (getX(), getY() + vspeed);
         vspeed = vspeed + ac;
    }
    public boolean onGround()
    {
        Actor under = getOneObjectAtOffset ( 0, 19, grass.class);
        return under != null;
    }
    public void checkfall()
    {
        if(onGround())
        {
            vspeed = 0;
        }
        
    } 
    public void jump()
    {
        vspeed = -4;
    }
} 
Can someone please help?
Builderboy2005 Builderboy2005

2011/12/30

#
Try only setting the vspeed to -4 if your character is currently on the ground. This will make it more of a jumping motion rather than a flying motion.
marzukr marzukr

2011/12/30

#
Thanks for trying to help but then keeps bouncing on the platform when touching one and it still flys when you hold down the space bar.
kiarocks kiarocks

2011/12/30

#
Try
public void jump()
{
    if(!onGround())
    {
         vspeed = -4;
    }
}
marzukr marzukr

2011/12/30

#
That stopped Agent X from flying but now he can't jump when he is touching a platform.
kiarocks kiarocks

2011/12/30

#
sorry, try
if(onGround())
instead.
marzukr marzukr

2011/12/30

#
Awesome, thanks that really helped!
kiarocks kiarocks

2011/12/30

#
That is what BuilderBoy was trying to say.
Builderboy2005 Builderboy2005

2011/12/30

#
Ah, I see I should have been more precise! Kiarocks specified what I meant though :) Glad you got it working!
marzukr marzukr

2011/12/30

#
Oh, I get it now, Thanks anyways!
You need to login to post a reply.