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

2019/4/8

Where is the Object?

TNesler TNesler

2019/4/8

#
Hello! I have created a subclass based on the Actor class to put some instructions on the screen. I use the prepare method to place the object. I want to remove the instructions when I start the game so I put this code in the Actor method of my main object:
getWorld().removeObject(Instructions);
However, I get an error saying the program can't find symbol...:-( I tried putting in Instructions.class in place of Instructions but I get this error: "Incompatible types" What am I doing wrong? Thanks for you help!
Erasedsword Erasedsword

2019/4/8

#
Random question is the class with the instructions the object you are referring to? If that is the case try getWorld().removeObject(this); I would say that the issue with the line of code you have provided is that you are trying to remove a string, while the removeObject method is looking for an actor.
Super_Hippo Super_Hippo

2019/4/8

#
The 'removeObject' method requires an Actor. In the first option, you passed a non-existent variable "Instructions", in the second option you passed the class Instructions. If you want to remove the object inside the Instructions class, then you can use "this" to get a reference to the object and remove it. However, you don't seem to do that. You can simply remove it from the world.
private Instructions instructions = new Instructions();

//when adding it
addObject(instructions, <x-value>, <y-value>);

//when removing it
removeObject(instructions);
If you wish to not have a reference to the Instructions object in your world, you can use this line to remove all Instructions objects from the world.
removeObjects(getObjects(Instructions.class));

//or, called from an Actor inside the world
getWorld().removeObjects(getWorld().getObjects(Instructions.class));
TNesler TNesler

2019/4/8

#
Thanks for the quick reply. Since I created the instructions object when I prepared the world, I don't think I should use option 1. Option 2, (Line 1) would be put in the World's Act method? Option 3 (Line 4) is what I was looking for,so that is what I used. Thanks! This is one of my pet peeves about Java, nesting objects. I realize that you could create different variables and use them, but that is equally confusing, IMO. Java is not a very good self documenting language!...:-)
Super_Hippo Super_Hippo

2019/4/8

#
Option 1: Not a problem if you created it in the prepare method. Option 2 and 3 are basically the same. I don't know what exactly this has to do with nesting objects though.
You need to login to post a reply.