2014-04-19 21 views
1

我想訪問一個孩子,所以我可以做一些hitTest與那個孩子,但我得到這個錯誤,你可能已經看到了一百萬次,並有足夠的時間來理解它的含義,不像我(As3的新手)。我不能訪問已添加到另一個孩子(它的父母)的孩子

1119: Access of possibly undefined property ground through a reference with static type flash.display:Sprite. 

這是一些代碼。

我開始在主構造函數的一切,然後將其添加到舞臺

 addChild(_scrollLayer); 
     //player 
     _scrollLayer.addChild(character); 
     //character.reset(); 
     //character.x = 640; 
     character.y = 0; 

     //level 
     _scrollLayer.addChild(ground); 
     ground.x = 640; 
     ground.y = 680; 


     //greenGoblin 
     _scrollLayer.addChild(goblin1); 
     goblin1.x = 500; 
     goblin1.y = 0; 

     _scrollLayer.addChild(goblin2); 
     goblin2.x = 800; 
     goblin2.y = 0; 

     _scrollLayer.addChild(goblin3); 
     goblin3.x = 100; 
     goblin3.y = 0; 

     //red Goblin 
     _scrollLayer.addChild(redGoblin1); 
     redGoblin1.x = 1100; 
     redGoblin1.y = 400; 



     while (_scrollLayer.ground.hitTestPoint(getChildAt(c).x, getChildAt(c).y, true)) 

這是當我得到您上面所看到的錯誤消息。

我試圖這樣做也

 while (ground.hitTestPoint(getChildAt(c).x, getChildAt(c).y, true)) 

沒有錯誤消息,怎麼過了則hitTest是不行的,這是因爲一切都在一個容器中。

以下是完整的命中測試代碼

 for (var c:int = 0; c < childrenOnStage; c++) 
     { 
      if (getChildAt(c).name == "player") 
      {  
       if (ground.hitTestPoint(getChildAt(c).x, getChildAt(c).y, true)) 
       { 
        touchingGround = true; 
        while (ground.hitTestPoint(getChildAt(c).x, getChildAt(c).y, true)) 
        { 

         OnGround(getChildAt(c)).incrementUp(); 

         if (ground.hitTestPoint(getChildAt(c).x, getChildAt(c).y, true)) 
         { 

         } 
         else 
         { 
          OnGround(getChildAt(c)).keepOnGround(); 
         } 
        } 
       } 
       else 
       { 
        touchingGround = false; 
        //trace("not touch"); 
        //character.gotoAndStop("jump"); 
       } 
      } 

//childrenOnStage is a variable. childrenOnStage = this.numChildren; 

我不明白爲什麼命中測試是行不通的。我會很感激的答覆,建議或提示請。

謝謝。

+0

_scrollLayer是儘管順便說一句。 – Moynul

回答

2

我以這種方式在命中測試時可能有點生鏽(我使用的視覺IDE比大多數更重),但我想我記得這個問題。

如果我沒有記錯,DisplayObject對象與編程對象的聲明不同,因爲您不能簡單地調用它們的名稱來訪問它們。 AS3認爲你正在調用Sprite的屬性。 (也就是說,如果你在時間線上工作,那將是另一回事。)

下面的代碼應該工作,雖然它沒有經過測試。你的代碼沒有看到作爲DisplayObject的'地面',所以它不能在其上調用「hitTestPoint」。因此,你必須解決這個問題。

var mcGround:MovieClip = MovieClip(_scrollLayer.getChildByName("ground")); 
mcGround.hitTestPoint(getChildAt(c).x, getChildAt(c).y, true); 

(感謝Amarghosh的答案here - I交叉檢查我與它的代碼。)

+1

謝謝,我目前正在睡覺的過程中。我會明天檢查。吃,睡,編碼,重複 – Moynul

+1

@Moynul在睡前吃東西顯然對你不好。您可以通過切換來限制身體錯誤:吃,編碼,睡眠,重複 –

相關問題