Actionscript - 从移动平面MC随机掉落
我不太确定如何描述我在该主题中的问题。我有一个飞机 MC 和一个板条箱 MC。飞机仅沿着 y 轴从屏幕底部飞到顶部。一路上我希望它随机掉落箱子 MC。我的代码如下。问题是板条箱会自发地不断生成,而不是靠近飞机。
function movePlane():void
{
var tempY:Number;
var tempX:Number;
var tempCrate:MovieClip;
var tempPlane:MovieClip;
for (var j:int =planes.length-1; j>=0; j--)
{
tempPlane = planes[j];
tempPlane.y += tempPlane.planeSpeed;
tempCrate = new Crate();
tempY = Math.floor(Math.random() * tempPlane.y);
tempX = Math.floor(Math.random() * tempPlane.x);
}
tempCrate.y = tempY;
tempCrate.x = tempX;
addChild(tempCrate);
}
I wasn't quite sure how to describe my problem in the subject. I have a plane MC and a crate MC. The plane only flies along the y axis from the bottom of the screen to top. Along the way I want it to randomly drop the crate MC. My code is below. The problem is that the crates spontaneously keep spawning and not near the plane.
function movePlane():void
{
var tempY:Number;
var tempX:Number;
var tempCrate:MovieClip;
var tempPlane:MovieClip;
for (var j:int =planes.length-1; j>=0; j--)
{
tempPlane = planes[j];
tempPlane.y += tempPlane.planeSpeed;
tempCrate = new Crate();
tempY = Math.floor(Math.random() * tempPlane.y);
tempX = Math.floor(Math.random() * tempPlane.x);
}
tempCrate.y = tempY;
tempCrate.x = tempX;
addChild(tempCrate);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑后的答案:
一旦您可以通过在每个平面上创建一个具有随机时间值的计时器来创建此行为,就可以在每个平面上放置一个板条箱。像这样:
编辑的答案:
这将在与其创建的平面相同的 x 轴上创建一个板条箱。
每次创建新的 Crate 时都必须使用 addChild,否则只会创建很多 crate,并且只有最后一个会添加到舞台中。为此,您必须将 addChild 移至循环中。
Edited answer:
To make a crate drop on each plane once you can create this behavior by creating a timer on each plane with a random time value. Like this:
Edited answer:
This will create a crate on the same x axis as the plane it's being created by.
You have have to use addChild each time you create a new Crate otherwise it will just create a lot of crates which only the last one will be added to the stage. To do this you have to move the addChild into the loop.