Actionscript - 从移动平面MC随机掉落

发布于 2024-11-04 13:15:56 字数 634 浏览 2 评论 0原文

我不太确定如何描述我在该主题中的问题。我有一个飞机 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 技术交流群。

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

发布评论

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

评论(1

放赐 2024-11-11 13:15:56

编辑后的答案:

一旦您可以通过在每个平面上创建一个具有随机时间值的计时器来创建此行为,就可以在每个平面上放置一个板条箱。像这样:

function addRandomCreation():void{
    var animationTime:Number = 5000; //The time the planes will be animating in ms 

    for(var i:int = 0; i < planes.length; i++){
        var planeTimer:Timer = new Timer(Math.round(animationTime * Math.random()));
        planeTimer.addEventListener(TimerEvent.TIMER, timerComplete(i));
        planeTimer.start();
    }
}

function timerComplete(planeID:int):function{
    return function(event:TimerEvent):void{
        event.target.stop();
        event.target.removeEventListener(event.type, arguments.callee);

        var tempCrate:MovieClip = new Crate();
        tempY = Math.round(Math.random() * planes[planeID].y);
        tempCrate.y = tempY;
        tempCrate.x = planes[planeID].x;
        addChild(tempCrate);        
    }
}

编辑的答案:

这将在与其创建的平面相同的 x 轴上创建一个板条箱。

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);
        tempCrate.y = tempY;
        tempCrate.x = tempPlane.x;
        addChild(tempCrate);
    }
}

每次创建新的 Crate 时都必须使用 addChild,否则只会创建很多 crate,并且只有最后一个会添加到舞台中。为此,您必须将 addChild 移至循环中。

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

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:

function addRandomCreation():void{
    var animationTime:Number = 5000; //The time the planes will be animating in ms 

    for(var i:int = 0; i < planes.length; i++){
        var planeTimer:Timer = new Timer(Math.round(animationTime * Math.random()));
        planeTimer.addEventListener(TimerEvent.TIMER, timerComplete(i));
        planeTimer.start();
    }
}

function timerComplete(planeID:int):function{
    return function(event:TimerEvent):void{
        event.target.stop();
        event.target.removeEventListener(event.type, arguments.callee);

        var tempCrate:MovieClip = new Crate();
        tempY = Math.round(Math.random() * planes[planeID].y);
        tempCrate.y = tempY;
        tempCrate.x = planes[planeID].x;
        addChild(tempCrate);        
    }
}

Edited answer:

This will create a crate on the same x axis as the plane it's being created by.

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);
        tempCrate.y = tempY;
        tempCrate.x = tempPlane.x;
        addChild(tempCrate);
    }
}

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.

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