2011-05-04 25 views
0

昨天有人在幫我解決我遇到的問題。我在測試之前接受了答案,並且遇到了問題。Actionscript - 函數不是有效的類型

我在做什麼是我有一架飛機MC和一箱MC。飛機沿y軸飛行,我試圖讓箱子mc隨機沿着飛機的路徑隨機掉落。這架飛機在y軸的每一個點上不斷下降箱子。

我使用移動板/放箱子的代碼是:

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

代碼有人給我砸1個箱子代替衆多的箱子是:

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

當我嘗試使用此代碼時,出現錯誤「功能不是類型」。我從來沒有見過函數用作返回類型。誰能幫我?

回答

2

退貨類型function應該大寫:Function。 timerComplete函數將planeID鎖定在閉包中,以便可以從事件處理函數(從timerComplete返回的函數)訪問該函數。

+0

啊很多謝謝肖恩:) – RapsFan1981 2011-05-04 21:39:28

相關問題