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

2013/1/7

Need help please! re adding a removed object

ddavid33 ddavid33

2013/1/7

#
i've made a game that involve picking up and object and places it in a different location. i have a little problem, i want the removed object to reappear in the area it was dropped, is that possible? thanks in advance david
danpost danpost

2013/1/7

#
You will need an instance Actor field in the class code of the object that does the picking up. It should initially be set to 'null' as well as set to 'null' when the object is dropped off. While the object is being held (after picked up and before dropped off), the field should be set to the object being held.
// instance fields
Actor objHeld = null;

// picking up
if (objHeld == null && Greenfoot.isKeyDown("u"))
{
    objHeld = getOneIntersectingObject(Whatever.class);
    if (objHeld != null) getWorld().removeObject(objHeld);
}
// releasing 
if (objHeld != null && Greenfoot.isKeyDown("r"))
{
    getWorld().addObject(objHeld, getX(), getY());
    objHeld = null;
}
Oh, to drop off in the area it was dropped, add two instance int fields to hold the x and y of the object being removed from the world, and check for proximity to that location before dropping off the object.
LordBeno LordBeno

2013/1/7

#
instead of removing it, just try to reposition it using command: setLocation(Xpos, Ypos);
danpost danpost

2013/1/7

#
Forget my last statement in the above post, when I re-read your initial post I thought maybe I had originally mis-interpreted what you wanted; but I believe I was initially spot-on now.
danpost danpost

2013/1/7

#
@LordBeno, I believe ddavid33 wants the actor that picks up the object to move to a different location and drop it off there as if carrying the object with him (not for his character to perform magic and have it just appear at the new location).
LordBeno LordBeno

2013/1/7

#
oh my bad xD
You need to login to post a reply.