Search code examples
randomactionscriptpositioning

Actionscript - randomly drop from moving plane MC


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

Solution

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