AS3在场景中调用Math.random?

时间:2013-03-10 12:59:22

标签: actionscript-3 math random

所以,我正在尝试制作一个小行星随机移动的游戏,但是当我把我的Math.random函数放在我的函数之外时,它只给1个值1次,并且每次小行星= 0都不重复如果我把它放在我的功能中,它会不断更新并发送我的小行星随机飞来飞去。有帮助吗? 编辑:对不起,如果我不清楚,我已经整夜了。我希望让我的小行星在屏幕上的随机路径上移动,然后随机选择另一条路径并在该路径上移动,这应该在每次x> 1024和y> 768

时发生
import flash.events.Event;
addEventListener(Event.ENTER_FRAME, massGain);
stop();

var math1:Number = Math.floor(Math.random()*20-10);
trace(math1);

function massGain(e:Event)
{


Mouse.hide();

Cosmo.x = mouseX;
Cosmo.y = mouseY;

Asteroid5.x = Asteroid5.x + math1;
Asteroid5.y = Asteroid5.y + math1;
if(Asteroid5.x >1024){
Asteroid5.x = 0;
}
if(Asteroid5.y > 768){
Asteroid5.y = 0;
}



}

2 个答案:

答案 0 :(得分:0)

使用它!

import flash.events.Event;
addEventListener(Event.ENTER_FRAME, massGain);
stop();

// random value to start of with
var math1:Number = Math.floor(Math.random()*10);//20-10 doesnt make sense .. its 10

function massGain(e:Event)
{
        Mouse.hide();

        Cosmo.x = mouseX;
        Cosmo.y = mouseY;


        if(Asteroid5.x >1024){
        Asteroid5.x = 0;
        math1 = Math.floor(Math.random()*10);// new random Value
        trace(math1);
        }
        if(Asteroid5.y > 768){
        Asteroid5.y = 0;
        math1 = Math.floor(Math.random()*10);// new random Value
        }

        Asteroid5.x = Asteroid5.x + math1;
        Asteroid5.y = Asteroid5.y + math1;

}

答案 1 :(得分:0)

import flash.events.Event;
addEventListener(Event.ENTER_FRAME, massGain);
stop();

var speedX = Math.floor(Math.random()*20-10);
var speedY = Math.floor(Math.random()*20-10);

function massGain(e:Event)
{


Mouse.hide();

Cosmo.x = mouseX;
Cosmo.y = mouseY;

Asteroid5.x = Asteroid5.x + speedX;
Asteroid5.y = Asteroid5.y + speedY;
if(Asteroid5.x >1024||Asteroid5.x<0){
speedX = -speedX;
}
if(Asteroid5.y > 768||Asteroid5.y<0){
speedY = -speedY;
}
}

试试这个。