Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 在Actionscript 3中访问不同的类_Actionscript 3_Flash - Fatal编程技术网

Actionscript 3 在Actionscript 3中访问不同的类

Actionscript 3 在Actionscript 3中访问不同的类,actionscript-3,flash,Actionscript 3,Flash,我正试图编写一个小游戏,但偶然发现了一些问题。 我有几节课。其中一个是wall类。当击中玩家时,应该从健康栏中移除一颗心脏。 打击玩家的效果很好,但是我不能移除UI类中的心脏 梅因 public class TwinRunner extends MovieClip { private var _timer:Timer; private var _userInterface:UI = new UI(); public function TwinRunner() {

我正试图编写一个小游戏,但偶然发现了一些问题。 我有几节课。其中一个是wall类。当击中玩家时,应该从健康栏中移除一颗心脏。 打击玩家的效果很好,但是我不能移除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);
        }
    }
我可以从主类访问setter函数,但不能从wall.as类访问。 有没有办法做到这一点?我还在学习,所以我希望这不是一个愚蠢的问题:D


提前谢谢

您的问题是要从父级中删除子级(墙),这会导致父级变量为null-请参见下面代码中的注释:

parent.removeChild(this);  //this will make parent null
MovieClip(parent)._userInterface.heartMeter = false;  //parent is now null because this has been removed
简单地切换这两行应该可以解决您的主要问题

另外,
\u userInterface
在主类中声明为私有变量,因此如果您想在wall类中访问它(就像您正在尝试的那样),您需要将其公开,否则会出现另一个错误


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

在你的wall课程中,类似这样的内容:

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

您的问题是您正在从父项中删除子项(墙),这会导致父项变量为null-请参阅下面代码中的注释:

parent.removeChild(this);  //this will make parent null
MovieClip(parent)._userInterface.heartMeter = false;  //parent is now null because this has been removed
简单地切换这两行应该可以解决您的主要问题

另外,
\u userInterface
在主类中声明为私有变量,因此如果您想在wall类中访问它(就像您正在尝试的那样),您需要将其公开,否则会出现另一个错误


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

在你的wall课程中,类似这样的内容:

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

非常感谢您的帮助,现在我得到了以下错误:ReferenceError:error#1069:Property(属性)在TwinRunner上找不到userInterface并且没有默认值。这是因为您已将
\u userInterface
声明为私有变量,这会阻止它在主类之外被访问。将其公开,错误就会消失。非常感谢您的提示:)非常感谢您的帮助,现在我收到了以下错误:ReferenceError:error#1069:Property(属性)在TwinRunner上找不到userInterface且没有默认值。这是因为您已将
\u userInterface
声明为私有变量,这会阻止它在主类之外被访问。将其公开,错误将消失。非常感谢您的提示:)+1用于实际显示所有相关代码。+1用于实际显示所有相关代码。