如何向舞台添加有限数量的对象
所以我的库中有一个导出为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我似乎您已经创建了一个 _box,并将其重新添加到输入帧的时间轴中。如果您在 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:
在你的代码中你只创建了一个盒子。您的 EnterFrame 处理程序只是为其分配了 16 次新的随机位置。如果您想要 16 个框,则需要每次在 EnterFrame 函数中创建一个新框。
但您不需要在此处使用 ENTER_FRAME 事件。您可以使用 for 循环 或 同时循环来创建 16 个盒子。
下面是一些代码:
我删除了 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:
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.
您现在所拥有的只是一遍又一遍地重新定位同一个框,因为您只创建了一个
Box
实例。您需要创建Box
的多个实例并将它们分别添加到舞台上。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 ofBox
and add them to the stage individually.尽管变量
boxAmount
另有暗示,但您说您只想要一个盒子。因此,为此,您只需将以下行移动到构造函数 (Main
) 中。然后删除或禁用进入帧事件。在这种情况下你不需要它。要检查该框是否被单击,请将侦听器附加到该框本身,而不是其父级:
这部分看起来非常奇怪。第一个条件涵盖了第二个条件也涵盖的情况,它们一起已经涵盖了所有可能的情况。
boxAmount
小于或等于boxLimits
或大于它。两次检查相等性会令人困惑。不需要包含最后的 else 语句。它实际上与以下代码具有相同的行为。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
).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:
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 toboxLimits
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.