PDA

View Full Version : [GameDesign?]Managing multiple enemies



flashmxboy
July 13th, 2009, 02:07 PM
Hi

I just have a little question about game design. I have one enemy movieClip and one class for that enemy. What would be the best way to copy this enemy. Making a lot of class instances?
Have enemies with instance names like "enemy1" "enemy2" etc. or will it work to just copy the enemy movieClip??

Thanks in advance. You guys here are all awsome!:D

therobot
July 13th, 2009, 02:50 PM
Hi

I just have a little question about game design. I have one enemy movieClip and one class for that enemy. What would be the best way to copy this enemy. Making a lot of class instances?
Have enemies with instance names like "enemy1" "enemy2" etc. or will it work to just copy the enemy movieClip??

Thanks in advance. You guys here are all awsome!:D

by copy i assume you mean using duplicateMovieClip in as2? Personally, i would create new instances, as it seems a little easier to manage, but I think duplicateMovieClip is actually faster than attachMovie (by a small, small margin). If you're using as3, you can't rely on duplicateMovieClip & attachMovie, however.

i like to create new instances of an object, and add them to an array of similar instances. It makes the most sense to group together things like items, bullets, and enemies in separate arrays.

Skribble
July 13th, 2009, 11:33 PM
You'll want to get in to some Arrays if you are getting in to classes, they will save your life and make handling large amounts of data really, really easy! An array is simply a collection of bits of information, for instance, Red, Blue, Green and Yellow could all be put in to a Colors array.

colorsArray:Array = new Array("Red", "Blue", "Green", "Yellow");

So, you can save the details of all of your enemies, and what their classes entail, in 1 big "multidimensional array"; this is lots of little arrays, inside a bigger array; think of it like putting all of your enemy classes in to 1 room, and you can just pull a copy out of that room whenever you like.


var enemyArray:Array = new Array(
new Array("C1", "Water", "Grass", 300, 50, "This is the Water Class enemy"),
new Array("C2", "Grass", "Fire", 300, 80, "This is the Grass Class enemy"),
new Array("C3", "Earth", "Water" 200, 50, "This is the Earth Class enemy")
);So in the first line, you are simply creating an array for all of your enemies and giving it a name, in this case, enemyArray. Then we make a NEW array for EACH of your enemies, and put it INSIDE your enemyArray, now it is a multidimensional array; an array, inside of another array.

I've just included some examples to show what you can do with this. I added in the Water, Grass and Earth classes, they are each given a Class Number, an elemental power, an elemental weakness, Health points, EXP when killed, and an explanation of that class for in-game purposes; you can contain any amount of data in an array, so don't feel limited by what I have here.

If you want to return the value of an entire class, you just use:


enemyArray[0]
//returns C1, Water, Grass, 300, 50, This is the Water Class enemy
enemyArray[1]
//returns C2, Grass, Fire, 300, 80, This is the Grass Class enemy
enemyArray[2]
//returns C3, Earth, Grass, 200, 50, This is the Earth Class enemy
The number represents the position of an element in an array, it starts at 0 for indexing purposes. This can be a little confusing at first, but you will get used to it soon enough.


If you want to return the value of 1 element in a certain class, you just use:


enemyArray[0][0]
//returns C1
enemyArray[1][0]
//returns C2
enemyArray[2][0]
//returns C3To make things easier you can make labels for your elements so you don't have to remember what number they are in your array.



waterClass = 0;
grassClass = 1;
earthClass = 2;
//
className = 0;
elementType = 1;
weaknessType = 2;
health = 3;
exp = 4;
description = 5;
//
var enemyArray:Array = new Array(
new Array("C1", "Water", "Grass", 300, 50, "This is the Water Class enemy"),
new Array("C2", "Grass", "Fire", 300, 80, "This is the Grass Class enemy"),
new Array("C3", "Earth", "Water" 200, 50, "This is the Earth Class enemy")
);
//
enemyArray[waterClass][weaknessType]
//returns Fire
To show you how this can be really handy, here is a script to attach an enemy, based on the multidimensional array we've made.


randomEnemy = random(2);
newEnemy = attachMovie(enemyArray[randomEnemy][className],"newEnemy"+getNextHighestDepth(), getNextHighestDepth++);
newEnemy.element = enemyArray[randomEnemy][elementType]
newEnemy.weakness = enemyArray[randomEnemy][weaknessType]
newEnemy.HP = enemyArray[randomEnemy][health]So here we can dynamically spawn random enemies with different classes with 1 block of code, instead of a different block for each enemy! I hope I explained it well enough, I kind of tried to condense everything so I hope you can still understand, heh.

Good luck!

Gnoll
July 14th, 2009, 12:14 AM
Any more information like language?

Do you know inheritance? eg Enemy1 and Enemy2 both extend AbstractEnemy, this will be vital in creating an enemy management system :)

Good luck,
Gnoll

p.s

Skribble: Did you know you can just use the literal [ ] rather than new Array() :D


var enemyArray:Array = [
["C1", "Water", "Grass", 300, 50, "This is the Water Class enemy"],
["C2", "Grass", "Fire", 300, 80, "This is the Grass Class enemy"],
["C3", "Earth", "Water" 200, 50, "This is the Earth Class enemy"]
];

Can make it easier to read :)

therobot
July 14th, 2009, 12:52 AM
Skip the multi-dimensional array look-ups if you can - they are slow.

If you have a bunch of enemy types to make, look into adopting some sort of factory pattern, which is really simple



// a simple factory
function makeEnemy(enemyType:int):Enemy
{

switch(enemyType)
{
case ENEMY_BUG:
return new Bug();
break;
case ENEMY_WOLF:
return new Wolf();
break;
}
}

SparK_BR
July 16th, 2009, 08:54 AM
calm down guys, i think he already knows that stuff

what he wants is something to spawn enemies in AS3 without using attachMovie

let's say you have your enemy class: Enemy which contains a MovieClip
so you must make new Instances of it and put them in the right place, just like you used to do with attachMovie but using "new"



//this is what gives the number in the end of the enemies end
var enemyCounter = 0;

//array for better control
var enemyList:Array = new Array();

function spawnEnemy():void {
//setting the name enemy0, enemy1, enemy2 and so on...
var this["enemy"+enemyCounter]:Enemy = new Enemy(); //not sure if you must use VAR or not here

//making a "shortcut" to your enemy called obj
var obj:Enemy = this["enemy"+enemyCounter];
obj.x = <where you want>;
obj.y = <where you want>;
//here you can set pretty much any property Enemy may have through obj

//adding obj to the array
enemyList.push(obj);

//must increase the enemyCounter to not override other enemies
enemyCounter++;
}

and don't forget to test it (cause i didn't XD)

flashmxboy
July 17th, 2009, 05:56 AM
Hi.
Thanks for all your replies:D I was looking for a fast and easy way to create many enemies who behaved exactly the same way. I have seen all your ways of doing it but i don't know if it will work for me. I have a enemy movie clip with a lot of animations in it.
And i have a enemy class that controls the enemy and its animations.
So i did it in a little different way. I created a function called "Run" in the enemy class and linked the enemy class to the enemy Movie Clip. (You know...export for action script and choose a class in the library)
Whit this done i can drag a bunch of enemy MovieClip`s from the library and name them "enemy1" "enemy2" etc. And start them with this code:
enemy1.Run();
enemy2.Run();

I dont think this is a good way to do it:P, what do you guys think??

Btw im using AS3 and Flash CS4:D

Gnoll
July 17th, 2009, 06:02 AM
I think this is getting closer to a good way to do it :)

Gnoll