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

2013/10/29

Enemy jump timer

Baenefin Baenefin

2013/10/29

#
Hello again all, I am trying to make my enemy jump at a random interval. He also falls. But I can't figure out how to make him stop jumping. Do I need to make a counter? This is what I have.
public class Enemy extends Actor
{
    int timer=0;
    int dir=0;
    private static final int jumpStrength = 18;
    public static final int Speed = 5;
    private static final int acceleration = 1;
    public int vSpeed = 0;

public void jump()
    {
        timer++;

        setVSpeed(-jumpStrength);
        fall(); 

    }

    public void setVSpeed(int speed)
    {
        vSpeed = speed;
    }
    
    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }

    private void checkFall()
    {
        if (onGround()) {
            setVSpeed(0);          
        }
        else {
            fall();
        }
    }

    public boolean onGround()
    {
        Block a = (Block) getOneObjectAtOffset(0,32,Block.class);
        return a !=null;
    }
public class Jumper extends Enemy
{
    GreenfootImage right = new GreenfootImage("JumperRight.png");
    GreenfootImage left = new GreenfootImage("JumperLeft.png");
    public void act() 
    {
        if (Greenfoot.getRandomNumber(100) > 90)
        jump();
        fall();

        if (dir==0)
        { setImage(right); }
        if (dir==180)
        { setImage(left); }
    }    
}
Do I need to make an intervall counter or something like that?
Baenefin Baenefin

2013/10/29

#
Why would you even say that?
You need to login to post a reply.