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

2022/5/30

How do you spawn actors on the top of the screen?

Phantom_Boi100 Phantom_Boi100

2022/5/30

#
I'm making a game where the player has to avoid the traffic and obstacles coming at the player, so I want the traffic/obstacles to spawn at the top. Any ideas? Thanks. Here's the width, height and cell size:
super(8, 8, 60)
If possible, please leave a reply before 6/6/22
Roshan123 Roshan123

2022/5/30

#
Use setPaintOrder() method. Not really sure what do you mean by “obstacles to spawn at the top”.
Phantom_Boi100 Phantom_Boi100

2022/5/30

#
Roshan123 wrote...
Use setPaintOrder() method. Not really sure what do you mean by “obstacles to spawn at the top”.
Basically, I want objects to spawn near the top of the screen/scenario. How would you do that?
Roshan123 Roshan123

2022/5/30

#
addObject(obstacle, getWidth()/2, getHeight ()/2);
OR
addObject(obstacle, x, y);
Now the x and y coordinate depends upon you, i.e. where you want to add
Phantom_Boi100 Phantom_Boi100

2022/5/31

#
Roshan123 wrote...
addObject(obstacle, getWidth()/2, getHeight ()/2);
OR
addObject(obstacle, x, y);
Now the x and y coordinate depends upon you, i.e. where you want to add
I did this but I can't get the actor "traffic" on (i have named it "Traffic"). I tried without a capital letter and with a capital letter, but neither way worked. How would you do it?
Roshan123 Roshan123

2022/5/31

#
Phantom_Boi100 wrote...
I did this but I can't get the actor "traffic" on (i have named it "Traffic"). I tried without a capital letter and with a capital letter, but neither way worked. How would you do it?
Use the variable traffic as a reference
Actor traffic = (Actor)getWorld().getObjects(Traffic.class).get(0);
danpost danpost

2022/5/31

#
To spawn an object on the top row, use the following:
int x = Greenfoot.getRandomNumber(getWidth());
addObject(new Traffic(), x, 0);
Phantom_Boi100 Phantom_Boi100

2022/6/10

#
danpost wrote...
To spawn an object on the top row, use the following:
int x = Greenfoot.getRandomNumber(getWidth());
addObject(new Traffic(), x, 0);
How do you put that x can be any value?
danpost danpost

2022/6/10

#
Phantom_Boi100 wrote...
How do you put that x can be any value?
Line 1 sets x to be a random value between 0 and 7, inclusive (since your world is 8 cells wide).
You need to login to post a reply.