Static function main - newbie question -

Hi,
I am totally new to HaXe and OpenFl (2 days) and I have just studied the Pong tutorial: everything is ok, everything works, but I don’t understand what public static function main is: it is evident that it is called somewhere because the ScaleMode specified in it is set, but where?

ok, now you can start laughing :wink:
I suppose it is called by default at startup, but I haven’t read anything about it yet.

thanks

It is indeed called at startup, it’s the main entry function where your program start.

The Pong tutorial starts like this

public function new() {
	super();	
	addEventListener(Event.ADDED_TO_STAGE, added);
}

function added(event) {
	removeEventListener(Event.ADDED_TO_STAGE, added);
	stage.addEventListener(Event.RESIZE, resize);
	init();
}

function resize(event) {
	if (!inited) init();
	// else (resize or orientation change)
}

public static function main() {
	// static entry point
	Lib.current.stage.align = flash.display.StageAlign.TOP_LEFT;
	Lib.current.stage.scaleMode = flash.display.StageScaleMode.SHOW_ALL;
	Lib.current.addChild(new Main());
	//
}

function init() {
	if (inited) return;
	inited = true;
	
	// code
}

Well, the turorial suggests to start coding from init, it sounds like a different approach.
When is main effectively executed? Immediately after constructor execution (possibily before ADDED_TO_STAGE event) or after a particular event?
When you call the constructor what is the exact sequence of operations/events that occurs by default before the code starts executing user code?
Is there a diagram somewhere?

public static function main() is called by the executable, it’s the first thing that will be called.
In it you have new Main() which call public function new().
Then an even listener is added to know when your app is added to the stage, when that’s the case the event callback function, here function added(event) is called.
And finally this function call function init() where everything is ready.
So yeah adding code ther (after // code) would be the best place.

There’s a long history to this template code :smile:

In standard Flash development, there is no “static main”, but in Haxe (outside of OpenFL) there is.

OpenFL supports a standard “public new” document class, with no “static main”, however, if you do define one, it will prefer it as the starting point rather than creating one for you.

On the iOS target, iOS will initialize first at the “non-retina” size before resizing to the “retina” size for the target device. Some people use the ADDED_TO_STAGE/RESIZE formula (like above) to try and wait for the latter resolution. However, if you properly handle resize events in your application, this is unnecessary

Ok, but when? If I put code in init and in main, which one is executed first? Oh, is there Event.INIT in OpenFl like in AS3?

In this case there are both init and main functions, the question is the same: which one is executed first? Do you mean that main executes code before any other function?

Based on the code you posted, things happen in this order:

  1. main()
  2. new()
  3. added()
  4. init()

However, you could get the same results by moving your code into the new() function and deleting all the others. None of that stuff is necessary in recent versions of OpenFL.

Main is called first, it executes before any other functions, and then, because you create a new Main object, init gets called, by you.

main is executed before constructor?! I thought it couldn’t be possible. Well, in this case main instances the class itself (Lib.current.addChild(new Main());), it is pretty weird, as the document class should be instanced already.

You mean the constructor handles automatically all ADDED_TO_STAGE, INIT and RESIZE stuff for iOS too?

You might be thinking of how document classes are handled in Flash Pro. This is normal for Haxe.

Waiting for a RESIZE event on iOS is not strictly necessary, as long as you handle the event yourself.

And the point of waiting for ADDED_TO_STAGE is to ensure that stage isn’t null, but OpenFL sets stage before new() is called.

This might help, this comes from the default OpenFL boot code:

If you have a static “main” method, we call that as your entry point. Whether you create anything or add anything onto the stage is up to you. This is more like the way that standard Haxe/Flash projects work.

However, if you do not, then we create an instance of your main class. I extend it with a class I call “DocumentClass”, which is able to make sure that it’s on the stage before it ever calls the “new()” constructor (or at least make sure that stage is defined) so that you can call the stage property immediately, just like in Flash. This is the way that ActionScript projects traditionally work, and how I write all the samples. The static main is there mostly for existing Haxe/Flash project compatibility

Oh, ok, that piece of code is very explicative :wink:
Thank you guys!

May be the Pong tutorial should be updated to explain the main purpose to AS3 users.

or avoid it entirely. I would like to write more tutorials myself to avoid these kinds of speed bumps :wink:

Yes, but it is usefull to know what it is.
About tutorials, I haven’t searched tutorials a lot yet, but I have found a lot of comments: people say HaXe and OpenFl are very promising projects, but they lack a good support and valuable samples and tutorials, maybe because the project is pretty young. Tutorials are always a plus :smile:

Those comments probably occurred before the “learn” section existed (or docs.openfl.org), it isn’t perfect, but working on making better :slight_smile:

I know, and they did, it is a big problem of the net, TRASH MEMORY: you search the web and find a lot of resources, often without a date, to discover later that they are old and useless, and nobody cares to remove them. If you don’t care for the time when the resources have been created you can develope a wrong idea of a particular argument, software, item in general, and HaXe is also vulnerable, as I have found negative comments about it, identical to 2 years old ones.