PDA

View Full Version : [AS3] Bullet Shooter Problem


thesock338
09-30-2009, 04:30 PM
So I made some Actionscript that shoots a bullet when I press the Spacebar, and you aim with the mouse...
Frame 1:
var gun:MovieClip = new Gun();
gun.x = stage.stageWidth/2;
gun.y = stage.stageHeight/2;
addChild(gun);

function point(event:Event){
var dx:Number = mouseX - gun.x;
var dy:Number = mouseY - gun.y;
var radians:Number = Math.atan2(dy, dx);
gun.rotation = radians * 180/Math.PI;
}
addEventListener(Event.ENTER_FRAME, point);


Gun:
stop();

function addbullet(event:MouseEvent):void {
var bullet:MovieClip = new Bullet();
var vx:Number = 5;
var ax:Number = 1;
bullet.x = 84;
bullet.y = 0;
addChild(bullet);
function movebullet(event:Event) {
vx += ax;
bullet.x += vx;
}
addEventListener(Event.ENTER_FRAME, movebullet);
}
stage.addEventListener(MouseEvent.MOUSE_DOWN, addbullet);

However, if I shoot, then move my mouse, the bullets follow the mouse. How would I fix this?

TomMalufe
09-30-2009, 07:56 PM
make sure the bullets are the child of the stage and not the gun (which is being rotated to follow the mouse).

stage.addChild(bullet);

You will have to rethink your math though because the bullet is no longer going along the x-axis alone.