I've tried so hard to get this to work. I've been working on a way to grab objects out of the list that getIntersectingObjects() methods creates. I've created a reference for the list to store it in. I then made a condition to check if the list is empty. If it is not empty, I'm trying to check if a certain class is in the list with the .contains() method. The code works beautifully up to the printing of the collisionList. I get a great representation of what is colliding with the Crab. I just don't know how to grab a reference to what the crab is crossing. My code is as follows:
I've tried using Worm.class for the argument for .contains() but it doesn't recognize that as an Object. I'm now wondering if it's even possible to reference the specific Objects that are inside the List. If anyone could help me out that would be great. Thanks!
P.S. I've also imported the List from java.util, so that's not the issue.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /** * Detects if the worm is close to the crab, and if so, eats it. */ public void detectCollision() { List collisionList = getIntersectingObjects( null ); System.out.println(collisionList); if (!collisionList.isEmpty()){ //Check for Worm if (collisionList.contains( new Worm())){ System.out.println( "IT WORKED!" ); } } //if(worm != null) //{ // world.removeObject(((Worm)worm)); // Greenfoot.playSound("eating.wav"); // wormsEaten++; // ((CrabWorld) getWorld()).eatenWorm(); //} } |