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

2014/2/24

How to spawn an object in front of the Player

Alex89 Alex89

2014/2/24

#
Hi, title says it all. I'm looking for a way to spawn an object right in front of the player. The special thing about this is that the angle is also important. That means it should always spawn in front of the player's face no matter in which direction he/she/it is turning. I tried to do it with the addObject method but no success in any way I could think of using it since I can only add objects with a X and Y coordinate. Any help? Thanks in advance.
Zamoht Zamoht

2014/2/24

#
Add this to your player class where you want to add the object.
1
2
3
4
Actor actor = new Actor(); //This will be the object you want to add.
getWorld().addObject(actor, getX(), getY()); //Add the object to the world.
actor.setRotation(getRotation()); //Sets the rotation of the new object to be equal to the players rotation.
actor.move(x); //Move the added object x pixels in front of the player.
Alex89 Alex89

2014/2/25

#
Thanks I'm gonna try this.
You need to login to post a reply.