我正在尝试编写一个小游戏,但偶然发现了一些问题。我有几节课。其中之一是墙类。击中玩家时应该从健康栏中移除一颗心。击中播放器效果很好,但我无法移除 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
提前致谢!