Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8570899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:39:49+00:00 2026-06-11T18:39:49+00:00

I am trying to program a little game but stumbled upon some problems. I

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:39:51+00:00Added an answer on June 11, 2026 at 6:39 pm

    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:

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

    Simply switching those two lines around should fix your main issue.

    Also, _userInterface is 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:

    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
    }
    

    Then in your UI Class:

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm currently (trying) to program a little game. And i have a small design/deadlock
im trying to programming a little game. So and i have a little D-Pad
I'm trying to make a little program with Qt. I have a main.cpp with
I am trying to make a little game in C#. The program asks the
I'm trying to program a little game for Android, and when I draw the
I am trying to make a little log in program. But i am not
I'm trying to get a little program to send hex over RS232. From what
I'm trying to build a little renaming program to help save me time in
Okay so I'm trying to make a little gag program that will run away
I am trying to do the design of a Bejeweled game. I have basically

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.