- Home
- Tutorials
- Flash
- Intermediate
- Simple Enemies
Simple Enemies

Unbroken code:
Andy Crockett
Loved writing code since I started a couple months ago, and I get pissed to see the lack of useful tutorials for widely used subjects on the web.
View all articles by Andy CrockettAnd for those of you that didn't read the Shooting bullets tutorial, here is the unbroken code:
var bullet_array:Array= new Array();
var bulletSpeed:Number = 5;
var xSpeed:Number = 0;
var ySpeed:Number = 0;
var reloadSpeed:Number = 100;//milliseconds
var bulletOffset:Number = 10;//pixels
var randomNum:Number = 0;
var life:Number = 0;
var totalLife:Number = 50;//frames
var reloaded:Boolean = true;
var x:Number = 0;
turret.onEnterFrame = function() {
if (Key.isDown(Key.LEFT)) {
this._rotation -= 5;
}</p>
<p> if (Key.isDown(Key.RIGHT)) {
this._rotation += 5;
}</p>
<p> if (Key.isDown(Key.SPACE)) {
fire();
}</p>
<p>};
function fire() {
if (reloaded) {
x = _root.getNextHighestDepth();
bullet_array.push("bullet"+x);
var bullet:MovieClip = _root.attachMovie("bullet", "bullet"+x, x);
bullet._x = turret._x;
bullet._y = turret._y;
randomNum = random(bulletOffset)-bulletOffset/2;
bullet._rotation = turret._rotation+randomNum;
bullet.xSpeed = Math.cos(Math.PI/180*bullet._rotation)*bulletSpeed;
bullet.ySpeed = Math.sin(Math.PI/180*bullet._rotation)*bulletSpeed;
bullet.life = 0;
bullet.onEnterFrame = function() {
this._x += this.xSpeed;
this._y += this.ySpeed;
this.life++;
if (this.life>totalLife) {
this.removeMovieClip();
this.unloadMovie();
}</p>
<p> };
reload();
}</p>
<p>}</p>
<p>function reload() {
reloaded = false;
timer = setInterval(this, "Reloaded", reloadSpeed);
}
function Reloaded() {
clearInterval(timer);
reloaded = true;
}
for (x=0; x<10; x++) {
attachEnemy();
}
function attachEnemy() {
x = _root.getNextHighestDepth();
var enemy:MovieClip = _root.attachMovie("enemy", "enemy"+x, x);
enemy._x = random(Stage.width);
enemy._y = random(Stage.height);
enemy.onEnterFrame = function() {
for (x in bullet_array) {
if (this.hitTest(_root[bullet_array[x]])) {
_root[bullet_array[x]].removeMovieClip();
this.removeMovieClip();
bullet_array.splice(x,1);
}
}
};
}
Spread The Word
Attachments
3 Responses to "Simple Enemies" 
|
said this on 20 Nov 2009 3:55:17 AM CST
this is aweome but hoe do
|
|
said this on 25 Nov 2010 7:14:51 PM CST
how do you get it to play
Also how w |


Author/Admin)
