在Actionscript 3中访问不同的类

时间:2012-09-24 17:13:55

标签: actionscript-3 flash

我正在尝试编写一个小游戏,但偶然发现了一些问题。 我有几节课。其中一个是墙类。当击中玩家时应该从健康栏中移除一颗心。 击中玩家的效果非常好,但是我无法移除UI类中的内心。

Main.as

public class TwinRunner extends MovieClip
{
    private var _timer:Timer;
    private var _userInterface:UI = new UI();

    public function TwinRunner()
    {
        //Timer initialize
        _timer = new Timer(800, 1);
        _timer.addEventListener(TimerEvent.TIMER_COMPLETE, onUpdateTime);
        _timer.start();

        stage.addChild(_userInterface);

        mcButton.addEventListener(MouseEvent.CLICK, onButtonClick);
        addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    }

这里我将UI类添加为实例。

Wall.as

        x += _vx;

        //Remove Wall when visible Sword hits
        if(MovieClip(parent).mcSword.isVisible)
        {
            if(this.hitTestObject(MovieClip(parent).mcSword))
            {
                trace("wall destroyed");
                parent.removeChild(this);
            }
        }

        //Remove Wall when Player hit
        else if(this.hitTestObject(MovieClip(parent).mcPlayer))
        {
            trace("player hit");
            parent.removeChild(this);
            MovieClip(parent)._userInterface.heartMeter = false;
        }

        //When the Wall is outside of the screen it gets removed
        if (x + width / 2 < 0)
        {
            parent.removeChild(this);
        }
    }

MovieClip(parent)._ userInterface.heartMeter = false; &lt; - 这部分给了我一个错误......

最后是UI.as

public class UI extends MovieClip
{
    private var heart1:heartFill = new heartFill();

    public function UI()
    {
        addChild(heart1);
        heart1.x = 200;
        heart1.y = 200;

        addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    }

    private function onAddedToStage(event:Event):void
    {
        addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
        addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }

    private function onRemovedFromStage(event:Event):void
    {
        removeEventListener(Event.ENTER_FRAME, onEnterFrame);
        removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
    }

    private function onEnterFrame(event:Event):void
    {

    }

    public function set heartMeter(visibility:Boolean):void
    {
        heart1.visible = false;
    }
}

我可以从Main类访问setter函数,但不能从wall.as类访问。 有办法吗?我还在学习,所以我希望这不是一个愚蠢的问题:D

提前致谢!

1 个答案:

答案 0 :(得分:1)

你的问题是你从父节点移除子节点(墙),然后将父节点变为空 - 请参阅下面的代码中的注释:

parent.removeChild(this);  //this will make parent null
MovieClip(parent)._userInterface.heartMeter = false;  //parent is now null because this has been removed

只需切换这两行就可以解决您的主要问题。

此外,_userInterface在您的主类中被声明为私有var,因此如果您想在墙类中访问它(就像您尝试的那样),您需要将其公开或你会得到另一个错误。


您可能想要考虑的事情是使用事件。这样,您的代码不直接引用其他类,如UI。类依赖关系的分离使测试和故障排除更容易。

在你的墙上课这样的事情:

else if(this.hitTestObject(MovieClip(parent).mcPlayer))
{
    trace("player hit");
    stage.dispatchEvent(new Event(PLAYER_HIT)); //PLAYER_HIT would be a public static constant in this class:  public static const PLAYER_HIT:String = "PlayerHit";
    parent.removeChild(this); //this needs to be last because after this line, stage and parent will be null
}

然后在你的UI类中:

private function onAddedToStage(event:Event):void
{
    addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
    addEventListener(Event.ENTER_FRAME, onEnterFrame);

    stage.addEventListener(Wall.PLAYER_HIT,playerHitHandler);
}

private function playerHitHandler(e:Event):void {
    heartMeter = false;
}