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

2012/12/14

Delaying An Actor

marzukr marzukr

2012/12/14

#
I have a project i'm doing and I need to delay and actor because it is a platform game and when the actor jumps if you hold the space bar the actor flys, I need to delay the actor after it jumps so it doesn't fly. How do I do this? Here is my code.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Odesseus here.
 * 
 * @author (marzukr) 
 * @version (12-13-12)
 */
public class Odesseus extends Actor
{
    private int speed = 4;
    private int vSpeed = 0;
    private int exel = 2;
    private int jp = -5;
    /**
     * Act - do whatever the Odesseus wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        keycheck();
        checkFall();
    }
    
    private void keycheck()
    {
        if(Greenfoot.isKeyDown("right"))
        {
            moveRight();
        }
        
        if(Greenfoot.isKeyDown("left"))
        {
            moveLeft();
        }
        
        if(Greenfoot.isKeyDown("space"))
        {
            jump();
        }
    }
    
    public void jump()
    {
        vSpeed = jp;
        fall();
    }
    
    public void moveRight()
    {
        setLocation ( getX() + speed, getY() );
    }
    
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY() );
    }
    
    public void fall()
    {
        setLocation (getX(), getY() + vSpeed);
        vSpeed = vSpeed + exel;
    }
    
    public boolean onGround()
    {
        Actor under = getOneObjectAtOffset (0, 40, ship.class);
        return under != null;
    }
    
    public boolean fiftyfour()
    {
        Actor under = getOneObjectAtOffset (0 , 40, bush.class);
        return under != null;
    }
    
    public boolean hi()
    {
        Actor under = getOneObjectAtOffset (0, 40, stone.class);
        return under != null;
    }
    
    public boolean yo()
    {
        Actor under = getOneObjectAtOffset (0, 40, water54.class);
        return under != null;
    }
    
    public void checkFall()
    {
        if(onGround())
        {
            vSpeed = 0;
        }
        else
        {
            if(fiftyfour())
            {
                vSpeed = 0;
            }
            else
            {
                if(hi())
                {
                    vSpeed = 0;
                }
                else
                {
                    if(yo())
                    {
                        vSpeed = 0;
                    }
                    else
                    {
                        fall();
                    }
                }
            }
        }
    }
}
This is the part I need the delay.
 public void jump()
    {
        vSpeed = jp;
        fall();
    }
    
danpost danpost

2012/12/14

#
You are just missing the condiion 'if(onGround())'. I do not know if it will make any difference; but you might want exel to be able to go into jp evenly.
marzukr marzukr

2012/12/14

#
Thanks danpost, this fixed the flying problem, (although I would have to do an if statement for fiftyfour, hi, and yo) but now it jumps to frequently and is too sensitive.
marzukr marzukr

2012/12/14

#
Never mind I fixed this by decreasing jp to -16. Thanks.
You need to login to post a reply.