ActionScript 3 - 调整math.Random的速度?

时间:2014-03-20 08:47:07

标签: actionscript-3 flash

我刚刚开始学习AS3。

我制作了这个小游戏,其中一些星星在背景中产生随机的坐标, 我让它随机地随机,所以它每次都会把星星产生到一个不同的地方, 但现在太快了:

import flash.events.Event;

addEventListener(Event.ENTER_FRAME,movestar);

Mouse.hide();

function movestar(e:Event)
{
star1.x++
star2.x++
star3.x++
star4.x++
star5.x++
star6.x++
star7.x++
star8.x++
star9.x++

star1.x = Math.random()*550;
star2.x = Math.random()*550;
star3.x = Math.random()*550;
star4.x = Math.random()*550;
star5.x = Math.random()*550;
star6.x = Math.random()*550;
star7.x = Math.random()*550;
star8.x = Math.random()*550;
star9.x = Math.random()*550;

star1.y = Math.random()*400;
star2.y = Math.random()*400;
star3.y = Math.random()*400;
star4.y = Math.random()*400;
star5.y = Math.random()*400;
star6.y = Math.random()*400;
star7.y = Math.random()*400;
star8.y = Math.random()*400;
star9.y = Math.random()*400;
 }

有没有办法可以调整星星的速度?

2 个答案:

答案 0 :(得分:1)

  

但现在太快了

快?每帧你给它们新的坐标,并在1像素上偏移它们......

这里有一个片段,在那里我创建了伪星(黑眼圈),随机散布它们并将它们线性移动到屏幕的左侧:

//Create stars for testing
var stars:Array = createStars(30);
trace(stars);
//Scatter them
scatterObjects(stars, stage.stageWidth, stage.stageHeight);
//Move them with some speed
var speed:Number = 0.5;
addEventListener(Event.ENTER_FRAME, onEnterFrame);

function onEnterFrame(e:Event):void {
    var i:uint, len:uint = stars.length, star:DisplayObject;

    for (i; i < len; ++i) {
        star = stars[i];
        star.x -= speed;
        if (star.x < 0) {
            star.x = stage.stageWidth;
        }
    }
}

function scatterObjects(list:Array, width:int, height:int):void {
    var star:DisplayObject, i:uint, len:uint = list.length;

    for (i; i < len; ++i) {
        star = list[i];
        star.x = width * Math.random();
        star.y = height * Math.random();
        addChild(star);
    }
}

function createStars(count:uint):Array {
    var result:Array = [], star:Shape, i:uint;
    for (i; i < count; ++i) {
        star = new Shape();
        star.graphics.beginFill(0x333333);
        star.graphics.drawCircle(0, 0, 5);
        result[i] = star;
    }
    return result;
}

答案 1 :(得分:0)

我认为说你是&#34;移动&#34;星星,因为看起来你只是随机地将每个帧的起点放在550 x 400的区域内。

为什么在每个输入帧事件中递增x?

无论如何,如果你想减慢&#34;运动&#34;星星,为moveStar添加一个计数器,如下所示:

counter++;
if(counter<3){
 return;
}
counter=0;

这将每隔三帧更新一次你的星星。您可以使用计时器,并在n毫秒后更新,当游戏在不同的计算设备上以不同的速率运行时,这将更准确。