Jump to content

Make one function for all the states


AbdSab
 Share

Recommended Posts

Hi all, so i'm new in phaser, i've started learning it by making a project, so i have a problem :

 

I have more than one level for example 10 levels each one has it's own state, so the problem i don't want to make every time the same update function and other functions for every state.

 

So is there any way to make one function for all the 10 state ?

 

And thank's :)

Link to comment
Share on other sites

Use object inheritance using Object.create http://blog.etianen.com/blog/2013/05/26/javascript-inheritance/. or use mixins, decorators etc, Phaser has no built in solution for this, you need to roll your own. Also check out my post about structuring States http://www.sitepoint.com/javascript-game-programming-using-phaser/.

Link to comment
Share on other sites

I had the same problem and did also choose the inheritance method.

Here's some code that could help:

 

The main Level "class" all Levels inherit from:

 

https://github.com/geibi/phaser/blob/master/js/Level.js

 

There i got some "parentCreate" and "parentUpdate" etc. function which are called by the child Level classes e.g.

https://github.com/geibi/phaser/blob/master/js/Levels/Level_2.js

 

Here you see e.g. in line 21

BasicGame.Level_2.prototype.update = function () {  this.parentUpdate();

and after the update, all other functions for this specific level are called

 

hope this helps :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...