1

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

主文件

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 类添加为实例。

墙体

        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);
        }
    }

电影剪辑(父)._userInterface.heartMeter = false; <- 这部分给了我一个错误...

最后是 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

提前致谢!

4

1 回答 1

1

您的问题是您正在从父级中删除子级(墙),然后导致父级 var 为空 - 请参阅下面代码中的注释:

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;
}
于 2012-09-24T17:20:57.077 回答