I’ve been having a problem I’ve been working on for multiple hours now with no luck. I have movement bound to arrow keys and the user looks around via the mouse. When I shoot I want it to continue off in the direction it initially started, but what happens is if the user moves the mouse the bullets change directions with it.
var myTimer2:Timer = new Timer(50);
var list:Array = new Array();
myTimer2.addEventListener(TimerEvent.TIMER, timerListener2);
myTimer2.start();
stage.addEventListener(MouseEvent.CLICK, shoot);
function shoot(shooteven:Event)
{
var Create:Bullet = new Bullet();
addChild(Create);
Create.x = player_obj.x;
Create.y = player_obj.y;
list.push(Create);
}
function timerListener2(e:TimerEvent):void
{
moveBullets();
}
function moveBullets():void
{
var target:Bullet;
for (var i:int=0; i<list.length; i++)
{
target = Bullet(list[i]);
target.currentPlayerRotation = player_obj.rotation;
target.x +=Math.cos((target.currentPlayerRotation)*degreesToRadians) *10;
target.y +=Math.sin((target.currentPlayerRotation)*degreesToRadians) *10;
}
}
And this is the code that I use at the start for the “player” to follow the cursor.
stage.addEventListener("mouseMove", Follow);
function Follow(e:MouseEvent):void
{
var a1 = mouseY - player_obj.y;
var b1 = mouseX - player_obj.x;
var radians1 = Math.atan2(a1,b1);
var degrees1 = radians1 / (Math.PI / 180);
player_obj.rotation = degrees1;
}
I think what is causing the problem is that when I push the bullets into the array they’re all sharing the effects of the user moving the mouse instead of keeping their initial value. If anyone has any input on this it would be greatly appriciated and sorry for the long post.
Thanks,
Cole
Try that code: