Jump to content

Phaser OOP examples


ageibert
 Share

Recommended Posts

Hello, good point,

 

Here, in the examples of Phaser:

 

http://examples.phaser.io/

 

Examples 'extended sprite demo 1' and 'extended sprite demo 2' (in Sprite section) can guide you.

 

For example ('extended sprite demo 1'):

// Here is a custom game object (constructor)MonsterBunny = function (game, x, y, rotateSpeed) {Phaser.Sprite.call(this, game, x, y, 'bunny');this.rotateSpeed = rotateSpeed;};MonsterBunny.prototype = Object.create(Phaser.Sprite.prototype);MonsterBunny.prototype.constructor = MonsterBunny;/*** Automatically called by World.update*/MonsterBunny.prototype.update = function() {this.angle += this.rotateSpeed;};
You can initialize the attributes of the 'class' in the constructor, and place the corresponding actions in the function update (), also add the methods you need.
 
All this in a javascript file (here is your 'class' js), then from another file you can instantiate an object MonsterBunny with:
 
var mb = new MonsterBunny(game, x, y, z); (for example), and use this object in the World.update() function.
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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