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