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

2012/10/4

Removing an object and adding it again at the same place.

Codybean1 Codybean1

2012/10/4

#
I know about adding anobject and removing one, but I want an object to be added at the same position it was removed. How do I go about doing this? :)
-nic- -nic-

2012/10/4

#
do you want to remove one & add one object because you could do (in the world)
oldX=Object.getx();
oldY=Object.gety();
removeObject(Object);
//condition to add object again
add Object(Object,oldX,oldY);
-nic- -nic-

2012/10/4

#
hope i helped
Codybean1 Codybean1

2012/10/4

#
Having a bit of trouble implementing the code. The Object I want to remove is called "SneakLeft". Also having trouble with what oldX and Y is and what I should be replacing it with.
-nic- -nic-

2012/10/4

#
oldX and oldY are just names for the saved coordanates of the sneakleft so you can put it back at the same place
-nic- -nic-

2012/10/4

#
did i help?
Gevater_Tod4711 Gevater_Tod4711

2012/10/5

#
try this:
public void readdObject() {
    //saving the old objects coordinates in this integer variables;
    int objectsX;
    int objectsY;
    //the part where you find the object;
    Actor actor = getOneObjectAtOffset(Actor.class, 0, 0); //could also be other classes or methods you are using now. Just leave yours;
    //give the values of the objects coordinates to the variables objectsX and objectsY;
    objectsX = actor.getX();
    objectsY = actor.getY();
    getWorld().removeObject(actor);//remove the actor;
    getWorld().addObject(new Actor(), objectsX, objectsY);//add the new Actor. Probably Actor is the wrong type. Just add the type you want;
}
Codybean1 Codybean1

2012/10/8

#
Here's what I got: public void readdObject() { //saving the old objects coordinates in this integer variables; int objectsX; int objectsY; //the part where you find the object; SneakLeft sneakleft = getOneObjectAtOffset(SneakLeft.class, 0, 0); //could also be other classes or methods you are using now. Just leave yours; //give the values of the objects coordinates to the variables objectsX and objectsY; objectsX = sneakleft.getX(); objectsY = sneakleft.getY(); getWorld().removeObject(sneakleft);//remove the actor; getWorld().addObject(new SneakLeft(), objectsX, objectsY);//add the new Actor. Probably Actor is the wrong type. Just add the type you want; } I'm getting a "cannot find symbol- method getOneObjectAtOffset(java.lang.Class<SneakLeft>,int,int)" error when compiling. Thanks for all you guys' help btw.
-nic- -nic-

2012/10/8

#
i think its
SneakLeft sneakleft = getOneObjectAtOffset(0,0,SneakLeft.class);
-nic- -nic-

2012/10/8

#
oh wait not sure Gravatar put that
Game/maniac Game/maniac

2012/10/8

#
is the class your putting this method in an actor or a world
Codybean1 Codybean1

2012/10/8

#
I was putting the method in my world.
danpost danpost

2012/10/9

#
This discussion is getting rather confusing. Hopefully, I can make things a little bit clearer. In order to add an object back to the world at the same location it was removed from, with code in the world class: you will need to save the object and both the x and y coordinates of the object before removing it.
// world class variables (or fields)
SneakLeft sneakleft; // to hold the object
int sneakleftX, sneakleftY; // to hold the removal location of the object
// when creating SneakLeft object (in constructor of world)
sneakleft = new SneakLeft();
addObject(sneakleft, whateverX, whateverY);
// when removing 'sneakleft'
sneakleftX = sneakleft.getX();
sneakleftY = sneakleft.getY();
removeObject(sneakleft);
// when re-adding 'sneakleft'
addObject(sneakleft, sneakleftX, sneakleftY);
You need to login to post a reply.