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

2015/3/24

Creating a bullet on the gun

jordan32 jordan32

2015/3/24

#
I have a player class and a bullet class. When the player presses the shoot key it creates a bullet object, however, I want to create the bullet where the gun is on the player which isn't the middle of the player. Any help would be appreciated, thank you.
danpost danpost

2015/3/24

#
You are probably adding the bullet into the world using something like this:
getWorld().addObject(new Bullet(getRotation()), getX(), getY());
There is a lot you can do with the new bullet at this point in the code if only you create a reference to the bullet before adding it into the world:
Bullet bullet = new Bullet();
Now, with the reference, you can:
// of course, add it to the world
getWorld().addObject(bullet, getX(), getY());
// set its rotation to that of the player
bullet.setRotation(getRotation());
// move forward a bit
bullet.move(30);
// move perpendicular to rotation
bullet.turn(90);
bullet.move(10);
bullet.turn(-90);
// set its image
bullet.setImage("harpoon.png");
There is more that could be done. It would be the perfect place to pass information to the new bullet object -- for examples: target location, what score counter to use if target was hit, the actor that created it.
jordan32 jordan32

2015/3/25

#
Thank you! I had:
Bullet bullet = new Bullet();
I just never thought about moving the bullet like that.
You need to login to post a reply.