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

2013/3/21

Adding an object question

Gingervitis Gingervitis

2013/3/21

#
Is there a way to randomly add an object that never spawns where the actor you control is?
danpost danpost

2013/3/22

#
You did not give the class name for your controlled actor, so I will call it 'Player'. In the class of the object being spawned, add the following method:
public void addedToWorld(World world)
{
    Player player=(Player) world.getObjects(Player.class).get(0);
    int offsetX=Greenfoot.getRandomNumber(world.getWidth()-100);
    int offsetY=Greenfoot.getRandomNumber(world.getHeight()-100);
    int spawnX=(player.getX()+50+offsetX)%world.getWidth();
    int spawnY=(player.getY()+50+offsetY)%world.getHeight();
    setLocation(spawnX,spawnY);
}
You can then add the object anywhere -- use (0, 0) -- and it will be moved to a random location off the player.
Gingervitis Gingervitis

2013/3/23

#
When I included this into my code, it made actor move really fast, bouncing all over the screen.
danpost danpost

2013/3/23

#
The code I gave will only affect the initial location of the spawned object, anything it or any other actor does after it is added to the world is being caused by some other code (not this code). Maybe you should show what code you have for your bouncing actor.
You need to login to post a reply.