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

2014/1/6

Spawning objects after time

Amature Amature

2014/1/6

#
Hey guys so all i need now to finish my plants vs zombies game is letting the zombies spawn after a certain time. With this i mean that once the first zombie spawns, wait for 5 seconds to let the second one spawn. Can anybody help me?
danpost danpost

2014/1/6

#
Add an instance int field to your world class. Increment the value in the act method and check it to be equal to some number around 250 to 300. When number has been reached, reset the value of the field back to zero and spawn a zombie. This process can also be done in a method that the act method calls.
// instance field in world class
private int zombieSpawnTimer;
// in act method of world class
runZombieSpawnTimer();
// new method in world class
private void runZombieSpawnTimer()
{
    zombieSpawnTimer = (zombieSpawnTimer+1)%300; // adjust '300' as desired
    if (zombieSpawnTimer == 0) spawnZombie();
}

private void spawnZombie()
{
    // add code spawning zombie here
}
Line 8 does both incrementing the timer AND resetting it back to zero when '300' (or whatever number you make it) is reached. '300' should be around 5 to 6 seconds if your scenario is run at normal speeds (between 50 and 60 frames, or act cycles, per second).
Amature Amature

2014/1/8

#
thanks!!! it worked! now i just have one more. When I place my Plant down to shoot it will shoot whenever a Parent is in the screen. But it doesn't shoot one it just continues shooting forever. I know why it does this but I don't know how exactly to fix it!
public void shoot()
    {
        Actor Parent = getParentInRange(); 
        if (Parent!= null) 
        {
            WaterBall wb = new WaterBall();
            getWorld().addObject(wb, getX(), getY());
            wb.turnTowards(Parent.getX(), Parent.getY());
            wb.move(10);
        }
    }
danpost danpost

2014/1/8

#
You will need to include an instance boolean field to the Plant class to indicate a shot has been taken:
// add instance field
private boolean shotTaken;
// in act, replace 'shoot();' with
if (!shotTaken) shoot();
// in 'shoot' method
shotTaken = true;
That should only allow one shot to be taken.
Amature Amature

2014/1/9

#
okay that works, but now since i got more parents spawning per level (i have a 60 second timer) if the another parent spawns in sight of one girl, the girl doesnt shoot, i also want it to shoot once when a parent is in their lane
danpost danpost

2014/1/9

#
Correct me if I am wrong. You want the girl to shoot one time at each parent that comes in range of her, whether spawned in range or moved within range. If a parent moves back out of range and then back in again, is she to shoot at that parent a second time? is there a restriction on how fast the girl can produce shoots? I just need to know what needs to be considered.
Amature Amature

2014/1/9

#
so basically, it will be like a plants vs zombies game. If you place a girl down I want it to have a certain range of y and x that are about as big as a square in the black and white background. And whenever a parent spawns into her range that she covers no matter the x- value she should shoot a ball at the parent. (The parents only move forward not to the side) So basically like a plants vs zombies where the plant shoots at the zombie
danpost danpost

2014/1/9

#
You need to be specific (not everybody is familiar with this 'plant vs zombies' game -- I am not, for one). None of this 'basically' stuff. Give exacting details, stating exactly what you want. Do not use expressions like 'her range' or 'as big as a square...'; give exact dimensions in pixels. I asked for one conformation and answers to two questions. I got an answer to the first question, but the other question remains unanswered and the conformation was round-about and confusing. Still, the first question can now be better asked with 'Should the girl be allowed to shoot at the same parent more than once?'.
danpost danpost

2014/1/9

#
If the parent is removed when hit, state so. If the girl can move, state how she moves (directions allowed, whether she moves automatically or by keyboard (or mouse) input, etc.). Confirm that shots always travel along a horizontal path from left to right. If not, state how they move. State all limitation you wish to impose on the girl with her shooting at the parents. No only should you state when a shot can or should be fired, you should state when she is not allowed to fire a shot, whether a delay between shots is required and by how much (exactly), whether there is any time limit between shots at the same parent (if allowed), etc.
danpost danpost

2014/1/9

#
Sorry if I am coming across a little rash. It is just that you are asking for a lot of help with very little to go on. Progamming is a precise art which requires a 'blueprint' for each project. Having that 'blueprint' in your mind does in no way help others to help you, unless you roll it out to look at. Again, my apologies if I have crossed you in any way.
Amature Amature

2014/1/13

#
No I was just rethinking my blue print, I will come back to you once I have put out the details of what I want to add, thanks for help :)
MacG MacG

2016/7/23

#
I've got a little problem. I wanna addObject with some interval, But I want it loop, every 2 seconds, the zombie will show and it not stoping, can I do that ?
danpost danpost

2016/7/23

#
MacG wrote...
I wanna addObject with some interval, But I want it loop, every 2 seconds, the zombie will show and it not stoping, can I do that ?
Just count act cycles; and when it reach the number of act cycles executed within a 2 second interval (usually something between 100 and 120) spawn a zombie and reset the 'int counter' field.
You need to login to post a reply.