This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
I am creating a 2D Java game that will have two players going against each other for the most points. The issue I am running into is when either player fires a bullet that player takes damage. Currently, the bullet is doing damage, but it should not be damaging itself to fire. Originally I was using the method intersect to detect the collision, but the results were basically the same with the exception of the players taking damage faster.
I believe my issue is I need a way to set who owns the bullet, but my thought process is I can use the enumeration class to have the ID set to Player and Player 2. This process has been working for collisions of a bullet at the enemy, bullet at the wall, and enemy at the player. I provided my attempt at handling the collision of the bullet into the player and also the method that creates the bullet for the player that shot.
for (int i=0; i<handler.object.size(); i ) {
GameObject tempObject = handler.object.get(i);
if (tempObject.getId() == ID.Bullet) {
// Player 1 hit with bullet
if (id == ID.Player) {
if (getBounds().contains(tempObject.getBounds())) {
System.out.println("Player 1 Health: " game.hpPlayer1);
game.hpPlayer1--;
System.out.println("Player 1 Health: " game.hpPlayer1);
}
}
// Player 2 hit with bullet
if (id == ID.Player2) {
if (getBounds().contains(tempObject.getBounds())) {
System.out.println("Player 2 Health: " game.hpPlayer2);
game.hpPlayer2--;
System.out.println("Player 2 Hit: " game.hpPlayer2);
}
}
}
}
// Creates the bullet for the player that shot
private void shootBullet() {
// Limits the tank to one shot per second
if (System.currentTimeMillis() - LastFired > 1000) {
// Player 1
if (id == ID.Player) {
if (game.ammoPlayer1 >= 1) {
handler.addObject(new Bullet(x 20, y 19, ID.Bullet, handler, ss, angle));
LastFired = System.currentTimeMillis();
game.ammoPlayer1--;
}
}
// Player 2
if (id == ID.Player2) {
if (game.ammoPlayer2 >= 1) {
handler.addObject(new Bullet(x 20, y 19, ID.Bullet, handler, ss, angle));
LastFired = System.currentTimeMillis();
game.ammoPlayer2--;
}
}
}
}
Subreddit
Post Details
- Posted
- 5 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/javahelp/co...