Actionscript 3 如何计算Math.PI/180一次并存储在变量中?

Actionscript 3 如何计算Math.PI/180一次并存储在变量中?,actionscript-3,flash,Actionscript 3,Flash,如何在代码中计算Math.PI/180,并将计算出的数字存储在变量中,以便游戏循环不会多次重新计算?我是actionscript的新手,我在游戏循环和计算过程中遇到了困难,所以如果你能给我看一下这样做的代码吗 function moveEnemies():void { var tempEnemy:MovieClip; for (var i:int =enemies.length-1; i>=0; i--) { tempEnemy = enemie

如何在代码中计算Math.PI/180,并将计算出的数字存储在变量中,以便游戏循环不会多次重新计算?我是actionscript的新手,我在游戏循环和计算过程中遇到了困难,所以如果你能给我看一下这样做的代码吗

function moveEnemies():void
{
    var tempEnemy:MovieClip;


    for (var i:int =enemies.length-1; i>=0; i--)
    {
        tempEnemy = enemies[i];
        if (tempEnemy.dead)
        {
            score++;
            score++;
            roachLevel.score_txt.text = String(score);
            enemies.splice(i,1);

        }
        else
        {

            tempEnemy.rotation += (Math.round(Math.random()*.4));
            tempEnemy.y +=  (Math.cos((Math.PI/180)*tempEnemy.rotation))*tempEnemy.speed;
            if (tempEnemy.x < 10)
            {
                tempEnemy.x = 14;
            }
            if (tempEnemy.x > stage.stageWidth - offset)
            {
                tempEnemy.x = stage.stageWidth - offset;
            }
            if (tempEnemy.y > stage.stageHeight)
            {
                removeEnemy(i);

                lives--;
                roachLevel.lives_txt.text = String(lives);
            }
        }
    }
}
function():void
{
敌人:电影唇;
对于(变量i:int=0.length-1;i>=0;i--)
{
敌人=敌人[我];
如果(敌人死了)
{
分数++;
分数++;
roachLevel.score_txt.text=字符串(分数);
3.拼接(i,1);
}
其他的
{
tempTowner.rotation+=(Math.round(Math.random()*.4));
tempfound.y+=(数学cos((数学PI/180)*tempfound.rotation))*tempfound.speed;
如果(温度x<10)
{
temp.x=14;
}
如果(tempDeffire.x>stage.stageWidth-偏移)
{
tempEngly.x=stage.stageWidth-偏移量;
}
if(temp.y>stage.stageHeight)
{
removeEnemy(一);
生命--;
roachLevel.lives_txt.text=字符串(lives);
}
}
}
}

在本文中,我只是按照您的要求将Math.PI/180的计算结果存储到一个变量中,并在代码的其他部分重复使用它

function moveEnemies():void
{
    var tempEnemy:MovieClip;
    var halfPI:Number = Math.PI / 180;

    for (var i:int =enemies.length-1; i>=0; i--)
    {
        tempEnemy = enemies[i];
        if (tempEnemy.dead)
        {
            score++;
            score++;
            roachLevel.score_txt.text = String(score);
            enemies.splice(i,1);

        }
        else
        {

            tempEnemy.rotation += (Math.round(Math.random()*.4));
            tempEnemy.y +=  (Math.cos(halfPI*tempEnemy.rotation))*tempEnemy.speed;
            if (tempEnemy.x < 10)
            {
                tempEnemy.x = 14;
            }
            if (tempEnemy.x > stage.stageWidth - offset)
            {
                tempEnemy.x = stage.stageWidth - offset;
            }
            if (tempEnemy.y > stage.stageHeight)
            {
                removeEnemy(i);

                lives--;
                roachLevel.lives_txt.text = String(lives);
            }
        }
    }
}
function():void
{
敌人:电影唇;
var halfPI:Number=Math.PI/180;
对于(变量i:int=0.length-1;i>=0;i--)
{
敌人=敌人[我];
如果(敌人死了)
{
分数++;
分数++;
roachLevel.score_txt.text=字符串(分数);
3.拼接(i,1);
}
其他的
{
tempTowner.rotation+=(Math.round(Math.random()*.4));
tempdefour.y+=(数学cos(halfPI*tempdefour.rotation))*tempdefour.speed;
如果(温度x<10)
{
temp.x=14;
}
如果(tempDeffire.x>stage.stageWidth-偏移)
{
tempEngly.x=stage.stageWidth-偏移量;
}
if(temp.y>stage.stageHeight)
{
removeEnemy(一);
生命--;
roachLevel.lives_txt.text=字符串(lives);
}
}
}
}

由于您不打算在运行时更改此值,因此最好将其设置为常量:

public static const PI_OVER_180:number=Math.PI/180


如果您在函数中计算这个值,那么每次调用函数时都会重新计算它,而浮点运算的代价很高。如果你知道你经常需要一个数字,而且它不会改变,那就把它设为常数。

这一点很好!重要的是要注意,只有在他为自己的代码实现了类的情况下,这才有效。如果他没有,这个变量仍然可以移到函数之外以提高性能。