1

Alright, so I get this error when I try to make it so that when BallProjectile collides with an obstacle, the ball stops and makes a new one.

// Checks if the ball is colliding with an obstace, then stops it if it is

BallProjectile obstacleCollision = (BallProjectile) getOneIntersectingObject(Obstacle.class);
        if (obstacleCollision != null)
        {
            xSpeed = 0;
            ySpeed = 0;
            myWorld.addObject(new BallProjectile(), 50, 559);
            return;
        }

How do I stop this error? Note that obstacle is just that, a circle created to get in the way of the ball.

Jack
  • 15
  • 7
  • add some debug statements into your code to verify that the correct code is being called when expected – Scary Wombat Nov 13 '14 at 00:14
  • So I think the problem is that getSpeedValue and getAngleValue are broken, not athing in BallProjectile, but I've been doing trial and error for hours and nothing has worked – Jack Nov 13 '14 at 05:15
  • I guess it would be interesting to see what the `getOneIntersectingObject()` method returns. – mattias Nov 15 '14 at 01:24
  • This set of comments was from a while ago, and it unrelated to the question at hand, I'm just not sure how to delete this comment chain – Jack Nov 15 '14 at 01:30

1 Answers1

0

The only place where you're doing a cast in the code provided is:

BallProjectile obstacleCollision = (BallProjectile) getOneIntersectingObject(Obstacle.class);

So I think it's where your problem is. Seems like the return of this method cannot be cast as BallProjectile

EDIT:

To verify your collision you could do either:

Obstacle obstacleCollision = (Obstacle) getOneIntersectingObject(Obstacle.class);
if(obstacleCollision != null) {
   //do your things here
}

OR

Actor obstacleCollision = getOneIntersectingObject(Obstacle.class);
if(obstacleCollision != null) {
   //do your things here
}

Both approaches should work the same way

Diego Urenia
  • 1,620
  • 2
  • 21
  • 28
  • If that's the case how do I make it detect that it's colliding with the obstacle and stop the ball? – Jack Nov 15 '14 at 01:30
  • Just a quick note, I am very new to Greenfoot. Anyway, If I remove the casting, I get an incompatible types error and Greenfoot highlights Obstacle.class – Jack Nov 15 '14 at 01:44
  • The java API says: getOneIntersectingObject protected Actor getOneIntersectingObject(java.lang.Class cls) Return an object that intersects this object. This takes the graphical extent of objects into consideration. Parameters: cls - Class of objects to look for (passing 'null' will find all objects). – Jack Nov 15 '14 at 02:00