This site requires JavaScript, please enable it in your browser!
Greenfoot back
JohnVarick
JohnVarick wrote ...

2015/6/17

Intersection help! :)

JohnVarick JohnVarick

2015/6/17

#
Hello! I am asking on how to REMOVE and object when a SEPARATE object is eaten.
   
    private void getCommand()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            run("left");
            setImage("left.png");
        } else if (Greenfoot.isKeyDown("right"))
        {
            run("right");
            setImage("right.png");
        } else 
        {
            stop();
        }

        if(Greenfoot.isKeyDown("space"))
        {
            jump();
            setImage("SO_JUMP.png");
        }

        if (!getIntersectingObjects(Border.class).isEmpty())
        {
            //http://www.greenfoot.org/topics/4270 <-- reference
            move(600);
        }
        
        if (!getIntersectingObjects(Heart2.class).isEmpty())
        {
           myWorld.removeObject(Border);
           // says cannot find variable "Border"
        }
    }
thank you!
danpost danpost

2015/6/17

#
'Border' is the name of one of your classes -- it does not refer to an actor object. More information is required to give a proper solution. How many Border instances are in the world? How many of these are to be removed when the player touches a Heart2 instance? If more than one Border instance is in the world, what is unique about the one you are hoping to have removed?
JohnVarick JohnVarick

2015/6/18

#
Hi again, there is only one Border, this object is acting as a "gate" and when Heart2 has been eaten by the Player, the Border object will be removed (as if it opened).
danpost danpost

2015/6/18

#
You could make use of the 'getObjects' method of the world class: getObjects(Border.class) or from the Actor class: getWorld().getObjects(Border.class) which creates a list of all Border objects in your world. The list will contain either one element (the one Border object in your world) or it will be empty (after the one Border object has been removed). The 'removeObjects' method of the world class can then be used, instead of the 'removeObject' method, to have any objects listed removed from the world:
getWorld().removeObjects(getWorld().getObjects(Border.class));
or possibly this:
myWorld.removeObjects(myWorld.getObjects(Border.class));
You need to login to post a reply.