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

2012/4/16

Enemy Iterations

Dingram Dingram

2012/4/16

#
Hi there, in my game there are multiple enemies of the same type which appear in each of the worlds. Is there a way, other than having them all as separate classes, to make it so that I can differentiate between them using variables. The reason I want to do this is so that the player can exit a "room" and when they return the enemies health is the same as when they left and any which have been killed stay dead, and in the location which they dropped. Thanks in advance.
davmac davmac

2012/4/16

#
You can store a reference to an actor in a variable when you create it. First you need to declare the instance variables:
  private Enemy enemy1;  // declare an instance variable
  private Enemy enemy2;
Then when you create the instances:
  enemy1 = new Enemy();
  addObject(enemy1, 100, 100);
  enemy2 = new Enemy();
  addObject(enemy2, 200, 100);
(I'm assuming this is in the world class). In other parts of the class you can refer to the variables 'enemy1' and 'enemy2'. If you have quite a large number of enemies you could use an array or list instead of separate variables.
Dingram Dingram

2012/4/17

#
Excellent, thanks very much. You guys have been exceptionally helpful!
Dingram Dingram

2012/4/17

#
Sorry, just one more question, where would I then store the variables which store the health and the alive/position status? Would it be a case of using enemy1.health(); or something similar to that?
davmac davmac

2012/4/17

#
Would it be a case of using enemy1.health(); or something similar to that?
That would probably be best, yes. You should store state (health etc) pertaining to an object within the object.
Dingram Dingram

2012/4/23

#
But would that then keep the variable throughout the game, or would it reset every time you enter/exit the room?
danpost danpost

2012/4/23

#
As long as the variables are kept within the object, passing that object from room to room will not change the state or values that object contains (a mistake many people make is trying to create a new object for the new world, which would, in essence, reset all the variables within it).
Dingram Dingram

2012/4/23

#
I'm not passing the object from room to room, I'm entering the room and objects are already there. The variables seem to reset every time the character enters the room, so the enemies don't stay dead...
davmac davmac

2012/4/23

#
Where in your code do you initialize the enemies? Does that code get executed when you transition between the rooms? If so, that is the problem. Eg. if you construct a new world every time you transition back to the room, and the enemies are initialized in that world's constructor, this would cause the problem you see.
Dingram Dingram

2012/4/26

#
To change the world/rooms I am using the "Greenfoot.setWorld(new room());" command, is this what you mean by creating a new world?
davmac davmac

2012/4/26

#
Yes; "new room()" creates a new world, though it bothers me that you've called it "room" and not "Room". Java class names should start with a capital... As you're creating a new room each time, and presumably the constructor re-initializes the enemies, the enemies will re-appear. To fix this, you have at least three options: 1) Add a new constructor to the room class, which allows passing in the enemies. Then, keep track of the enemies for the room even when you switch worlds. You probably will need to pass the information to each new world. 2) Do not create a new room each time, but instead re-use the existing one. You may find this problematic as each world holds a background image which consumes a sizable chunk of memory. You'd probably want to create a data structure (a class) to hold each of the worlds in your game, and share this between all the worlds; alternatively, each world can have a reference to each of the worlds in each of the directions that it is possible to travel in from that world. 3) Store the information in 'static' variables (private static Enemy enemy1). The variables will be then be shared between all instances, so the information won't be lost when you create a new room. However, you'll need to move initialization out of the constructor and into a static initializer block; also, be aware that the "reset" function provided by Greenfoot can't reset static data.
You need to login to post a reply.