无法弄清楚我的代码有什么问题。请帮忙。
错误:
场景 1,图层“动作”,第 3 帧,第 120 行 1180:调用可能未定义的方法 rad2deg。场景 1,图层“动作”,第 3 帧,第 121 行 1180:调用可能未定义的方法 rad2deg。场景 1,图层“动作”,第 3 帧,第 129 行 1180:调用可能未定义的方法 rad2deg。
代码:
import flash.utils.Timer;
import flash.events.TimerEvent;
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var missile:Array = new Array();
var timer: Timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER, addMissile);
timer.start();
plane.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey_2);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed_2);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed_2);
stage.addEventListener(Event.ENTER_FRAME, moveObjects);
function fl_MoveInDirectionOfKey_2(event:Event)
{
if (upPressed)
{
plane.y -= 8;
}
if (downPressed)
{
plane.y += 8;
}
if (leftPressed)
{
plane.x -= 8;
}
if (rightPressed)
{
plane.x += 8;
}
}
function fl_SetKeyPressed_2(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = true;
break;
}
case Keyboard.DOWN:
{
downPressed = true;
break;
}
case Keyboard.LEFT:
{
leftPressed = true;
break;
}
case Keyboard.RIGHT:
{
rightPressed = true;
break;
}
}
}
function fl_UnsetKeyPressed_2(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = false;
break;
}
case Keyboard.DOWN:
{
downPressed = false;
break;
}
case Keyboard.LEFT:
{
leftPressed = false;
break;
}
case Keyboard.RIGHT:
{
rightPressed = false;
break;
}
}
}
function addMissile(evt:TimerEvent):void {
var missile:Missile = new Missile();
var side:Number = Math.ceil(Math.random() * 4);
if (side == 1) {
missile.x = Math.random() * stage.stageWidth;
missile.y = - missile.height;
} else if (side == 2) {
missile.x = stage.stageWidth + missile.width;
missile.y = Math.random() * stage.stageHeight;
} else if (side == 3) {
missile.x = Math.random() * stage.stageWidth;
missile.y = stage.stageHeight + missile.height;
} else if (side == 4) {
missile.x = - missile.width
missile.y = Math.random() * stage.stageHeight;
}
missile.angle = getAngle(missile.x, missile.y, plane.x, plane.y);
missile.speed = Math.ceil(Math.random() * 15);
addChild(missile);
missile.push(missile);
}
function moveObjects(evt:Event):void {
moveMissile();
}
function moveMissile():void {
for (var i:int = 0; i < missile.length; i++) {
var dx = Math.cos(rad2deg(missile[i].angle)) * missile[i].speed;
var dy = Math.sin(rad2deg(missile[i].angle)) * missile[i].speed;
missile[i].x += dx;
missile[i].y += dy;
}
}
function getAngle(x1:Number, y1:Number, x2:Number, y2:Number):Number {
var radians:Number = Math.atan2(y2 - y1, x2 - x1);
return rad2deg(radians);
}