Jump to content
  • Advertisement
Sign in to follow this  
F-117

creating Enemies

This topic is 2044 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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     

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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. 
 
 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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();
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!