如何从as3中的子级访问父级中的变量

时间:2014-01-23 01:08:33

标签: actionscript-3

我正在尝试访问存储在文档类中的变量healthStatehealthInt。我已经尝试了MovieClip(parent).healthState++;,但是我得到了错误1120访问未定义的属性健康状态。变量是公开的。在声明变量时还有什么我应该做的吗?

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 个答案:

答案 0 :(得分:1)

您无法转换为MovieClip,因为错误指出,该值在MovieClip类中不存在。

尝试转换为对象:

Object(parent).healthState++;

此语法也应该有效:

parent["healthState"]++;