我遇到了一个问题,我现在已经工作了好几个小时而没有运气。我将运动绑定到箭头键,用户通过鼠标环顾四周。当我开始拍摄时,我想让它继续朝最初开始的方向移动,但是如果用户移动鼠标,子弹会随之改变方向。
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;
}
我认为造成这个问题的原因是,当我将子弹推入阵列时,他们都会分享用户移动鼠标的效果而不是保持其初始值。如果有人对此有任何意见,那么对于这篇长篇文章来说,这将是非常有用的。抱歉。
谢谢, 科尔
答案 0 :(得分: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;
}
}
答案 1 :(得分:1)
我之前有这样的情况。我能找到的最好的解决方案是在射弹类中添加两个属性来存储初始角度的正弦和余弦。如果射弹的速度永远不会改变,你可以很好地保存射弹的deltaX和deltaY。你不再需要子弹的“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);
}
移动子弹的循环也被简化了,因为屏幕上的每个射弹都没有sin()/ cos()调用。
for (var i:int=0; i<list.length; i++)
{
target = Bullet(list[i]);
target.x += target.dirCos * 10;
target.y += target.dirSin * 10;
}
我希望这可以帮助你。