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

2012/2/24

Really Basic Question!

1
2
TheNightStrider TheNightStrider

2012/2/24

#
Say i have a actor, Play, int the world and i want to have some code in the world that removes this object - i can understand (this) but am not sure about this. Thanks
Starsonovasa Starsonovasa

2012/2/24

#
If you are removing the actor from the world class, then make sure that when you create a new instance of the actor in the world class, you first make an object of the class with a name and then add the object into the world. Afterwards, you simply need to put: removeObject(nameOfObject*); * - nameOfObject is whatever you deciced to name the object
TheNightStrider TheNightStrider

2012/2/24

#
Example pls?
TopInPut TopInPut

2012/2/24

#
Just write :
public void kill() {
Actor actor = (Actor) getOneObjectAtOffset(0, 0, Actor.class);
if(onActor()) {
getWorld().removeObject(actor);
}
}
TheNightStrider TheNightStrider

2012/2/24

#
but what if i don't want to check if there is an object at offset - just i want to remove a object?
Starsonovasa Starsonovasa

2012/2/24

#
Actor something = new Something();
//that will create the object that you want to get rid of
getWorld().removeObject(something);
Duta Duta

2012/2/24

#
@TopInPut What is the contents of the method onActor and how is it getting a reference to that particular actor? Surely its if(actor != null) as opposed to if(onActor())
TheNightStrider wrote...
but what if i don't want to check if there is an object at offset - just i want to remove a object?
Then just do this (I'm assuming you have a reference on the object, which I've called (imaginatively) theActor
getWorld().removeObject(theActor);
This is assuming that theActor's class is a subclass of Actor, and that you are placing the code into a class which is itself a subclass of Actor. If you're placing the code in a subclass of World, then just do the following:
removeObject(theActor);
and finally, if the call to remove theActor is coming from theActor itself, then replace theActor with this
danpost danpost

2012/2/24

#
If the object to be removed is the only actor of its type currently in the world, then you could use
removeObjects(getObjects(Play.class));
Above, I assumed that Play was the actor Class of the object to be removed. If 'Play' is not the actor class, but a reference to an instance of that class, being the actor you want removed, then use
removeObject(Play);
TheNightStrider TheNightStrider

2012/2/24

#
Thanks alot for all replys - i am still very very new to this, but thanks alot.
Duta Duta

2012/2/24

#
Everyone starts somewhere :) Good luck and happy coding!
TheNightStrider TheNightStrider

2012/2/25

#
BTw, how do i reference a class?? Like an actor?
danpost danpost

2012/2/25

#
There are several ways that a reference to an actor may be created. To list a few: (1) In the case that there is only a limited number of such class actors to be put in the world, and there are added to the world within the constructor of the world, you can use the following for one, and repeat with different names for others, the following:
public Player player = new Player();
as a world instance Player variable and then 'player' can be used anywhere in the world class to refer to that player. (2) Again, in the world class, if one and only one Player is ever (and always) in the world, then anywhere in the world class you can get a reference to it by way of the following:
Player player = (Player) (getObjects(Player.class).get(0));
(3) If you get a reference in the world class, you can send it to an actor class by way of the constructor of that actor class, as follows:
 Counter counter = new Counter();
Scoreboard scoreboard = new Scoreboard(counter);
you just need to set up an instance object variable in the class recieving the reference to the other object, and create a constructor to recieve it and set it to that instance object variable. (4) Other ways include using 'getOneIntersectionObject(Class class)' or 'getOneObjectAtOffset(int xOffset, int yOffset, Class class) or referencing the world to use 'getObjects(Class class)' which returns a list of object that need to be checked for null, and if not null, get the first object off the list and cast it to the appropriate class, and assigned to an object variable of said class.
TheNightStrider TheNightStrider

2012/2/26

#
thx
TheNightStrider TheNightStrider

2012/2/27

#
Also, how do i set the instance object variable for recieving etc.
danpost danpost

2012/2/27

#
OK, in my example for (3) above you have a reference to a Counter class object being sent to a new Scoreboard class object. The Scoreboard class should start something like this:
import greenfoot.*;

public class Scoreboard extends Actor
{
    Counter counter = null;

    public Scoreboard(Counter countr)
    {
        counter = countr;
        updateImage();
    }
    // etc.
There are more replies on the next page.
1
2