Jump to content

Extending Phaser.Sprite with coffee script


mirimCZ
 Share

Recommended Posts

Hi,

 

I'm new to Phaser, as a prove-of-concept and get-to-know-phaser for my final app (which will be pretty big) I'm building simple chess game. I'm stuck on extending Phaser.Sprite object with my own, I need to send some extra information with Sprite to onInputDown event. Maybe there is some other way around to do this, I dont insist on extending, if there is some other clean way to get information to event callback.

As mentioned I'm using coffee script (I dont insist on cs either, but I'm not going back to clean javascript). I have PieceFactory, that is creating Pieces, here is coffee script code, its realy nothing :)

Factory:

  drawPawn: (x, y, color)->    targetField = @board.getSquare x, y    pawn = new Piece(@game, targetField.positionX, targetField.positionY, color, 5)

Piece class:

class Piece extends Phaser.Sprite  constructor: (game, x, y, texture, frame)->    sprite = super game, x, y, texture, frame

This is Piece compiled to this javascript code:
 

var Piece,  __hasProp = {}.hasOwnProperty,  __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };Piece = (function(_super) {  __extends(Piece, _super);  function Piece(game, x, y, texture, frame) {    var sprite;    sprite = Piece.__super__.constructor.call(this, game, x, y, texture, frame);  }  return Piece;})(Phaser.Sprite);

When this code executes, nothing happens. No error codes, no renders, as it was never executed...

I would love to know how to get this extending working with coffee script. I would also appreciate "best practices" for passing data and/or functionality to event callbacks.

Thanks in advance, have a nice day.

 

 

Solution:

Extending was working ok, you need to append your Sprite instance to world / group / etc. Example given:
 

customSprite = new ExtendedSprite(100, 100, 'sprite_name')@game.world.addChild customSprite # @game is instance of Phaser.Game obvsly
Link to comment
Share on other sites

Well it seems that constructing extended object isnt enought and I need to add it some where, some how, maybe to a group or a world or another sprite as child?

What are the best practices for this? I dont only look for working solution, but for clear one as well.

 

Solution:

Extending was working ok, you need to append your Sprite instance to world / group / etc. Example given:
 

customSprite = new ExtendedSprite(100, 100, 'sprite_name')@game.world.addChild customSprite # @game is instance of Phaser.Game obvsly
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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