Qaz_
17 posts
|
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:baserange, 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*180-90;
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._x-this.width/2, this.y-this.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.y-this.height/2, true) or root.cant_build.hitTest(this.x-this._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.x-tur._x;
oppside = -1*(minion.y-tur.y);
angle = Math.atan2(oppside, adjside);
angle = Math.round(angle/Math.PI*180);
tur.rotation = -1*(angle);
};
};
}
could anyone help?
|
feartehstickman
526 posts
|
“tur.onEnterFrame = function() {
adjside = minion.x-tur._x;
oppside = -1*(minion.y-tur.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?
|
Qaz_
17 posts
|
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
|
pl0xz0rz
33 posts
|
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 < cameraMaxY-enemies[i].y) {
bestTarget = i;
bestTargetValue = cameraMaxY-enemies[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
|
Qaz_
17 posts
|
ok thank you ill see if it works. And just so you know this is not a TD game.
|