2012-05-29 64 views
-1

我正在進行拖放式遊戲。 在發生MouseEvent.MOUSE_UP我有一個if語句,像這樣:as3 cs5拖放遊戲中的奇數錯誤#1009

function fnUp(event:MouseEvent):void 
    { 
     clicked.stopDrag(); 
     if (clicked.dropTarget.parent.parent.name == clicked.currentLabel) 
     { 
      var dropped:Card = clicked.dropTarget.parent.parent as Card; 
      dropped.gotoAndStop(dropped.name); 
      dropped.bg.gotoAndStop("done"); 
      removeChild(clicked); 

     } 
     else 
     { 
      clicked.x = clicked.sx; 
      clicked.y = clicked.sy; 
      clicked.bg.gotoAndStop("off"); 
     } 
     clicked = null; 
    } 

「點擊」是類型卡的早些時候宣佈VAR();和它的設置中的MouseEvent.MOUSE_DOWN像這樣:

function fnDown(event:MouseEvent):void 
    { 
     clicked = event.currentTarget as Card; 
     clicked.bg.gotoAndStop("on"); 
     clicked.startDrag(); 
     addChild(clicked); 
    } 

我想,如果我建立我的if語句這樣,如果卡在任何地方下降就不會有錯誤,但在三個可能的一個目標卡片。但我仍然得到:

TypeError: Error #1009: Cannot access a property or method of a null object reference.

tldr;如果我將我的卡放在3個可能目標中的一個之外,我會得到一個錯誤。

回答

1

在哪條線上出現錯誤?您需要檢查所有的下列對象的存在:

if (clicked.dropTarget && 
clicked.dropTarget.parent && 
clicked.dropTarget.parent.parent) 
{ 
    then do something... 
} 
+0

我拖着夾子只有當條件適用我把if語句做一些事情:clicked.dropTarget.parent.parent.name ==點擊.currentLabel。我得到的錯誤只有當我將它放到舞臺上三個可能的droptargets之一以外時纔會得到,只有它不應該反應,因爲它應該在if語句的else部分之下(我有道理?) – silvith

+0

你需要檢查是否存在所有變量,因爲如果由於某些原因'clicked.dropTarget'爲'null',那麼你將無法獲得'clicked.dropTarget.parent'這個將導致1009錯誤。 –

+0

正如我試圖解釋的那樣,if語句適用於droptargets。只有當我在舞臺上的droptargets之外放下「單擊」時,我會遇到1009錯誤。 – silvith