Java game collision detection need help!
I am a first time programmer and i have to make a game in java for a unit at college and i am really struggling with something, my game is about a cannon that shoots cannon balls at the control of the player to try and hit and pop balloons that are by the top of the page, whilst trying to avoid hitting bombs that move from left to right randomly between the cannon and the balloons, each time a player hits a balloon it disappears but each time they hit a bomb they lose a life. I managed to get a code to make the balloons hit disappear and it works but I'm struggling with the part where i make a player lose a life each time they hit a bomb part. i have tried a code for it and it says there are no errors but it doesn't do what it is supposed to do it just does nothing when the ball hits a bomb so i was wondering if there was anyone that could help me out with this problem. Here is the code for the balloon collision and the one that i tried for removing lives, they are all in the ball class
public void act()
{
move(20.0);
hasCollided();
}
public void hasCollided()
{
Actor RedBalloon = getOneObjectAtOffset(0, 0, RedBalloon.class);
Actor Bomb = getOneObjectAtOffset(0, 0, Bomb.class);
Actor Lives = getOneObjectAtOffset(0, 0, Lives.class);
{
if(RedBalloon != null) {
Game world = (Game) getWorld();
world.removeObject(RedBalloon);
if(Bomb != null) {
getWorld();
world.removeObject(Lives);
}
}
}
}
}
Let me know if you need any more information
Re: Java game collision detection need help!
Re: Java game collision detection need help!
Do you have an int named lives? If so just find out if the cannon intersects then lives--;. Not sure if that helps, the full code would help.