What is a super()

Really dumb question, really a beginner of openfl came from coding flash. what is a super()? what’s its purpose?

This is more of a Haxe than an OpenFL question. Check this Haxe Manual page: https://haxe.org/manual/types-class-inheritance.html

1 Like

It connects back to the super class. So if you are extending your class with a parent class like MovieClip, Sprite , EventDispatcher etc.

class  A extends MovieClip {

       function new () 

    {

    super() ; 

    }

    }

Then you must call super ( ) in the constructor ( new function ) . If you don’t extend your class, it’s not required.
:slight_smile:

1 Like

Just a friendly advice: To become a good programmer, learn to google.
99% of things you dont know did happen to people before you. Just typing the topic “What is a super()” into google gives you all the answers and it saves you and everyone elses time.

Thanks for asking and let us know how else we can help :smiley:

1 Like

Thank you! Any ideas how you can assign that class to an object, like lets say I’ll spawn a sprite of an enemy and I’ll assign it an “Enemy Class” with its own variables like health, attack, attributes. any idea how to do that?

I did

var enemysprite:Sprite

addChild(enemysprite)
enemySprite = EnemyClass()

enemySprite.health -= 1

Didn’t work, any help would be great.

in OpenFL you cannot directly attach a variable with sprite instance. I think it’s because like AS3 the sprite or movieclip class is not Dynamic.

You can use Reflect class to do something similar.
https://api.haxe.org/Reflect.html
The syntax is a bit longer but it serves a similar purpose:

Reflect.setProperty (enemySprite,"health",100 )

var health_Int:int = Reflect.getProperty (enemySprite,"health"  );

var newHealth_Int:int = Reflect.setProperty(enemySprite,"health",health_Int -1   );

It looks like a lot of code but I think that’s how it’s done in OpenFL to match AS3.

Another way can be creating a separate variable called enemySpriteHealth_Int . Or if you have got many enemies then make an array that maps with enemy instance array. Like this:

var enemyInstance_Arr_Sp:Array = new Array<Sprite>() ; 
var enemyHealth_Arr_Int:Array = new Array<Int>() ; // both array map to each other 
....
....

Its the other way around, you dont create a Sprite and then assign it to an Enemy, but you create an Enemy which is a Sprite.

class Enemy extends Sprite {
  public _health:Int;
   //...and all the rest like attack, other attributes and ofc the Sprite information itself

  function new(h:Int){
    super();
   _health = h;
   //...
}

At least its a easier solution.

Lol, this is the thread that googling brought me to.