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

2013/5/26

Donkey Kong Game barrel Srimulation

toscany toscany

2013/5/26

#
I am using Greenfoot for the first time and I am making a Donkey Kong Game. Everything is work fine but the only problem I have is how to stimulate a barrel with a delay in between each barrel.
public void lunchBarrels(){      
                             
            for(int i = 0; i < 50; i++){
                addObject(Barrel.class, Greenfoot.getRandomNumber(1300), 118);
                this.delayObjectInstantiation();           
     
  
    }
    
     public void delayObjectInstantiation(){
         int delay = 50;
         while(delay > 0){
                delay--;
                if(delay == 0) return;
         }
     }
   
These functions are present in the World class. lunchBarrels function is call on the World class constructor. The problem is that it is not working as I planned. If any body has an idea on how to solve this problem , I will really like you input. Thank in advance
Your code just doesn't work. What you're trying to do is delay the barrel, but your causing a "delay" that happens in a time before the program even renders everything. If you ever want a delay that lasts a couple of frames, never put it in a while statement. That while statement happens before a single frame is rendered. What you need to do is have an int that will keep track of the time since it last fired (by subtracting 1 from itself every frame), then have a method that will return true if that counter is 0. When the barrel fires, you tell the counter to go back to 50. So I would have something like this:
//in your instance variables
private int barrelFireCounter = 0;
...
//constructor and other methods
...

public void act()
{
    if (barrelCanFire()) //Or whatever you want to call it
        fire();
    if (barrellFireCounter > 0)
        barrelFireCounter--;
    ...
}

public boolean barrelCanFire())
{
    return barrelFireCounter <= 0 //And whatever else is need to fire
}

public void fire()
{
    ...
    barrelFireCounter = 50;
}
I hoped that helped!
You need to login to post a reply.