在另一个中添加 object

Adding object within another

我的主舞台是 550x400。 header 区域是一个统计栏。所以我在它下面有一个元素,我将其命名为 550x350 的 gameStage。

我每隔 1 秒创建一个圆圈,然后尝试将它们随机放置在我的 gameStage 中。它似乎不起作用。它们似乎被添加到一个 550x350 的元素中,但它是从我的主舞台的顶部开始的——而不是在我的 gameStage 中。

此外,如果我简单地执行 addChild(circle) 它会创建一个偶数 25 半径的圆。一旦我做 gameStage.addChild(circle),圆圈就会稍微倾斜。

我做错了什么?

private function createCircle():void {      
        var stageSafeX:Number = Math.random()*gameStage.width;
        var stageSafeY:Number = Math.random()*gameStage.height;

        var circle:Sprite = new Sprite();
        circle.graphics.clear();
        circle.graphics.beginFill(Math.random()*0xFFFFFF, 1);
        circle.graphics.drawCircle(0, 0, circleRadius);
        circle.graphics.endFill();
        circle.x = stageSafeX;
        circle.y = stageSafeY;
        circle.name = String(circleCount);
        gameStage.addChild(circle);

}

好的,我正在使用 Flash Develop,所以您必须原谅我,因为这个程序没有 FLA 文件,只有 classes 并且它使用 Main class 启动程序(更容易让人想起 Java,如果你曾经用它编程的话)。但我将向您展示的代码或多或少与您想要的方式相同。

首先我会建议你制作一个 randomNumber 函数,我在制作这段代码时使用了它所以如果你想使用它这是我使用的那个(我把它放在 Main class 中,你可以把随心所欲):

public static function randomNumber(minValue:Number, maxValue:Number):uint {
    return Math.floor(Math.random() * (1 + maxValue - minValue)) + minValue;
}

这是包容性的,意思是如果你输入randomNumber(1, 10),它会给你一个1到10之间的数字,包括1和10。这或多或少是常识,但我想我还是提一下只是为了澄清。

现在介绍 addCircle 函数:

public static function addCircle(gameStage:Sprite, circleRadius:uint):void {
    //Initializing the new circle instance
    var newCircle:Sprite = new Sprite();

    //Basically the same code you had (you don't need to set the alpha value to 1, it's default value is 1 regardless)
    newCircle.graphics.beginFill(Math.random() * 0xFFFFFF);
    newCircle.graphics.drawCircle(0, 0, circleRadius);
    newCircle.graphics.endFill();

    //Since the circle's origin is the center, you want its outer edges to be bound to the gameStage's edges
    var safeStageX:Number = Main.randomNumber(newCircle.width / 2, gameStage.width - newCircle.width / 2);
    var safeStageY:Number = Main.randomNumber(newCircle.height / 2, gameStage.height - newCircle.height / 2);

    //Adding the circle to the gameStage's display field
    gameStage.addChild(newCircle);

    //Only set the circle's x and y AFTER you add it to the gameStage's display list, otherwise it might not set properly
    newCircle.x = safeStageX;
    newCircle.y = safeStageY;
}

下面我给出我创​​建gameStage的代码。你可能已经有了它的东西,但我会提供我的,以防你想使用它:

//Initializing the gameStage instance
var gameStage:Sprite = new Sprite();

//Adding the gameStage to the Stage's display field
this.stage.addChild(gameStage);

//Setting the gameStage's width and height (using "gameStage.width = 550" and "gameStage.height = 350" WILL NOT WORK)
//Use the color of your main game's background so you don't see this fill (unless you want to)
//Either do this or add a background picture, you need to do one or the other in order to set the gameStage's dimensions
gameStage.graphics.beginFill(0x000000);
gameStage.graphics.drawRect(0, 0, 550, 350);
gameStage.graphics.endFill();

//This puts the gameStage on the bottom of the screen (since it's 50 pixels shorter in the y direction)
gameStage.y = 50;

最后,我会给你实际的 for 循环来创建你的圈子(这个函数出现在你的 gameStage 所在的同一个 class/FLA 中,因为 addCircle 函数需要接受那个 gameStage 实例:

//Now let's populate your gameStage
for (var i:uint = 0; i < [number of circles you want]; i++) {
    Main.addCircle(gameStage, [radius of the circle]);
}

大功告成!我还将包括整个 Main class,以便您了解所有功能如何协同工作。

package {
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.Event;

    public class Main extends Sprite {

        public function Main() {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            var gameStage:Sprite = new Sprite();
            this.stage.addChild(gameStage);

            gameStage.graphics.beginFill(0x000000);
            gameStage.graphics.drawRect(0, 0, 550, 350);
            gameStage.graphics.endFill();
            gameStage.y = 50;

            for (var i:uint = 0; i < 150; i++) {
                Main.addCircle(gameStage, Main.randomNumber(15, 25));
            }
        }

        public static function addCircle(gameStage:Sprite, circleRadius:uint):void {
            var newCircle:Sprite = new Sprite();

            newCircle.graphics.beginFill(Math.random() * 0xFFFFFF);
            newCircle.graphics.drawCircle(0, 0, circleRadius);
            newCircle.graphics.endFill();

            var safeStageX:Number = Main.randomNumber(newCircle.width / 2, gameStage.width - newCircle.width / 2);
            var safeStageY:Number = Main.randomNumber(newCircle.height / 2, gameStage.height - newCircle.height / 2);

            gameStage.addChild(newCircle);

            newCircle.x = safeStageX;
            newCircle.y = safeStageY;
        }

        public static function randomNumber(minValue:Number, maxValue:Number):uint {
            return Math.floor(Math.random() * (1 + maxValue - minValue)) + minValue;
        }
    }
}