I’m not even close to finished on this turret script, but i
t works. The trouble I’m having is this script only takes effect to only 1, turret, homing turret,laser turret, and string turret. This script is going on my website as a turret tut. But the trouble comes as it can only effect 1 enemy, and 4 different turrets. I tried to write a for looping script, and setting it as a var, IE: for (k=0;k<100;k++){ target1 = _root[“obj1”+k] }
This is the frame script (Don’t comment on its messy writing …)
money = 0;
fire = 0;
_root.enemy = false;
//Points to attach at
Tx = _root.obj1._x;
Ty = _root.obj1._y;
fire2 = 0;
Ys = _root.player2._x;
Yy = _root.player2._y;
fire3 = 0;
Zs = this.player3.Bp1._x;
Zy = this.player3.Bp1._y;
fire4 = 0;
Js = _root.player4._x;
Jy = _root.player4._y;
speed = 0;
xspeed = 0;
yspeed = 0;
speed = 4;
handling = 90;
allowshoot1 = false;
allowshoot2 = false;
allowshoot3 = false;
allowshoot4 = false;
var speed:Number = 4;
var dx:Number;
var dy:Number;
var distance:Number;
var numMiss:Number = 10;
var turning = 12;
onEnterFrame = function () {
//Firing Turret
//Created by zachary
//I worked hard on these, So...Credit would be nice....
distance = 150;
rx = _root.obj1._x;
ry = _root.obj1._y;
tx = _root.target1._x;
ty = _root.target1._y;
if (Math.sqrt((rx-tx)*(rx-tx)+(ry-ty)*(ry-ty))<distance) {
allowshoot1 = true;
} else {
allowshoot1 = false;
}
rx = _root.target1._x;
ry = _root.target1._y;
cx = _root.obj1._x;
cy = _root.obj1._y;
angle = 5+(180*Math.atan2(ry-cy, rx-cx)/Math.PI);
if (allowshoot1 == true) {
_root.obj1._rotation = angle;
}
//Angle System and shoot system (:
fire++;
if (allowshoot1 == true) {
if (fire>=obj1.speed) {
//Thanks kirupa, for the dynamic sound tut..:D
kirupaSound = new Sound(this);
kirupaSound.attachSound("Boom1");
kirupaSound.start(0, 1);
//Edn of Sound attachment
this.obj1.player.play();
bullet1 = attachMovie("bullet", "bullet_"+_root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x:Tx, _y:Ty});
fire = 0;
bullet1._rotation = angle;
bullet1.dirx = Math.cos(angle*Math.PI/180)*200;
bullet1.diry = Math.sin(angle*Math.PI/180)*200;
}
}
bullet1.onEnterFrame = function() {
this._x += this.dirx/50;
this._y += this.diry/50;
if (this.hitTest(_root.target1)) {
unloadMovie(this);
obj1.UpgradePts += obj1.earn;
_root.target1.Strength -= 25;
}
this._alpha -= 2;
if (this._alpha<=0) {
unloadMovie(this);
}
};
//Laser Turret
//Fires laser at enemy
//these are slower but give more damage
fire2 = 0;
ex = _root.target1._x;
ey = _root.target1._y;
dx = _root.player2._x;
dy = _root.player2._y;
angle2 = (170*Math.atan2(ey-dy, ex-dx)/Math.PI);
_root.player2._rotation = angle2;
fire1++;
if (fire1>=26) {
kirupaSound = new Sound(this);
kirupaSound.attachSound("Laser1");
kirupaSound.start(0, 1);
bullet2 = attachMovie("bullet2", "bullet_2"+_root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x:Ys, _y:Yy});
fire1 = 0;
bullet2._rotation = angle2;
bullet2.dirx = Math.cos(angle2*Math.PI/180)*200;
bullet2.diry = Math.sin(angle2*Math.PI/180)*200;
}
bullet2.onEnterFrame = function() {
this._x += this.dirx/50;
this._y += this.diry/50;
if (this.hitTest(_root.target1)) {
_root.target1.Strength -= 50;
unloadMovie(this);
}
};
//String Turret
//Draws Line from Our Dot to enemy
//these take very little damage from our enemy
// my personal favorite
distance = 100;
//Distance
kx = _root.target1._x;
ky = _root.target1._y;
px = _root.player3._x;
py = _root.player3._y;
angle3 = 5+(180*Math.atan2(ky-py, kx-px)/Math.PI);
if (allowshoot3 == true) {
_root.player3._rotation = angle3;
}
rx = _root.player3._x;
ry = _root.player3._y;
tx = _root.target3._x;
ty = _root.target3._y;
//Some stats..
if (Math.sqrt((rx-tx)*(rx-tx)+(ry-ty)*(ry-ty))<distance) {
allowshoot3 = true;
} else {
allowshoot3 = false;
}
if (allowshoot3 == true) {
createEmptyMovieClip("pen", 5);
pen.lineStyle(2, 0x009900);
pen.moveTo(_root.player3._x, _root.player3._y);
pen.lineTo(target1._x, target1._y);
} else {
unloadMovie("pen");
}
if (["pen"].hitTest(_root.target1)) {
_root.target1.Strength -= 12;
}
//...Homing Bullets!
//Thanks SL for the BASIC, script for functioning
//homing bullets!
distance = 150;
rx = _root.player4._x;
ry = _root.player4._y;
tx = _root.target1._x;
ty = _root.target1._y;
if (Math.sqrt((rx-tx)*(rx-tx)+(ry-ty)*(ry-ty))<distance) {
allowshoot4 = true;
} else {
allowshoot4 = false;
}
ox = _root.target1._x;
oy = _root.target1._y;
ux = _root.player4._x;
uy = _root.player4._y;
angle4 = 5+(180*Math.atan2(oy-uy, ox-ux)/Math.PI);
_root.player4._rotation = angle4;
fire++;
if (fire>=125) {
if (allowshoot4 == true) {
bullet4 = attachMovie("bullet5", "bullet_"+_root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x:Js, _y:Jy});
bullet4._rotation = (player4._rotation)+90;
fire = 0;
this.player4.anim.play();
this.player4.anim2.play();
bullet4.onEnterFrame = function() {
if (enemy == true) {
if (this.hitTest(_root.target1)) {
unloadMovie(this);
_root.target1.Strength -= 100;
}
dx = _root.target1._x-this._x;
dy = _root.target1._y-this._y;
angle = Math.round(Math.atan2(dy, dx)*180/Math.PI);
ro = this._rotation;
if ((angle-ro<90 && angle-ro>-90) || angle-ro>270 || angle-ro<-270) {
this._rotation += turning;
} else if (angle-ro<180 || angle-ro>-180) {
this._rotation -= turning;
}
distance = Math.sqrt((dx*dx)+(dy*dy));
xx = Math.sin(this._rotation*Math.PI/180)*8;
yy = Math.cos(this._rotation*Math.PI/180)*8;
this._x += xx;
this._y -= yy;
} else {
dx = player4._x-this._x;
dy = player4._y-this._y;
angle = Math.round(Math.atan2(dy, dx)*180/Math.PI);
ro = this._rotation;
if ((angle-ro<90 && angle-ro>-90) || angle-ro>270 || angle-ro<-270) {
this._rotation += turning;
} else if (angle-ro<180 || angle-ro>-180) {
this._rotation -= turning;
}
distance = Math.sqrt((dx*dx)+(dy*dy));
xx = Math.sin(this._rotation*Math.PI/180)*8;
yy = Math.cos(this._rotation*Math.PI/180)*8;
this._x += xx;
this._y -= yy;
}
};
}
}
};