Object/Sprite/MovieClip Referencing

I am in need of a little help with the latest version of Haxe/openFL.

When targeting a movie clip in openFL from a function I get the error unknown identifier. An example:

addChild(parentMC);

parentMC.addChild(childMC);

private function someFunction():Void{
trace(parentMC.childMC.x);
}

output:
Source/Main.hx:263: characters 2-18 : Unknown identifier : parentMC

I have also tried calling the child directly like so

childMC.x

but get the same type of error.

How do I get the property the child MC or even the parent from within a function?

using hate version:
3.2.1
Libraries:
actuate: [1.8.6]
air3: [0.0.1]
box2d: [1.2.3]
flambe: [4.1.0]
format: [3.2.1]
haxeui: [1.8.18]
hscript: [2.0.7]
hvm: [0.0.2]
hxcpp: [3.2.205]
hxsl: [2.0.5]
layout: [1.2.1]
lime-samples: [2.6.0]
lime: [2.9.1]
openfl-samples: [3.3.1]
openfl: [3.6.1]
swf: [2.2.0]

There are three types of variables: static, instance, and local.

class Main {
    //Static variable.
    public static var firstVar:Float = 1;
    
    //Instance variable.
    public var secondVar:Float = 2;
    
    public function new() {
        //Local variable.
         var thirdVar:Float = 3;
         
         //You can access a static variable anywhere.
         trace(firstVar);
         
         //You can access an instance variable from any
         //non-static function in the class.
         trace(secondVar);
         
         //You can access a local variable in the same function.
         trace(thirdVar);
    }
    
    public function exampleFunction():Void {
        //You can still access static and instance variables.
        trace(firstVar);
        trace(secondVar);
        
        //You CANNOT access local variables from a different function.
        //This produces an error:
        trace(thirdVar);
    }
}

If you want to “remember” a local variable, turn it into an instance variable:

class Main {
    private var thirdVar:Float;
    
    public function new() {
        //You can still access it from the constructor.
        thirdVar = 3;
    }
    
    public function exampleFunction():Void {
        //Now you can access it from this function too, because
        //it's an instance variable.
        trace(thirdVar);
    }
}

Hi, thanks for the response but my issue is not getting a variable value but getting the properties of a movie clip from a function.

Back in actionscript I could get the the width,height, x position, y position by doing something like this:

trace(root.parentMC.childMC.x);

In openfl/haxe this is not the case. Documentation says to get a movie/sprite properties I have to do somethins like this:

getChildByName("childMC");

but this is returning a null value. How would I get these properties from a movie clip in stage/root from a function.

class Main extends Sprite {
    public function new () {
                  super ();

                    var background = Assets.getBitmapData("assets/bg-scene.jpg");
    		var bitmap = new Bitmap (background);
    		var backgroundsMC: MovieClip = new MovieClip();
                
                 addChild(backgroundMC);
    }
   
   private function simplefunction():Void{
           trace(backgroundsprite.width);       
           //this is where I am stuck I cannot get this property no matter how I write this code.
   }    

}

Update:

trace(this.backgroundMC.getChildAt(1));

trace(this.backgroundMC.getChildAt(1));

Both return

Source/Main.hx:125: characters 8-40 : openfl.display.DisplayObject cannot be called

childMC is not a property of parentMC, so ‘.’ is never going to work.

But you have to define your var outside of the ‘new’ function. As soon as it’s a class variable, you can access it in all your functions in this class. At the moment it’s local to function new…

This is exactly what I was telling you about. You’re creating a local variable (backgroundMC), and then trying to access it from a different function. If you want to access it, turn it into an instance variable:

class Main extends Sprite {
    private var backgroundMC:MovieClip;
    
    public function new () {
        super ();
        
        var background = Assets.getBitmapData("assets/bg-scene.jpg");
        var bitmap = new Bitmap (background);
        backgroundMC = new MovieClip();
        
        addChild(backgroundMC);
    }
    
    private function simplefunction():Void{
        trace(backgroundMC.width);
    }    
    
}
2 Likes

Thanks a lot for the info! You finally gave me the “a ha!” moment. I was approaching this from an actionscript 3 way of doing things. This is exactly the info I needed to make my app work! Thanks again for the info!