Jump to content

Phaser OOP examples


Recommended Posts

Hello, good point,


Here, in the examples of Phaser:




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

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.

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.


  • Recently Browsing   0 members

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