如何向舞台添加有限数量的对象

发布于 2024-12-25 02:59:22 字数 1125 浏览 0 评论 0原文

所以我的库中有一个导出为 Box 的盒子。我已经尝试过:

package {

    import flash.display.MovieClip;
    import flash.events.*;

    public class Main extends MovieClip {

        private var _box:Box=new Box  ;
        private var boxAmount:Number=0;
        private var boxLimit:Number=16;


        private var _root:Object;
        public function Main() {

            addEventListener(Event.ENTER_FRAME, eFrame);

            addEventListener(MouseEvent.MOUSE_DOWN, mouseclick);
        }

        private function eFrame(event:Event):void {
            if (boxAmount <= boxLimit) {
                boxAmount++;
                _box.y=Math.random()*stage.stageHeight;

                _box.x=Math.random()*stage.stageWidth;

                addChild(_box);

            } else if (boxAmount >= boxLimit) {
                removeEventListener(Event.ENTER_FRAME, eFrame);
            } else {
                addEventListener(Event.ENTER_FRAME, eFrame);
            }
        }
    }
}

但它没有按计划工作。

我想做的是让我的盒子停留在屏幕上舞台上的随机位置,并在单击时将其删除(但这将在稍后进行)。此代码出于某种原因将对象添加到舞台,然后将其删除并再次添加,最多 16 次。

谢谢

So I have a box exported as Box in my library. I have tried :

package {

    import flash.display.MovieClip;
    import flash.events.*;

    public class Main extends MovieClip {

        private var _box:Box=new Box  ;
        private var boxAmount:Number=0;
        private var boxLimit:Number=16;


        private var _root:Object;
        public function Main() {

            addEventListener(Event.ENTER_FRAME, eFrame);

            addEventListener(MouseEvent.MOUSE_DOWN, mouseclick);
        }

        private function eFrame(event:Event):void {
            if (boxAmount <= boxLimit) {
                boxAmount++;
                _box.y=Math.random()*stage.stageHeight;

                _box.x=Math.random()*stage.stageWidth;

                addChild(_box);

            } else if (boxAmount >= boxLimit) {
                removeEventListener(Event.ENTER_FRAME, eFrame);
            } else {
                addEventListener(Event.ENTER_FRAME, eFrame);
            }
        }
    }
}

But it did not work as planned.

What I am trying to do is make my box stay on the screen at a random place on the stage and remove it when clicked (but that will come later). This code is for some reason adding the object to the stage and then removing it and adding it again up to 16 times.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

客…行舟 2025-01-01 02:59:22

我似乎您已经创建了一个 _box,并将其重新添加到输入帧的时间轴中。如果您在 eFrame 函数内部而不是之前创建一个新的框实例,那么它应该可以工作,然后您继续重新分配给相同的变量名称,而不是重用一个对象,例如:

package {

import flash.display.MovieClip;
import flash.events.*;

public class Main extends MovieClip {


    private var boxAmount:Number=0;
    private var boxLimit:Number=16;


    private var _root:Object;
    public function Main() {

        addEventListener(Event.ENTER_FRAME, eFrame);

        addEventListener(MouseEvent.MOUSE_DOWN, mouseclick);
    }

    private function eFrame(event:Event):void {
        if (boxAmount<=boxLimit) {
            boxAmount++;

            var _box:Box=new Box  ;

            _box.y=Math.random()*stage.stageHeight;

            _box.x=Math.random()*stage.stageWidth;

            addChild(_box);


        } else if (boxAmount >= boxLimit) {
            removeEventListener(Event.ENTER_FRAME, eFrame);
        } else {
            addEventListener(Event.ENTER_FRAME, eFrame);





        }
    }
}
}

I it seems like you have created one _box, and re-add it to the timeline on enter frame. It should work if you create a new box instance inside the eFrame function rather than before it, then you keep reassigning to the same variable name, rather than reusing the one object eg:

package {

import flash.display.MovieClip;
import flash.events.*;

public class Main extends MovieClip {


    private var boxAmount:Number=0;
    private var boxLimit:Number=16;


    private var _root:Object;
    public function Main() {

        addEventListener(Event.ENTER_FRAME, eFrame);

        addEventListener(MouseEvent.MOUSE_DOWN, mouseclick);
    }

    private function eFrame(event:Event):void {
        if (boxAmount<=boxLimit) {
            boxAmount++;

            var _box:Box=new Box  ;

            _box.y=Math.random()*stage.stageHeight;

            _box.x=Math.random()*stage.stageWidth;

            addChild(_box);


        } else if (boxAmount >= boxLimit) {
            removeEventListener(Event.ENTER_FRAME, eFrame);
        } else {
            addEventListener(Event.ENTER_FRAME, eFrame);





        }
    }
}
}
烟织青萝梦 2025-01-01 02:59:22

在你的代码中你只创建了一个盒子。您的 EnterFrame 处理程序只是为其分配了 16 次新的随机位置。如果您想要 16 个框,则需要每次在 EnterFrame 函数中创建一个新框。

但您不需要在此处使用 ENTER_FRAME 事件。您可以使用 for 循环同时循环来创建 16 个盒子。

下面是一些代码:

package {

import flash.display.MovieClip;
import flash.events.*;

public class Main extends MovieClip {

    private var boxAmount:Number=0;
    private var boxLimit:Number=16;

    public function Main() {
        addBoxes();
    }

    private function addBoxes():void {

        while (boxAmount<=boxLimit) {
            boxAmount++;

            var box:Box = new Box();
            box.y=Math.random()*stage.stageHeight;
            box.x=Math.random()*stage.stageWidth;

            addChild(box);

            // listen for mouse clicks
            box.addEventListener(MouseEvent.CLICK, onBoxClick);
        }
    }

    private function onBoxClick(e:MouseEvent):void {
        var clickedBox:Box = e.target as Box;
        removeChild(clickedBox);
    }

}
}

我删除了 EnterFrame 处理程序,只创建了一个名为 addBoxes 的函数。我正在使用 while 循环来装箱。请注意,每次循环时我都会创建一个新框,而不仅仅是重用旧框。我还向每个框添加了一个鼠标单击事件侦听器,以便在单击时可以将其从舞台上删除。

您肯定会想要更改其中一些内容以使其满足您的目的,但它应该让您朝着正确的方向前进。

In your code you are only ever creating one box. Your enterFrame handler is just assigning it a new random position 16 times. If you want 16 boxes you'll need to create a new box each time in the enterFrame function.

But you don't need to use the ENTER_FRAME event here. You could just use a for loop or a while loop to create the 16 boxes.

Here's some code:

package {

import flash.display.MovieClip;
import flash.events.*;

public class Main extends MovieClip {

    private var boxAmount:Number=0;
    private var boxLimit:Number=16;

    public function Main() {
        addBoxes();
    }

    private function addBoxes():void {

        while (boxAmount<=boxLimit) {
            boxAmount++;

            var box:Box = new Box();
            box.y=Math.random()*stage.stageHeight;
            box.x=Math.random()*stage.stageWidth;

            addChild(box);

            // listen for mouse clicks
            box.addEventListener(MouseEvent.CLICK, onBoxClick);
        }
    }

    private function onBoxClick(e:MouseEvent):void {
        var clickedBox:Box = e.target as Box;
        removeChild(clickedBox);
    }

}
}

I removed your enterFrame handler and just made a function called addBoxes. I'm using a while loop to crate the boxes. Notice that each time through the loop I'm creating a NEW box, not just reusing the old one. I'm also adding a mouse click event listener to each box so it can be removed from the stage when clicked.

You'll surely want to change some of this to get it to work for your purposes, but it should get you headed in the right direction.

白芷 2025-01-01 02:59:22

您现在所拥有的只是一遍又一遍地重新定位同一个框,因为您只创建了一个 Box 实例。您需要创建 Box 的多个实例并将它们分别添加到舞台上。

package {

    import flash.display.MovieClip;
    import flash.events.*;

    public class Main extends MovieClip {

        private var boxAmount:Number=0;
        private var boxLimit:Number=16;
        private var _root:Object;

        public function Main() {
            addEventListener(Event.ENTER_FRAME, eFrame);
            addEventListener(MouseEvent.MOUSE_DOWN, mouseclick);
        }

        private function eFrame(event:Event):void {
            if (boxAmount<=boxLimit) {
                boxAmount++;
                //create a new box instance
                var _box:Box = new Box();
                _box.y=Math.random()*stage.stageHeight;
                _box.x=Math.random()*stage.stageWidth;

                addChild(_box);
            } else {
                removeEventListener(Event.ENTER_FRAME, eFrame);
            } 
        }
    }
}

What you have at the moment is just repositioning the same box over and over because you only ever create one Box instance. You need to create multiple instances of Box and add them to the stage individually.

package {

    import flash.display.MovieClip;
    import flash.events.*;

    public class Main extends MovieClip {

        private var boxAmount:Number=0;
        private var boxLimit:Number=16;
        private var _root:Object;

        public function Main() {
            addEventListener(Event.ENTER_FRAME, eFrame);
            addEventListener(MouseEvent.MOUSE_DOWN, mouseclick);
        }

        private function eFrame(event:Event):void {
            if (boxAmount<=boxLimit) {
                boxAmount++;
                //create a new box instance
                var _box:Box = new Box();
                _box.y=Math.random()*stage.stageHeight;
                _box.x=Math.random()*stage.stageWidth;

                addChild(_box);
            } else {
                removeEventListener(Event.ENTER_FRAME, eFrame);
            } 
        }
    }
}
染墨丶若流云 2025-01-01 02:59:22

尽管变量 boxAmount 另有暗示,但您说您只想要一个盒子。因此,为此,您只需将以下行移动到构造函数 (Main) 中。

_box.y=Math.random()*stage.stageHeight;
_box.x=Math.random()*stage.stageWidth;
addChild(_box);

然后删除或禁用进入帧事件。在这种情况下你不需要它。要检查该框是否被单击,请将侦听器附加到该框本身,而不是其父级:

_box.addEventListener(MouseEvent.MOUSE_DOWN, mouseclick);

if (boxAmount<=boxLimit) {
    // ...
} else if (boxAmount >= boxLimit) {
    // ...
} else {
    // ...
}

这部分看起来非常奇怪。第一个条件涵盖了第二个条件也涵盖的情况,它们一起已经涵盖了所有可能的情况。 boxAmount 小于或等于 boxLimits 或大于它。两次检查相等性会令人困惑。不需要包含最后的 else 语句。它实际上与以下代码具有相同的行为。

if (boxAmount<=boxLimit) {
    // ...
} else if (boxAmount > boxLimit) {
    // ...
}

Although the variable boxAmount suggests otherwise, you said you only want one box. So, to do this, you just need to move the following lines into the constructor (Main).

_box.y=Math.random()*stage.stageHeight;
_box.x=Math.random()*stage.stageWidth;
addChild(_box);

Then remove or disable the enter frame event. You don't need it in this case. To check if the box got clicked, attach the listener to the box itself, not to it's parent:

_box.addEventListener(MouseEvent.MOUSE_DOWN, mouseclick);

if (boxAmount<=boxLimit) {
    // ...
} else if (boxAmount >= boxLimit) {
    // ...
} else {
    // ...
}

This part looks really strange. The first condition covers a case that is also covered by the second condition, together they already cover all possible cases. boxAmount is either less or equals to boxLimits or it is greater than it. Checking for equality twice is confusing. There is no need to include the last else statement. It actually has the same behaviour as the following code.

if (boxAmount<=boxLimit) {
    // ...
} else if (boxAmount > boxLimit) {
    // ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文