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

2014/12/26

Prevent Objects spawning on eachother

HELLOITSME HELLOITSME

2014/12/26

#
HEY, dunno how to prevent objects spawning on eachother. I know I have to put it in the code below. public void spawnContainer() { int spawnX = Greenfoot.getRandomNumber(1024); if(Greenfoot.getRandomNumber(80) == 1){ getWorld().addObject(new Container(), spawnX, 0); } } My objects do move down and also remove when they are at the edge but problem is sometimes they spawn on eachother D: thx in advance :)
danpost danpost

2014/12/26

#
Being you are creating these Container objects on a random basis, I believe it would not be a problem to just have them remove themselves from the world when spawn on another. You can adjust the rate of spawning to compensate by lowering the random number generated. This would increase the rate of spawning whilst the removal will apparently decrease it. You can make use of the 'addedToWorld' method in the Container class to place the check for intersectors and removal if needed.
danpost danpost

2014/12/26

#
There may be something to be concerned with. The code provided above suggests that an Actor object in the world is spawning the Container objects. What class do you actually have the code in? are Container object only supposed to spawn when this actor is in the world? and, is there never any more than one of these actors (actors created from the class the code is in) in the world?
HELLOITSME HELLOITSME

2014/12/26

#
ye I made a new Actor object for spawning Objects. Tried to let them spawn in Road (extends world) but it wont spawn. What I did is add the object spawnContainers in Road so it'll continue spawning
danpost danpost

2014/12/26

#
There is an 'act' method for world subclasses also -- it just does not get automatically added into the class when you create it as when you create an Actor subclass. You can cut/paste the 'spawnContainer' method into your Road class (remove 'getWorld().' from the last line); then add the following into the Road class:
public void act()
{
    spawnContainer();
}
Finally, you can remove the 'spawnContainers' class. From there, you could replace the call to the 'spawnContainer' method in the act method it with the code from within the 'spawnContainer method; then remove the 'spawnContainer' method
HELLOITSME HELLOITSME

2014/12/26

#
ah i see thanks im still a noob haha, but how do I make use of the addedtoworld method?
fejfo fejfo

2014/12/26

#
public void addedToWorld() {
         code here
}
danpost danpost

2014/12/27

#
fejfo wrote...
public void addedToWorld() {
         code here
}
That is not correct:
protected void addedToWorld(World world)
{
    if (isTouching(Container.class))
    {
        world.removeObject(this);
    }
}
The greenfoot framework will call this method on any actor object when it is added into a world object.
HELLOITSME HELLOITSME

2014/12/27

#
ah man fixed it thanks danpost! :)
You need to login to post a reply.