2012-02-01 156 views
1

我一直有一個問題,我一直在努力多小時,現在沒有運氣。我有移動綁定到箭頭鍵,用戶通過鼠標環顧四周。當我開槍時,我希望它繼續沿着它最初開始的方向繼續前進,但是如果用戶移動鼠標,子彈就會隨着它改變方向。鼠標移動列表中的子彈

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; 
    } 
} 

這是我在開始時使用「播放器」來跟隨光標的代碼。

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; 
} 

我認爲是什麼原因造成的問題是,當我把子彈放入數組他們都共享用戶的影響,移動鼠標,而不是保持其初始值。如果有人對此有任何意見,這將是非常appriciated和長期職位的遺憾。

感謝, 科爾

回答

1

嘗試代碼:像這樣的前

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; 
    Create.currentPlayerRotation = player_obj.rotation; 
    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.x +=Math.cos((target.currentPlayerRotation)*degreesToRadians) *10; 
     target.y +=Math.sin((target.currentPlayerRotation)*degreesToRadians) *10; 
    } 
} 
+0

感謝隊友,這工作完全歡呼。 – 2012-02-01 20:22:01

1

我有情況。我能找到的最佳解決方案是向彈丸類添加兩個屬性來存儲初始角度的正弦和餘弦。如果拋射物的速度永不改變,你可以很好地保存拋射體的deltaX和deltaY。你不需要Bullets的「currentPlayerRotation」了。

您的拍攝功能應該是這樣的:

function shoot(shooteven:Event) 
{ 
    var Create:Bullet = new Bullet(); 
    addChild(Create); 
    Create.x = player_obj.x; 
    Create.y = player_obj.y; 
    Create.dirCos = Math.cos((player_obj.rotation)*degreesToRadians); 
    Create.dirSin = Math.sin((player_obj.rotation)*degreesToRadians); 
    list.push(Create); 
} 

應該使子彈過於簡化,現在沒有罪(循環)/ COS()調用在屏幕上每一個彈丸。

for (var i:int=0; i<list.length; i++) 
{ 
    target = Bullet(list[i]); 
    target.x += target.dirCos * 10; 
    target.y += target.dirSin * 10; 
} 

我希望這可以幫助你。