PDA

View Full Version : Predictive target shooting...


zuperxtreme
02-26-2009, 07:13 PM
Hey guys, I'm just trying to make a tower defense test. Just to see if I can.

I've ran into a little problem:

1) The shooting is off. It shoots a tad too behind. I'm guessing I have to calculate the vector of the "enemy" and accommodate for the shot. I just don't know how.

2) The bullets aren't erasing correctly. They erase, but I get an error traced(Error #2025)

3) The bullets aren't rotated correctly.

If anyone needs the whole file or fla let me know.

Cheeers.

Here's most of it:

stage.addEventListener(Event.ENTER_FRAME,function( ){
shot_txt.text = String(shotCount)

//move mc to mouse position
mousePointer.x = mouseX
mousePointer.y = mouseY
//--
coord_txt.x = mouseX+10
coord_txt.y = mouseY
coord_txt.text = "x: "+mouseX+"/y: "+mouseY
//if closer than radius of tower, set the towers rotation to aim at enemy
//if(enemyArray.length){
for(var i:int = 0; i < enemyArray.length; i++){
if(getDistance(enemyArray[i], Tower) < Tower.radius){
Tower.rotation = ((Math.atan2(enemyArray[i].y-Tower.y, enemyArray[i].x-Tower.x))/Math.PI)*180
shootEnemy()



}


}
})

//-----------SHOOT AT ENEMIES------------

function shootEnemy(){
if (getTimer() - t > 500){
var newBullet:bullet_1 = new bullet_1
shotCount++
newBullet.x = Tower.x
newBullet.y = Tower.y
newBullet.rotation = Tower.rotation
addChild(newBullet)

newBullet.addEventListener(Event.ENTER_FRAME,moveB ullet)
t = getTimer();
}

}
function moveBullet(e:Event){

e.target.dirx = Math.cos(e.target.rotation*Math.PI/180);
e.target.diry = Math.sin(e.target.rotation*Math.PI/180);
e.target.x += e.target.dirx*e.target.bulletSpeed
e.target.y += e.target.diry*e.target.bulletSpeed

if(e.target.x<50 || e.target.x> 500 || e.target.y<50 || e.target.y> 350){

removeChild(MovieClip(e.target)
)

}
}

rrh
02-27-2009, 06:51 PM
1) Get distance from tower to creep. Divide by speed of bullet. Multiply by speed of creep to find how far ahead of the creep the tower should be aiming.

There's a more accurate way, but there's a little more math to it. Basically you would need to get the current speed of the creep towards or away from the tower, and then solve for time:
time = CreepStartingDistance / (BulletSpeed - CreepSpeed)

2) Your erasing error is probably the fault of not removing the eventListener.

zuperxtreme
02-27-2009, 10:44 PM
Agh! Thanks a ton man. I was starting to lose hope with this thread. I'll try it out and report back. :)