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

2019/5/24

Spawn Actors in Certain Area

AdiBak AdiBak

2019/5/24

#
Hi, I'm trying to make one of my actors, Traffic, spawn in the middle third of the world. However, I'm not sure how to do this. Can someone help? Thank you. Here's the snippet in MyWorld code:
if (trafficCounter == 100){
            Traffic t = new Traffic();
            addObject(t, Greenfoot.getRandomNumber(getWidth()/4) * 4, 0);
            trafficCounter = 0;
        }
danpost danpost

2019/5/24

#
Dividing by 4 and then multiplying by 4 will still get you anywhere across the world, but only at any x-coordinate that is a multiple of 4. Take the value of 1/3 the world width and add a random up to 1/3 the world width.
You need to login to post a reply.