Jump to content

How to write a class that extends from Phaser.Sprite?


isfuturebright
 Share

Recommended Posts

I'm trying to make a Player class to have it's own update and stuff. How can I extend Phaser.Sprite to make it work?

TEKILA.Player= function(texture){    PIXI.Sprite.call( this );};TEKILA.Player.prototype = Object.create( PIXI.Sprite.prototype);//Extends sprite..TEKILA.Player.prototype.constructor = TEKILA.Player;

I'm trying to follow what I've seen in Phaser, but I've into some problems when trying to do a game.add later on, and also my js here doesn't seem to find PIXI. What's the right way to go about this?

Link to comment
Share on other sites

  • 5 months later...

I strongly recommend using CoffeeScript. Extending a Sprite in Phaser is a huge mess (no offense to the developer, it's javascript's fault). It's done correctly and works perfectly with CoffeeScript classes! (I'm actually using LiveScript, not CoffeeScript, but it's similar)


So you can write this:

class MonsterBunny extends Phaser.Sprite	(game, x, y, @rotateSpeed) -> super game, x, y, 'bunny'	update: -> @angle += @rotateSpeed

Instead of this:

//  Here is a custom game objectMonsterBunny = 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;};
Link to comment
Share on other sites

  • 8 months later...
  • 1 month later...
  • 1 month later...

I'd argue against Coffeescript at this point because of how soon we're going to be using ES6. Once jashkenas solves that problem, then Coffeescript ahoy – transpilers are great things and I like how little one has to type in CS.

 

But, right now, Coffee is using reserved words whose semantics are going to change when ES6 lands.

Link to comment
Share on other sites

  • 4 months later...

@rich I'm having trouble getting this to work using ES6 syntax. It doesn't seem like the update method is called automatically from my player class extending Phaser.Sprite, but I'm not really sure why. 

 

If I add something simple like: 

 

  update() {         
    console.log('player update');
  } 
 
I don't get any log messages at all. I also tried adding a super.update(); call before the log statement, but that didn't do anything either. The only way I can get update to work so far is by adding it to the update method in my level stage. 
 
class Level1 extends Phaser.State {
 
update() {        
let game = this.game; 
let player = this.game.players[0];  
player.update();
}
}
export default Level1;

but then I can't separate out my update functionality for specific class types, which will lead to very messy level files for each stage. Any ideas?
Link to comment
Share on other sites

@Skeptron Ah, I think I got it.

 

I had been adding my sprites to the scene prior to extending the Phaser.Sprite class, so now I had to change the initialization logic. I'm still not entirely sure why the old method doesn't work in this case, but I noticed the game.add.existing line in one of the sprite examples posted earlier on this thread and that fixed my issues.  

 

new: 

super(game, x, y, 'char5');
game.add.existing(this);
game.physics.p2.enable(this);    
game.camera.follow(this)
 
old:
this.sprite = this.game.add.sprite(x, y, 'char'5);
game.physics.p2.enable(this.sprite);
game.camera.follow(this.sprite);
Link to comment
Share on other sites

  • 11 months later...

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...