Using of addChild() for Tilemap from another class [SOLVED]

Hi folks!

I’m working on OpenFL application and ran into a trouble. I need to create tilemaps dynamically and I created additional class for this functionality (the class extends Sprite class, but main class - not).

Unfortunately I found out that calling addChild() from a method of the additional class does nothing! Tilemaps are not added and I can’ see them. Could you please help me and explain how to use addChild method from another class?

I also tried following:

  1. Additional class doesn’t extend Sprite, main class extends Sprite. Send lambda with addChild() from main to additional class. Invoke this labda when I need to add tilemap from additional class. Nothing happened - tilemap is not showed.
  2. Additional class doesn’t extend Sprite, main class extends Sprite. Cast ‘this’ reference to DisplayObjectContainer and send from main class to additional class. Then call casted_this_from_main_class.addChild(). Nothing happened - tilemap is not showed.

With Google I found only old solution with using of reference to stage. It didn’t help me.

It would be better if you provide a sample code.

Ok, let me simplify the task…

I have to use addChild() for tilemap in main function, but it show nothing!
Below is a code:

package;

import openfl.Assets;
import openfl.display.Sprite;
import openfl.display.Tileset;
import openfl.geom.Rectangle;
import openfl.display.Tilemap;
import openfl.display.Tile;

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

    public static function main()
    {
        // I have to do this, because main() is a static method and
        // super() shall be called before I start using of addChild().
        var m = new Main();
        
        var bitmapData = Assets.getBitmapData("assets/temp.png");
        var tileset = new Tileset(bitmapData);
        tileset.addRect(new Rectangle( 0,  0, 32, 32));
        tileset.addRect(new Rectangle(32,  0, 32, 32));
        tileset.addRect(new Rectangle( 0, 32, 32, 32));
        tileset.addRect(new Rectangle(32, 32, 32, 32));
        var tilemap = new Tilemap(100, 100, tileset, false);

        tilemap.addTile(new Tile(0, 0, 0));
        tilemap.addTile(new Tile(1, 32, 0));
        tilemap.addTile(new Tile(2, 0, 32));
        tilemap.addTile(new Tile(3, 32, 32));

        // this addChild() do nothing - I don't see tilemap conent in an output area.
        // Without m. I can't call addChild(), because static method can't have 
        // access to non-static part of the class
        m.addChild(tilemap);
    }
}

How to operate addChild() correctly() ???

var m = new Main ();

This creates an instance of the class that does not remain referenced, and it is not even added to the Stage.

Thanks,
I’ve solved my issue. User GeoKureli from Haxe #openl chat explained what was wrong in my code.

Now I invoke non-static method from new() and pass ‘this’ reference to the method. With using of the reference, I call addChild method.

in class Main:

public function new()
{
    super();
    main(this);
}

public function main(ref: Main)
{
    ... creating of tileset, tilemap, adding tiles and so on... 
    ...
    ref.addChild(tilemap);
}

Next I’ going to put main logic (loop with preriodical adding of tilemaps to main()

…I forgot.
There is no problem to pass ‘this’ to a method of another class.
:+1: