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

2018/2/26

A side scroller game

Joman2k Joman2k

2018/2/26

#
I have made a side scroller game, very basic with a man that will jump over some walls he doesn't move the walls they move towards him. All the player has to do is jump over the wall. the problem is I cant figure out how to make the walls spawn at random times, because all i have right now is a single wall at the start and no more will be made is there anyway to make them spawn randomly at a certain coordinate? Any help will be greatly appreciated, Thanks!
danpost danpost

2018/2/26

#
You can add an act method to your world class:
public void act()
{
    if (<< conditions are right >>) addObject(new Wall(), getWidth()+30, getHeight()-1);
}
The condition will probably incorporate the use of an int field for a countdown timer.
Joman2k Joman2k

2018/2/27

#
Thanks for the help! could you give an example of a countdown timer for the condition?
Joman2k Joman2k

2018/2/27

#
never mind! i figured it out, i used this
if (Greenfoot.getRandomNumber(100) == 1) addObject(new wall(),1000,675);
the only issue i have now is that it might spawn 2 walls right next to each other and I need to make it so after spawning one it will wait then start the condition again. Does anyone know how to do this?
danpost danpost

2018/2/27

#
You will need an int timer field. Set it to the sum of a constant (minimum "time") and a random (variable "time"):
// field
private int spawnTimer;

// in act
if (spawnTimer >= 0 && ++spawnTimer < 0)
{
    addObject(new wall(), 1000, 675);
    spawnTimer = 100+Greenfoot.getRandomNumber(100); // play around with the '100' values
}
You need to login to post a reply.