Reputation: 73
I have this code for my DoLogic method. And I'm trying to do a intersection between the shots and the obstacles but I really can't think of nothing.. cause both are different objects.. i tried to do some but it didn't really detect something at all.
for(int i=0; i<shots.length; i++)
{
if(shots[i] != null)
{
shots[i].moveShot(SHOTSPEED);
if(shots[i].getXPos() > 1280)
{
shots[i] = null;
}
}
}
for(int i=0; i<obstacles.length; i++)
{
if(obstacles[i] == null)
{
obstacles[i] = generateObstacle();
break;
}
if(obstacles[i] != null)
{
obstacles[i].moveObstacle();
if(obstacles[i].getXPos() < 10)
{
obstacles[i] = null;
}
else if(obstacles[i].intersects(Player1.character))
{
obstacles[i] = null;
GameSounds.hit("/resources/8bit_bomb_explosion.wav");
lives--;
}
}
}
Can you guys give me an example or at least an advice how to do an intersection between an obstacle and a shot?
Upvotes: 0
Views: 135
Reputation: 168835
Do these classes implement Shape
? If not, they should. See the answer to Collision detection with complex shapes for an SSCCE.
..and i should implement the Rectangle in Obstacle and Oval in shot?
That seems logical to me, from your description of both objects.
..i just type implements Shape?
I would tend to use a Rectangle2D
or Rectangle2D.Double
for the obstacle & an Ellipse2D
or Ellipse2D.Double
for the shot. Rather than extend them, just hold them as an instance variable.
Give it a go & let us know how you go. If you get stuck, post an SSCCE of your best attempt.
You might need to hot-link to some small images.
..
..
Upvotes: 1