creating Enemies

Started by
4 comments, last by Aldacron 10 years, 11 months ago

I have gone through many tutorials and books for beginner on java game development. It teaches about creating character, environment, collision detection and ai.

But I cann't find chapters on creating enemies that keeps on appearing on screen (like cars appearing on track one after the other) where can I find tutorials for this, or any certain book with this particular chapter

Advertisement

You're going to want to create an Enemy class that has all the properties that an enemy can have. For cars you'd probably want properties like maxSpeed, acceleration, direction, etc.

Then you need to decide how the game engine will add them to the game world. I would create a function called createEnemy (or whatever you like) which simply spawns enemies. Then you call the function createEnemy whenever you want to add a new enemy to the game. Maybe this is on a timer or maybe it's dependent on the conditions of the game. That part is up to you.

You'll probably want to save all the enemies in an array so that you can do things to them all at the same time. So every time an enemy is created you at it to the enemies array, and then you loop through that array every time the game engine updates. Each update the enemy cars will move, turn, be destroyed, etc.

You're going to want to create an Enemy class that has all the properties that an enemy can have. For cars you'd probably want properties like maxSpeed, acceleration, direction, etc.

Then you need to decide how the game engine will add them to the game world. I would create a function called createEnemy (or whatever you like) which simply spawns enemies. Then you call the function createEnemy whenever you want to add a new enemy to the game. Maybe this is on a timer or maybe it's dependent on the conditions of the game. That part is up to you.

You'll probably want to save all the enemies in an array so that you can do things to them all at the same time. So every time an enemy is created you at it to the enemies array, and then you loop through that array every time the game engine updates. Each update the enemy cars will move, turn, be destroyed, etc.

I have created an Enemy superclass in which I have created Enemy1 subclass.

Enemy superclass stores all the parameters(health-max & current, coordinates).

Enemy1 subclass only stores variables about coordinates

package palt;
public class Enemy1 extends Enemy {
public Enemy1(int centerX, int centerY) {
setCenterX(centerX);
setCenterY(centerY);
}
}
After this I place Enemy1 on screen via mainclass
em = new Enemy1(300, 300); // for coordinate
g.drawImage(Enemy1, em.getCenterX() - 50, em.getCenterY() - 50, this); // to draw enemy1
I can print more then one enemy using above instruction again & again with different variables.
But, what i want is always have 3 enemies on screen. I could use an if loop stating
If (em < 3){em++;}
but with which varaiable do i do this and I beleive I should be doing it in main class probably to update method.

First of all, there's no reason to subclass Enemy just to add coordinates. You can store those in the Enemy class just fine.

A simple solution to your problem would be to use an ArrayList. You can add new enemies to the list when they are spawned, and choose not to spawn if the list contains more than three enemies. Then in your render loop, you can iterate the enemies in the list and draw them.

First of all, there's no reason to subclass Enemy just to add coordinates. You can store those in the Enemy class just fine.

A simple solution to your problem would be to use an ArrayList. You can add new enemies to the list when they are spawned, and choose not to spawn if the list contains more than three enemies. Then in your render loop, you can iterate the enemies in the list and draw them.

The reason behind using subclass is to create more then one kind of enemy later.

and could you give an eg about using an array in this case

The reason behind using subclass is to create more then one kind of enemy later.

That's fine. But the coordinates can still be in the base class.

and could you give an eg about using an array in this case

Here's how to do it with an ArrayList. This is just a quick example of the operations you need. You'll have to adapt it to however your code base is structured.


// Create an ArrayList somewhere
ArrayList<Enemy> list = new ArrayList<Enemy>();

// Add new enemies to it. Assume MAX_ENEMIES is 3 in this case
if(list.size() < MAX_ENEMIES)
    list.add(new Enemy(300, 300));

// Draw the enemies in your render loop.
for(Enemy e : list) {
    e.draw();
}

This topic is closed to new replies.

Advertisement