I am trying to program a little game but stumbled upon some problems.
I have several classes. One of them is the wall class. Which when hitting the player should remove a heart from the health bar.
Hitting the player works pretty good but I cant remove the heart which is inside the UI class.
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);
}
Here I am adding the UI class as an instance.
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; <- This part gives me an error…
And last the 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;
}
}
I can access the setter function from the Main class but not from the wall.as class.
Is there a way to do so? I am still learning so I hope this is not such a stupid question 😀
Thanks in advance!
Your problem is that you’re removing the child (wall) from the parent, which then casuses the parent var to be null – see comments in code below:
Simply switching those two lines around should fix your main issue.
Also,
_userInterfaceis declared as a private var in your main class, so if you want to access it in your wall class (like you are trying to do), you’ll need to make it public or you’ll get another error.Something you may want to think about, is using events instead. That way, your code isn’t directly referencing other classes like the UI. separation of class dependencies makes testing and troubleshooting easier.
So something like this in your wall class:
Then in your UI Class: