2014-05-23 43 views
0

所以確定我有點新的閃光我自己更多的是一個java男人,但無論如何,我不斷收到這個錯誤,所有的解決方案發現要麼不工作或他們打破了遊戲的碰撞錯誤#2025:提供的DisplayObject必須是調用者的孩子 - gotoAndStop後的遊戲循環

下面的代碼

stop(); 

import flash.utils.Timer; 
import flash.utils.getDefinitionByName; 
import flash.events.Event; 
import flash.events.TimerEvent; 

var playerobj:player; 
var nextObject:Timer; 
var objects:Array = new Array(); 
var score:int = 0; 
const speed:Number = 9.0; 
playerobj = new player(); 
playerobj.y = 650; 
addChild(playerobj); 


setNextObject(); 
addEventListener(Event.ENTER_FRAME, moveObjects); 


function setNextObject() 
{ 

    nextObject = new Timer(1000+Math.random()*1000,1); 
    nextObject.addEventListener(TimerEvent.TIMER_COMPLETE,newObject); 
    nextObject.start(); 

} 

function newObject(e:Event) 
{ 
    var newObject:AI; 
    newObject = new AI(); 
    newObject.x = Math.random() * 480; 
    addChild(newObject); 
    objects.push(newObject); 

    setNextObject(); 

} 

function moveObjects(e:Event) 
{ 
    for (var i:int=objects.length-1; i>=0; i--) 
    { 
     objects[i].y += speed; 
     if (objects[i].y > 800) 
     { 

      removeChild(objects[i]); 
      score = score + 10000; 
      objects.splice(i,1); 

     } 
     if (objects[i].hitTestObject(playerobj)) 
     { 

      cleanUp(); 

     } 
    } 

    playerobj.x = mouseX; 



} 

function cleanUp():void 
{ 
    while (this.numChildren > 0) 
      { 

       removeChildAt(0); 



      } 

    nextObject.stop(); 
    gotoAndStop(4); 
    stop(); 
} 

它必須以某種方式與此相關的問題,但只要gotoAndStop被稱爲遊戲似乎環周圍回到框架不真的很確定爲什麼,謝謝你的幫助

回答

2

在清理函數中,聲明它之後,您應該刪除ENTER_FRAME事件偵聽器。此外,我會停止計時器,然後刪除孩子,我會刪除您添加到動態階段的對象。清理函數中的stop()是多餘的。

function cleanUp():void { 

    removeEventListener(Event.ENTER_FRAME, moveObjects); 
    nextObject.stop(); 

    for(var i:uint = 0; i < objects.length; i++){ 
    removeChild(objects[i]); 
    } 
    removeChild(playerObj) 

    gotoAndStop(4);  
} 

此外,最好在時間軸中保留最少的代碼並儘可能地在類中移動。

0

錯誤是在cleanUp()

function moveObjects(e:Event) 
{ 
    for (var i:int=objects.length-1; i>=0; i--) 
    { 
     objects[i].y += speed; 
     if (objects[i].y > 800) 
     { 

      removeChild(objects[i]); 
      score = score + 10000; 
      objects.splice(i,1); 

     } 
     if (objects[i].hitTestObject(playerobj)) 
     { 

      cleanUp(); 

     } 
    } 

    //playerobj no more exists if cleanUp() is called, move this line above cleanUp(); 

    //playerobj.x = mouseX; 

    //or inside cleanUp() put that line 

    //removeEventListener(Event.ENTER_FRAME, moveObjects); 


} 
相關問題