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

2013/11/13

getActor method

PrezHawkeye PrezHawkeye

2013/11/13

#
Basically i'm trying to write a method that returns the number of Character A that the player killed and another method that returns the number of character B remaining after all instances of Character A have been removed from the world. Would Greenfoot's getActor method work for this idea or is there a better way of coding this.
danpost danpost

2013/11/13

#
To track the number of objects of a type removed from the world, you would need to use an instance field; this can be in the world class or in a counter class (it could be elsewhere; but then certain condition would need to be met, depending on where it is put). 'get' and 'set' methods would need to implemented for the field. These methods would be used by your actor class(es) to keep its current value. The 'act' method in your world class would be used to check whether none of the one type of object are in the world and then get the number of the other type of object.
danpost danpost

2013/11/13

#
I think I should mention that 'getActor' is a method supplied by greenfoot only in the MouseInfo class. That is, other than getting the actor that a mouse action is performed on, the 'getActor' method is not used without creating the method yourself. You are probably referring to the World class method 'getObjects' which returns a List object containing the objects of the class given that are in the world (or all objects in the world if 'null' is passed as the argument). A List class method ('size()', 'get(int)', 'isEmpty()' to name a few) would then be applied to the List object to obtain information about or from the list.
You need to login to post a reply.