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

2014/12/4

How to make the enemy's jump height change randomly

cloud41910 cloud41910

2014/12/4

#
I am trying to make my enemy jump with a random height so there are times that it's jump can be too high to low here's my code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Heartless here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
    /**
     * Act - do whatever the Heartless wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
public class LastHeartless extends Actor
{
    HeartlessBlast heartlessblast = new HeartlessBlast();
    private int shotDelay = 0;
    private int JumpChance = 0;
    private int vspeed = 0;
    private int acceleration = 1;
    boolean CanJump;
    public void act() 
    {
        fall();
        fap();
        shoot();
        randomjump();
    }
    public void annihilate()
    {
        Actor K = getOneObjectAtOffset(0,0,K.class);
     getWorld().addObject(heartlessblast, getX(), getY());
    
    if(K != null){
       World world = getWorld();
        NewGameOver newgameover = new NewGameOver();
        world.addObject(newgameover, world.getWidth()/2,world.getHeight()/2);
        world.removeObject(K);
        
    }
    
    }
      public void fap()
    {
         getWorld().addObject(heartlessblast, getX(), getY());  
    }
    
    public void shoot()
    {
        if(shotDelay >= 100)
        {
            getWorld().addObject(new HeartlessBlast(), getX(), getY());
            shotDelay = 0;
        }
        shotDelay++;
    }
   public boolean isOnGround()
    {
        Clone Window = (Clone) getWorld();
        if(getY() + (getImage().getHeight() / 2) >= (Window.getHeight() -1)) {
             vspeed = 0; return true;
        }
        return false;
    }
   public void randomjump()
   {
       if( isOnGround() && Greenfoot.getRandomNumber(80) ==3)
       {
           jump();
        }
    }
    public void jump()  
   {  
     vspeed += 8+Greenfoot.getRandomNumber(8); 
     fall();  
   }  
   public void fall()  
   {  
     setLocation ( getX(), getY() + vspeed);  
     vspeed = vspeed + acceleration;  
   }  
}
cloud41910 cloud41910

2014/12/4

#
It only falls down
danpost danpost

2014/12/4

#
In line 73, in your 'jump' method, you are adding a downward pull to your actor by adding to the vertical speed. I admit -- that was partly my fault as I did lead you to do that yesterday (however, I did add, when giving it, 'or something like that' -- I must have been in a hurry and did not have time to totally think everything through).
cloud41910 cloud41910

2014/12/4

#
It's working now i made a higher value for the jump method
You need to login to post a reply.