在另一个中添加对象

时间:2015-07-22 15:41:07

标签: actionscript-3 flash

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

我以1秒的间隔创建圈子,然后尝试将它们随机放置在我的gameStage中。它似乎没有工作。看起来它们被添加到550x350元素中,但它从我主舞台的顶部开始 - 而不是在我的gameStage中。

此外,如果我只是执行addChild(circle),它会创建一个均匀的25半径圆。一旦我做了gameStage.addChild(圆圈),圆圈会稍微偏斜。

我做错了什么?

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);

}

1 个答案:

答案 0 :(得分:1)

好的我正在使用Flash Develop,所以你必须原谅我,因为这个程序没有FLA文件,只有类,它使用Main类来启动程序(更让人联想到如果您曾编程过Java,请参阅Java)。但是我给你的代码或多或少与你想要的代码相同。

首先我建议你制作一个randomNumber函数,我用它来制作这段代码所以如果你想在这里使用它我使用的那个(我把它放在Main类中,你可以把它放在任何地方你想要的):

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所在的同一个类/ 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类,以便您可以看到所有函数如何协同工作。

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;
        }
    }
}