Novice issue: Referencing class variables for a movieclip

Hey guys. It’s been a while since I’ve used Flash and I am still trying to wrap my head around Actionscript 3.0 and the way it works; I am so used to the way AS2 works.

I have provided the code below, it’s very simple, so you might be able to skip this explanation and just take a look at the error and the code:

  • I have made a movieclip called HeroMC in my FLA.

  • The HeroMC movieclip has the AS Class linkage of exactly the same name, “HeroMC”.

  • I have made an actionscript class file, HeroMC.as, to house specific variables for my hero; such as Name, Age, xPos and yPos.

  • I have also made an actionscript class file, Main.as, the document class for this FLA.

  • In Main.as I instantiate HeroMC using the variable hero, and then run the function addHero() from my constructor.

  • addHero() is a simple stage.addChild(hero);

This all works perfectly fine and it adds my hero to the stage. However, the problem arises whenever I try to reference one of the variables from HeroMC.as. I am met with this error - I will use the variable xPos as an example.

Here is the error:

1119: Access of possibly undefined property xPos through a reference with static type HeroMC.

Here is the code for Main.as:

package  {
    import flash.display.*;
    import flash.events.*;
    
    public class Main extends MovieClip{
            
        // Create the hero
        public var hero:HeroMC = new HeroMC();


        //Constructor
        public function Main() 
        {
            //Add the hero
            addHero();
        }
        
        //Adds the hero to the game
        public function addHero():void
        {
            // Add hero object to stage
            stage.addChild(hero);
            
            hero.x = hero.xPos;
            hero.y = hero.yPos;
        }


    }
}



Here is the code for HeroMC.as:

package  {    
    import flash.display.MovieClip;
    import flash.events.Event;
    
    public class HeroMC extends MovieClip {


        public function HeroMC() {
            // Hero Name
            var HeroName = "Hero";
            // Hero Age
            var HeroAge = 20;
            // X and Y position
            var xPos = 300;
            var yPos = 300;
        }


    }
}



It is obvious this is a very simple problem, but I have searched Google, Kirupa and Flashkit for an answer, but none of the threads I find discuss my specific circumstances and I am unable to find the specific information I need to. If you could help me out it would be most appreciated. If you require any more information please let me know and I will do so.

Thanks in advance for taking the time to look at my issue.

Brendan