RapsFan1981
RapsFan1981

Reputation: 1197

Actionscript - function is not a valid type

Yesterday someone on here was helping me with a problem I was having. I accepted the Answer before I had tested it and am running into a problem.

What I am doing is I have an airplane mc and a crate mc. The airplane flies along the y axis and I was trying to get the crate mc to drop somewhere randomly along the plane's path. The plane keeps dropping crates at every point along the y axis.

The code I'm using to move the plate/drop the crate is:

function makePlane():void
{
    var chance:Number = Math.floor(Math.random() * 60);
    if (chance <= 1)
    {
        trace(chance);

        var tempPlane:MovieClip;
        //Make sure a Library item linkage is set to Plane...
        tempPlane = new Airplane();
        tempPlane.planeSpeed = 10;
        tempPlane.x = Math.round(Math.random() * 1000);
        tempPlane.y = Math.round(Math.random() * -1000);
        addChild(tempPlane);
        trace("Made Plane!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        planes.push(tempPlane);
    }
}

function movePlane():void
{

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

The code someone gave me to drop 1 crate only instead of numerous crates is:

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

When I try using this code I get the error 'function is not a type'. I've never seen function used as a return type before. Can anyone help me?

Upvotes: 0

Views: 328

Answers (1)

Sean Fujiwara
Sean Fujiwara

Reputation: 4546

The return type function should be capitalized: Function. The timerComplete function is locking the planeID in a closure, so that it is accessible from the event handler (the function returned from timerComplete).

Upvotes: 2

Related Questions