2014-01-23 79 views
0

我試圖訪問存儲在文檔類中的變量healthStatehealthInt。我已經試過MovieClip(parent).healthState++;,正如你所看到的,但是我得到了未定義屬性健康狀態的錯誤1120訪問。該變量是公共的。聲明變量時還有什麼我應該做的嗎?如何在as3中訪問其父子項中的變量

private function collisionDetection() { 

    for (var i = 0; i < enemies.length; i++) { 

     var type = getQualifiedClassName(enemies[i]); 
     var hype = myMath.distanceBetween(enemies[i],player); 

     if (hype < 10 && enemies[i] is Swirl && enemies[i].parent != null) { 

      MovieClip(this).removeChild(enemies[i]); 
      MovieClip(parent).healthState++; 

      if (healthState <= 8) { 

       MovieClip(parent).healthInt--; 
       trace(healthInt); 
       hearts.gotoAndStop(healthState); 
      } 

      if (MovieClip(parent).healthInt <= 0) { 

       MovieClip(parent).desiredLabel = 'GameOver'; 
       MovieClip(parent).tweenOut(); 
       gameOver = true; 
       removeChild(enemies); 
       squares.splice(0); 
       enemies.splice(0); 
       parent.removeChild(this); 
      } 
     } 
    } 
} 

回答

1

您無法投射到影片剪輯,因爲錯誤指出該影片在MovieClip類內不存在。

儘量鑄造的對象,而不是:

Object(parent).healthState++; 

這句法也應該工作:

parent["healthState"]++;