The Armor Games website will be down for maintenance on Monday 10/7/2024
starting at 10:00 AM Pacific time. We apologize for the inconvenience.

ForumsProgramming ForumDifferent Types of Enemies

12 6088
SheWhoMustNotBeNamed
offline
SheWhoMustNotBeNamed
185 posts
Nomad

If this question has been answered anywhere else in the forums, please just link me to it.

I am an almost complete beginner to AS3. In attempt to learn it, I've been following a tutorial to making an Avoider Game. Here's the link: http://gamedev.michaeljameswilliams.com/2008/09/17/avoider-game-tutorial-1/

In my game, I would like to have different types of enemies on the screen. Right now though, it only shows me how to have one enemy over and over again. This doesn't have to be a perfect solution, as all I need is for it to be done by tomorrow. I'll paste my code below that I think has to do with the enemy, but just let me know if you need more information.

AvoiderGame.as
package
{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class AvoiderGame extends MovieClip
{
public var army:Array;
public var avatar:Avatar;
public var gameTimer:Timer;
public function AvoiderGame()
{
army = new Array();
var newEnemy = new Enemy(100,30);
army.push (newEnemy);
addChild( newEnemy );

avatar = new Avatar();
addChild( avatar );
avatar.x = mouseX
avatar.y = mouseY

gameTimer = new Timer( 25 );
gameTimer.addEventListener(TimerEvent.TIMER, onTick);
gameTimer.start();
}
public function onTick( timerEvent:TimerEvent ) :void
{
if (Math.random()<0.1)
{
var randomX:Number = Math.random() * 400
var newEnemy = new Enemy(randomX,-15);
army.push (newEnemy);
addChild( newEnemy );
}


avatar.x = mouseX;
avatar.y = mouseY;

for each (var enemy:Enemy in army )
{
enemy.moveDownABit();
if ( PixelPerfectCollisionDetection.isColliding( avatar, enemy, this, true ) )
{
gameTimer.stop();
dispatchEvent(new AvatarEvent( AvatarEvent.DEAD));
}

}
}
}
}


Enemy.as
package
{
import flash.display.MovieClip;
public class Enemy extends MovieClip
{
public function Enemy(startX:Number, startY:Number)
{
x = startX;
y = startY;
}

public function moveDownABit() :void
{
y = y + 3;
}
}

}



Any help you can give would be awesome.

  • 12 Replies
master565
offline
master565
4,104 posts
Nomad

I don't understand what the problem is. Can you elaborate.

gaboloth
offline
gaboloth
1,612 posts
Peasant

Well, you just have to use the same code you used before. create the class for the new enemy, give it different stats (ex. y = y + 6 in movedownabit for a faster enemy) and use the same code that you used for the other enemy for attaching it and calling the movement and collision functions.

SheWhoMustNotBeNamed
offline
SheWhoMustNotBeNamed
185 posts
Nomad

Would I have to change anything with the army part in the onTick function? Would I just change the math.random to a lower number and use it for each? Thanks again.

master565
offline
master565
4,104 posts
Nomad

I'm still not sure what you want, but if you want to make different enemies with similar properties, make a new action script documents and have in it

public class (enemy name) extends Enemy

SheWhoMustNotBeNamed
offline
SheWhoMustNotBeNamed
185 posts
Nomad

and then just follow the same code as before? There is a part in the documentClass code when I create a new array: army. Do I have to change any of this? Is it possible to have the Enemy have multiple appearances that change randomly as they appear?

My goal is just to have 4 or 5 different looking enemies that do the exactly same thing.

master565
offline
master565
4,104 posts
Nomad

In the "AvoiderGame.as" you could try something like this


var greenEnemy:Enemy = new greenEnemyMC()
var redEnemy:Enemy = new redEnemyMC()
var enemySelector:Number = Math.ceil(Math.random()*2) + 0;


Note: greenEnemyMC is the name of the movieclip in your library that you are exporting

Then when you add the enemy, use this in the function


if(enemySelector == 1)
{
addChild(greenEnemy)
}
if(enemySelector == 2)
{
addChild(redEnemy)
}


That should create 2 different enemies with the exact same properties.
SheWhoMustNotBeNamed
offline
SheWhoMustNotBeNamed
185 posts
Nomad

I'm sorry, I'm really new at this, and am struggling to understand this. What parts of the code would I have to get rid of, where would I put the new things in, and what would I have to create?

Would I delete this part

army = new Array();
var newEnemy = new Enemy(100,30);
army.push (newEnemy);
addChild( newEnemy );

and add the first part you said?

Thanks for everything, again.
SheWhoMustNotBeNamed
offline
SheWhoMustNotBeNamed
185 posts
Nomad

This might be completely ridiculous, but could I have one enemy that has multiple frames, each a different appearance, then have them be randomly chosen to be shown? I have now idea if this is even possible, but if it is, how would I go about it? I apologize for my ignorance.

master565
offline
master565
4,104 posts
Nomad

This might be completely ridiculous, but could I have one enemy that has multiple frames, each a different appearance, then have them be randomly chosen to be shown? I have now idea if this is even possible, but if it is, how would I go about it? I apologize for my ignorance.


I'm not sure you can access timelines of individual movieclips.
SheWhoMustNotBeNamed
offline
SheWhoMustNotBeNamed
185 posts
Nomad

Alright, really sorry for triple posting, but I thought if you could see my game you might understand better.
Here's the link: http://www.truploader.com/view/102294
Basically I want the faces to be of multiple people, but have it still be the same amount.

master565
offline
master565
4,104 posts
Nomad

Here's the link: http://www.truploader.com/view/102294


It gives me an error.

Would I delete this part


I think so, try it out.
SheWhoMustNotBeNamed
offline
SheWhoMustNotBeNamed
185 posts
Nomad

Sorry about the link, here it is, again: http://www.truploader.com/view/102294

Showing 1-12 of 12