
metadata
Hi, I’m trying to make a turret that shoots at an enemy. I’ve made it so far so that it rotates toward the enemy but i cant seem to make it shoot. i was going to use the same code off of a tower defense game my friend made but neither me or him can get it. Here is the code I’m using to put it on the stage and make it rotate:
function turret2() {
attachMovie(“range”,“range”,_root.getNextHighestDepth(),{\_width:base_range, _height:base\_range});
attachMovie(“tur”,“tur”,_ root.getNextHighestDepth());
angle = Math.atan2(dist\_y, dist\_x);
this._x = this.x+this.speed\*Math.cos(angle);
this.\_y = this.y+this.speed\*Math.sin(angle);
this._ rotation = angle/Math.PI\*18090;
tur.onEnterFrame = function() {
if (!placed) {
tur.\_x = _root.\_xmouse;
tur._ y = _root._ymouse;
_root.range.alpha = 100;
can\_be\_placed = true;
if (root.cant\_build.hitTest(this.\_xthis.width/2, this.ythis._ height/2, true) or _root.cant\_build.hitTest(this.x+this.\_width/2, this.y+this._height/2, true) or _root.cant\_build.hitTest(this.x+this.\_width/2, this.ythis._height/2, true) or _root.cant\_build.hitTest(this.xthis.\_width/2, this.y+this._height/2, true)) {
_root.range.alpha = 0;
can\_beplaced = false;
}
}
};
range.onEnterFrame = function() {
if (!placed) {
this._ x = _root.\_xmouse;
this._ y = _root.ymouse;
}
};
onMouseDown = function () {
if (can\_be\_placed) {
placed = true;
}
tur.onEnterFrame = function() {
adjside = minion.xtur.\_x;
oppside = 1\*(minion.ytur.y);
angle = Math.atan2(oppside, adjside);
angle = Math.round(angle/Math.PI\*180);
tur._ rotation = 1\*(angle);
};
};
}
could anyone help?


metadata
[Step 1](http://www.kongregate.com/forums/4gameprogramming/topics/39889properforumcodeformattingguide)


metadata
“tur.onEnterFrame = function() {
adjside = minion.xtur.\_x;
oppside = 1\*(minion.ytur.y);
angle = Math.atan2(oppside, adjside);
angle = Math.round(angle/Math.PI\*180);
tur.rotation = 1\*(angle);”
Why are you multiplying by 1 there? Is it something to do with AS2’s atan function?
And also, where is the actual line that tells the turret to shoot (make a bullet, shoot it)? Like tur.shoot(enemy) or something?


metadata
I multipled by 1 becase of the variable i have set for the angle… thats not the problem… i need to find how to make the turret shoot


metadata
This is the code for rotating the cannon and firing a bullet
`
//This attempts to shoot at the point by normalizing the vector and firing a bullet
function shoot(x,y){
if (this.reload > .99){
this.reload = 1;
x = this.x;
y = this.y;
var temp = 1 / Math.sqrt((x*x+y*y)/(this.cannon.x*this.cannon.x + this.cannon.y * this.cannon.y));
this.cannon.x = temp*x;
this.cannon.y = temp*y;
var l = new blackOrb(this.cannon.x + this.x,this.cannon.y + this.y,this.ammo.r,this.cannon.x*this.power,this.cannon.y*this.power,this.ammo.m);
stuff.push(l);
bullets.push(l);
}
}
`
But you also need this.
`
//This happens to each tower on each frame, this is a naive method, there are better methods with better performance
//but for a TD game where there aren't many creeps, this is close enough.
function aim(id){
var bestTarget = null;
var bestTargetValue = 0;
switch (towerPatterns[towers[id].pattern].lineOfSightType){ //Selects the enemy to attack
case 0:
for (var i=0;i<enemies.length;i++){
if ((towers[id].y  enemies[i].y) * (towers[id].y  enemies[i].y) + (towers[id].x  enemies[i].x) * (towers[id].x  enemies[i].x) < towerPatterns[towers[id].pattern].range * towerPatterns[towers[id].pattern].range) {
if (bestTargetValue < cameraMaxYenemies[i].y) {
bestTarget = i;
bestTargetValue = cameraMaxYenemies[i].y;
}
}
}
break;
}
if (bestTarget === null) {return null} else {
switch (towerPatterns[towers[id].pattern].velocityPerception){
case 0:
var dir = new vektor2d(0,0);
dir.x = enemies[bestTarget].x + (enemies[bestTarget].img.width / 2);
dir.y = enemies[bestTarget].y + (enemies[bestTarget].img.height / 2);
return dir;
break;
}
}
return null;
}
`
NINJA EDIT: Forgot to call the shoot() function
`
function aiTower(id){
var l = aim(id);
if (l !== null) towers[id].shoot(l.x,l.y);
};
`
Another ninja edit: Typo


metadata
ok thank you ill see if it works. And just so you know this is not a TD game.
