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

2016/10/7

How to spawn object after 3 seconds

Matchr Matchr

2016/10/7

#
private int ObjectFallTimer;
//summon object ove time   
 private void ObjectFall()
    {
        //insert timer code here using available code.
    }
// decide what object will be spawned    
public void ObjectRandom(){
        int objectChooser;
        for(;;)
        {
            objectChooser=Greenfoot.getRandomNumber(5);
            if ( objectChooser == 1){
               BananaFall();
            }
            if ( objectChooser == 2){
               PenFall();
            }
            if ( objectChooser == 3){
               PinnapleFall();
            }
            if ( objectChooser == 4){
               AppleFall();
            }
        }
    }
    public void BananaFall(){
        int bf = Greenfoot.getRandomNumber(481);
        addObject(new Banana(),bf,0);
    }
    public void PenFall(){
        int pnf = Greenfoot.getRandomNumber(481);
        addObject(new Pen(),pnf,0);
    }
    public void PinnapleFall(){
        int pf = Greenfoot.getRandomNumber(481);
        addObject(new Pinnaple(),pf,0);
    }
    public void AppleFall(){
        int af = Greenfoot.getRandomNumber(481);
        addObject(new Apple(),af,0);
    }
danpost danpost

2016/10/7

#
You just need an int field for the timer (like your ObjectFallTimer field). Increment the timer in the act method (or a method called from there, like your ObjectFall method) so that it continually runs. After incrementing, check its value against some limiting value (maybe between 160 and 180, which is about 3 seconds in a normal speed scenario). If it value reaches that limit, spawn a random actor and reset the time back to zero.
Matchr Matchr

2016/10/8

#
like this?maybe
//forgot the ObjectFallTimer
//let say
int t=0;
int i=0;
public void ObjectFall(){
//summon object maximum 50 times
        if (i <=50){
            i++;
//how to keep this sequencing?  
//this code is for timing       
        if( t<=180 )
        {
            t++;
        }
            ObjectRandom();
        }
}
danpost danpost

2016/10/8

#
You want the timer to increment every act cycle, so do not place any conditions on incrementing it (you do not need the 'i' field; and line 13, the incrementing of 't', should be outside any 'if' blocks). The only thing that is conditional (placed within an 'if' block) is calling 'ObjectRandom', along with resetting the value of 't' back to zero (and that is when the timer has reached the given limit).
You need to login to post a reply.