1

对于学校,我正在为移动设备创建一个基本的 AS3 游戏,基本上你需要知道的是我需要一个舞台宽度的矩形(380)(高度无关紧要)然后在几秒钟后生成顶部另一个产生,这个过程无限重复,直到另有说明。我还没有做太多,所以我没有太多代码要显示,但如果有人能告诉我如何将不胜感激。

我有向下的运动,只是没有产卵

var rectangle:Shape = new Shape;
var RecTimer:Timer = new Timer(10,10000);
RecTimer.addEventListener(TimerEvent.TIMER, fRecMovement);
RecTimer.start()


function fRecMovement (e:TimerEvent):void {

rectangle.graphics.beginFill(0xFF0000); // choosing the colour for the fill, here it is red
rectangle.graphics.drawRect(0, 0, 480,45.49); // (x spacing, y spacing, width, height)
rectangle.graphics.endFill();
addChild(rectangle); // adds the rectangle to the stage
rectangle.y +=1


}
4

1 回答 1

0

每次要添加内容时,都需要使用 new 关键字创建一个新形状。但是您还需要跟踪您创建的每个矩形,以便您可以移动它。在下面的代码中,矩形数组是所有矩形的列表。然后您可以浏览列表并移动每个矩形。

var RecTimer:Timer = new Timer(10,10000);
RecTimer.addEventListener(TimerEvent.TIMER, onTimer);
RecTimer.start();

var rectangles:Array = []; // a list of all the rectangles we've made so far

function spawnRectangle():void {
    var rectangle:Shape = new Shape();
    rectangle.graphics.beginFill(0xFF0000); // choosing the colour for the fill, here it is red
    rectangle.graphics.drawRect(0, nextRectangleY, 480, 45.49); // (x spacing, y spacing, width, height)
    rectangle.graphics.endFill();
    addChild(rectangle); // adds the rectangle to the stage

    rectangles.push(rectangle); // adds the rectangle to our list of rectangles
}

function moveAllRectangles():void {
    for each (var rectangle:* in rectangles) {
        rectangle.y += 1;
    }
}

function onTimer(e:TimerEvent) {
    spawnRectangle();
    moveAllRectangles();
}

这将在每次计时器运行时创建一个新的矩形,但您可能希望比这慢一些。也许你可以用一个单独的计时器来做到这一点。

此外,如果您继续让此代码运行,它会减慢很多速度并使用大量内存,因为它将不断创建新的矩形并且永远不会停止!看看你是否能找到一种方法来限制它。

于 2015-10-22T23:42:31.367 回答