Doing math on stageHeight/Width

Hello all. I’ve been using OpenFL/Haxe for approximately 1 day :slight_smile: But I’m experienced with Flash/AS3 and so getting up to speed has been no problem. But I have encountered an issue when trying to use the stageHeight or width. Every time I try to divide, or multiply with them, I get a crash. I suspect this might be a stupid question but I’m a bit confused as to how the math in Haxe works, and why it only seems to happen with these values. I discovered that I am apparently supposed to use Std.int while doing these operations, and while that works fine if I use a flat number, when attempting to divide or multiply the stageHeight/Width, it simply crashes.

Any help would be appreciated.

Hi there. How are you accessing stageWidht/Height property? by this.stage.stageWidth or Lib.stage.stageWidth? It could be related to this.stage being null if the object is not added to displaylist.
And which kind of error do you get in debug? You only mentioned that it’s crashing (which target?). In flash you usually get a runtime error during the crash. Be good to check that.

1 Like

Ah… figured it out. Turns out it was because it was returning null…(I swear I did a trace to make sure it had a value but… I guess I’m just crazy)
Your mention of the displaylist made me check if I could do it in the main file rather than in a class, and voila, it was fine. I was writing in the constructor(right term for Haxe?) which I presume means it’s attempting to access the stage before being added to it and thus can’t find it. I hooked the it up to an ADDED_TO_STAGE event and all is well :sweat_smile::man_facepalming:

That’s what i thought. Glad you’ve figured it out. :wink::+1:

Doing stage.stageWidth in the constructor of the document class is okay, but other classes do need to wait until after they are added to the Stage :slight_smile: