0

所以,我正在尝试制作一个小行星随机移动的游戏,但是当我将 Math.random 函数放在我的函数之外时,它只给出 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;
}



}
4

2 回答 2

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;

}
于 2013-03-10T13:06:51.900 回答
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;
}
}

试试这个。

于 2013-03-10T13:14:07.800 回答