1

无法弄清楚我的代码有什么问题。请帮忙。

错误:

场景 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);
}
4

2 回答 2

0

看起来你没有定义一个函数rad2deg()

在某处添加:

function rad2deg(radians:Number):Number
{
    return radians * 180 / Math.PI;
}

需要注意的重要一点是,在 Flash中唯一有用的地方是设置rotation.DisplayObject

例如,您的代码中的这一行将无法按预期工作 -Math.cos()等等需要弧度。

Math.cos(rad2deg(missile[i].angle))
于 2013-06-04T02:13:09.427 回答
0

我想我有答案。尝试将此添加到您的代码中:

deg2rad = Math.PI/180;
rad2deg = 180/Math.PI;

让我知道这个是否奏效。

于 2013-06-04T02:10:41.673 回答