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

2018/12/5

How can a Enemy shoot randomly?

pleasetroll pleasetroll

2018/12/5

#
I want that Bowser shoots to the left side randomly, but how do I make this? His coordinates are (x = 700, y = 200). PLEASE HELP ME!
danpost danpost

2018/12/5

#
pleasetroll wrote...
I want that Bowser shoots to the left side randomly, but how do I make this? His coordinates are (x = 700, y = 200). PLEASE HELP ME!
Add a timer field to the Bowser class. Choose two values that you want to limit the delay between shots -- one for the minimum delay and one for the maximum delay. The difference between these two values is the range. Set the timer to the sum of the minimum value and a random value in the range. Decrement the timer each act. When it becomes zero, shoot and reset the timer as before.
pleasetroll pleasetroll

2018/12/6

#
danpost wrote...
pleasetroll wrote...
I want that Bowser shoots to the left side randomly, but how do I make this? His coordinates are (x = 700, y = 200). PLEASE HELP ME!
Add a timer field to the Bowser class. Choose two values that you want to limit the delay between shots -- one for the minimum delay and one for the maximum delay. The difference between these two values is the range. Set the timer to the sum of the minimum value and a random value in the range. Decrement the timer each act. When it becomes zero, shoot and reset the timer as before.
i got this:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bowser here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bowser extends Actor
{   private GifImage g = new GifImage("Bowserreadytofightleft.gif");
    private GifImage f = new GifImage("Bowserstarving.gif");
    private int minShotDelay = 40;
    private int maxShotDelay = 160;
    private int shotTimer = minShotDelay;
    /**
     * Act - do whatever the Bowser wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        standard();
        hitted();
        shootRandomly();
        if (shotTimer == 0)
        {
            getWorld().addObject(new Fireballs(), getX(), getY());
            shotTimer = minShotDelay+Greenfoot.getRandomNumber(1+maxShotDelay-minShotDelay);
        }
        else shotTimer--;
        
    }
    public void standard()
    {if(!isTouching(Fireball.class))
        {
        this.setImage(g.getCurrentImage());
    }
    }
    public void hitted()
    {if(isTouching(Fireball.class))
        {
        this.setImage(f.getCurrentImage());
        HealthBar.health--;
    }
    
    }
    
     public void shootRandomly()
    {
     Fireballs fireballs = new Fireballs();
    getWorld().addObject( fireballs,  getX(), getY());
    }  
    
    


}
pleasetroll pleasetroll

2018/12/6

#
pleasetroll wrote...
danpost wrote...
pleasetroll wrote...
I want that Bowser shoots to the left side randomly, but how do I make this? His coordinates are (x = 700, y = 200). PLEASE HELP ME!
Add a timer field to the Bowser class. Choose two values that you want to limit the delay between shots -- one for the minimum delay and one for the maximum delay. The difference between these two values is the range. Set the timer to the sum of the minimum value and a random value in the range. Decrement the timer each act. When it becomes zero, shoot and reset the timer as before.
i got this:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bowser here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bowser extends Actor
{   private GifImage g = new GifImage("Bowserreadytofightleft.gif");
    private GifImage f = new GifImage("Bowserstarving.gif");
    private int minShotDelay = 40;
    private int maxShotDelay = 160;
    private int shotTimer = minShotDelay;
    /**
     * Act - do whatever the Bowser wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        standard();
        hitted();
        shootRandomly();
        if (shotTimer == 0)
        {
            getWorld().addObject(new Fireballs(), getX(), getY());
            shotTimer = minShotDelay+Greenfoot.getRandomNumber(1+maxShotDelay-minShotDelay);
        }
        else shotTimer--;
        
    }
    public void standard()
    {if(!isTouching(Fireball.class))
        {
        this.setImage(g.getCurrentImage());
    }
    }
    public void hitted()
    {if(isTouching(Fireball.class))
        {
        this.setImage(f.getCurrentImage());
        HealthBar.health--;
    }
    
    }
    
     public void shootRandomly()
    {
     Fireballs fireballs = new Fireballs();
    getWorld().addObject( fireballs,  getX(), getY());
    }  
    
    


}
But it does nothing?!
danpost danpost

2018/12/6

#
pleasetroll wrote...
i got this: << Code Omitted >> But it does nothing?!
Remove lines 49-50 and replace with (cut/paste) lines 24=29.
pleasetroll pleasetroll

2018/12/6

#
danpost wrote...
pleasetroll wrote...
i got this: << Code Omitted >> But it does nothing?!
Remove lines 49-50 and replace with (cut/paste) lines 24=29.
THANK YOU SO MUCH!!!
You need to login to post a reply.