我制作一个自上而下的射击游戏,到目前为止,我的角色可以移动,并且可以在点击鼠标时进行拍摄。但它只会向右射击而不是射向光标的位置。我该如何解决这个问题?
这里是主要课程中的项目符号代码:
public var bulletList:Array = [];
stage.addEventListener(MouseEvent.CLICK, shootBullet, false, 0, true);
stage.addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
public function shootBullet(e:MouseEvent):void
{
var bullet:Bullet = new Bullet(stage, harold.x, harold.y, harold.rotation);
bullet.addEventListener(Event.REMOVED_FROM_STAGE, bulletRemoved, false, 0, true);
bulletList.push(bullet);
stage.addChild(bullet);
}
public function loop(e:Event):void
{
if(bulletList.length > 0)
{
for(var i:int = bulletList.length-1; i >= 0; i--)
{
bulletList[i].loop();
}
}
}
public function bulletRemoved(e:Event):void
{
e.currentTarget.removeEventListener(Event.REMOVED_FROM_STAGE, bulletRemoved);
bulletList.splice(bulletList.indexOf(e.currentTarget),1);
}
以下是我的Bullet类中的代码:
package
{
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;
public class Bullet extends MovieClip
{
private var stageRef:Stage;
private var speed:Number = 10;
private var xVel:Number = 0;
private var yVel:Number = 0;
private var rotationInRadians = 0;
public function Bullet(stageRef:Stage, X:int, Y:int, rotationInDegrees:Number):void
{
this.stageRef = stageRef;
this.x = X;
this.y = Y;
}
public function loop():void
{
xVel = Math.cos(rotationInRadians) * speed;
yVel = Math.sin(rotationInRadians) * speed;
x += xVel;
y += yVel;
if(x > stageRef.stageWidth || x < 0 || y > stageRef.stageHeight || y < 0)
{
this.parent.removeChild(this);
}
}
}
}
答案 0 :(得分:0)
我想你忘记在构造函数中指定rotationInRadians
:
this.rotationInRadians = rotationInDegrees * (Math.PI / 180);
顺便说一句,除非你希望子弹能够在飞行过程中改变方向,否则你不需要计算xVel
和yVel
每个loop()
,你可以计算它在构造函数中,存储它,然后更新每个x
y
和loop()
。
答案 1 :(得分:0)
一些想法:
this.rotationInRadians = rotationInDegrees * (Math.PI / 180);
private var rotationInRadians:Number;
我认为这就是它。