hakura88

help with phaser engine, render, update etc.

Recommended Posts

Hello Forum

I have started to creating a game with phaser.io . I followed the tutorial from lessmilk. Now I have a basic question. 

In the tutorial he use this method: 

// Create our 'main' state that will contain the game
var mainState = {
    preload: function() { 
        // This function will be executed at the beginning     
        // That's where we load the images and sounds 
    },

    create: function() { 
        // This function is called after the preload function     
        // Here we set up the game, display sprites, etc.  
    },

    update: function() {
        // This function is called 60 times per second    
        // It contains the game's logic   
    },
};

// Initialize Phaser, and create a 400px by 490px game
var game = new Phaser.Game(400, 490);

// Add the 'mainState' and call it 'main'
game.state.add('main', mainState); 

// Start the state to actually start the game
game.state.start('main');

But I also see in other projects or examples like in the phaser.io examples something like this:

var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, render: render });

function preload() {

}

function create() {

}

function render() {

}

 

Now I want to know what I should take or learn better. I think the first one is an older version of phaser.io? Is there some issues with the old one, what is better? Or can I make it like I want at the end?

Thank you.

Share this post


Link to post
Share on other sites

The difference of the two examples is that the first examples uses states and the second one does not use states.

If you only have one state it does not really matter wich example you chose, but when you add multiple states you will need the first example. You can then create multiple states (for example one for the menu and two levels).

This makes it also very easy to break up the code into multiple files, so that you have a seperate file for every state.

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.