Author Topic: Constructor vs Create Function  (Read 2005 times)

Shadow

  • Member
  • **
  • Posts: 9
  • Karma: +0/-0
    • View Profile
Constructor vs Create Function
« on: Mon, Mar 14, 2011 »
Hey,

In the template file I have they use constructors when creating the PlayState and MenuState. However, the example programs on flixel.org override the create function and don't use a constructor. Just wanted to know which method is proper or when and where to use each approach.

Thanks

cai

  • Contributor
  • ****
  • Posts: 465
  • Karma: +0/-0
  • the illest of villains
    • View Profile
    • Brandon Cash
Re: Constructor vs Create Function
« Reply #1 on: Mon, Mar 14, 2011 »
The proper method is to use create().  I've never really looked into why it's necessary, but I think it just delays creating things for a bit until everything has had time to instantiate.
Follow me on Twitter | Come join us at #flixel on irc.freenode.net!

Titch

  • Contributor
  • ****
  • Posts: 270
  • Karma: +0/-0
  • Thing with the guy in the place.
    • View Profile
Re: Constructor vs Create Function
« Reply #2 on: Mon, Mar 14, 2011 »
create() is called once the state has a valid stage reference, I believe. Also things written in the constructor aren't run through the JIT compiler so they run slower.
Free cake whippings every day at #flixel on irc.freenode.net.

ChainedLupine

  • Member
  • **
  • Posts: 91
  • Karma: +0/-0
    • View Profile
    • Dit Dah Games
Re: Constructor vs Create Function
« Reply #3 on: Mon, Mar 14, 2011 »
The only difference is that the constructor is only called right when you new the FlxState, where the create() event is called during the phase of FlxGame switching states.  (FlxGame::switchState)  Why is this a difference?

It really depends on how you to do things.  %99 of the time, you would probably never notice the difference, as you would do FlxG.state = new MyFlxState.  As long as you aren't trying to access parts of FlxGame that are in a transitory state while a FlxState is being loaded, you're fine.

But safest is to use FlxState::create(), always.

BTW Titch, I believe that isn't the case.  From the references I can find, it's the $init and $cinit code that is interpreted.  (See http://www.onflex.org/ACDS/AS3TuningInsideAVM2JIT.pdf, page 43.)   $init is called on load (ie: static initializations) and $cinit is called on Class definition (not instantiation) (ie: code that lives at the base class level).

photonstorm

  • Administrator
  • Key Contributor
  • *****
  • Posts: 1502
  • Karma: +1/-0
    • View Profile
    • Photon Storm
Re: Constructor vs Create Function
« Reply #4 on: Tue, Mar 15, 2011 »
Chained - I think Titch meant that flixel itself doesn't call create until a stage reference exists (rather than that is how the AVM works).

It's still not correct however, as there's no such checks in switchState :)

I'm not overly bothered about where things are placed. There's no technical REASON to put it into create() as it's just a blank dummy function for you to use. Flixel gives it no special treatment, or has any kind of hooks into it.

But logically it makes a lot of sense, and it's tidy. For small games I'd say it doesn't matter terribly, but it's certainly a good habit to get in to.
http://www.photonstorm.com



"Tell me and I will forget, show me and I might remember, involve me and I will understand" - Confucius

lnBetween

  • Member
  • **
  • Posts: 59
  • Karma: +0/-0
    • View Profile
Re: Constructor vs Create Function
« Reply #5 on: Tue, Mar 15, 2011 »
So lets say im in my MenuState and I want to switch to PlayState what code should I use?

cai

  • Contributor
  • ****
  • Posts: 465
  • Karma: +0/-0
  • the illest of villains
    • View Profile
    • Brandon Cash
Re: Constructor vs Create Function
« Reply #6 on: Tue, Mar 15, 2011 »
So lets say im in my MenuState and I want to switch to PlayState what code should I use?
I don't see how this really relates to the question in this thread.  If this was a separate question, another thread would have been more polite.
Anyway, you can switch states like this:
Code: [Select]
FlxG.state = new MenuState();
Follow me on Twitter | Come join us at #flixel on irc.freenode.net!

photonstorm

  • Administrator
  • Key Contributor
  • *****
  • Posts: 1502
  • Karma: +1/-0
    • View Profile
    • Photon Storm
Re: Constructor vs Create Function
« Reply #7 on: Tue, Mar 15, 2011 »
Heh, I think he replied here to another thread in which we were talking about when you need to use "new" before a class or not :)
http://www.photonstorm.com



"Tell me and I will forget, show me and I might remember, involve me and I will understand" - Confucius

lnBetween

  • Member
  • **
  • Posts: 59
  • Karma: +0/-0
    • View Profile
Re: Constructor vs Create Function
« Reply #8 on: Tue, Mar 15, 2011 »
Hehe sorry I got it all wrong... Thanks anyway! :)