Jump to content

Sprite Inheritance?


Dang_Khoa
 Share

Recommended Posts

Here like this:

<script type="text/javascript">//  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;};(function () {    var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create });    function preload() {        game.load.image('bunny', 'assets/sprites/bunny.png');    }    function create() {        var wabbit = new MonsterBunny(game, 200, 300, 1);        wabbit.anchor.setTo(0.5, 0.5);        var wabbit2 = new MonsterBunny(game, 600, 300, 0.5);        wabbit2.anchor.setTo(0.5, 0.5);        game.add.existing(wabbit);        game.add.existing(wabbit2);    }})();</script>
Link to comment
Share on other sites

And here is another example. In this case you can see that MonsterBunny is responsible for setting its own random x/y location and scale. It also adds itself to the game world as soon as it is created.

<script type="text/javascript">MonsterBunny = function (game, rotateSpeed) {    //  We call the Phaser.Sprite passing in the game reference    //  We're giving it a random X/Y position here, just for the sake of this demo - you could also pass the x/y in the constructor    Phaser.Sprite.call(this, game, game.world.randomX, game.world.randomY, 'bunny');    this.anchor.setTo(0.5, 0.5);    this.rotateSpeed = rotateSpeed;    var randomScale = 0.1 + Math.random();    this.scale.setTo(randomScale, randomScale)    game.add.existing(this);};MonsterBunny.prototype = Object.create(Phaser.Sprite.prototype);MonsterBunny.prototype.constructor = MonsterBunny;MonsterBunny.prototype.update = function() {    //  Automatically called by World.update    this.angle += this.rotateSpeed;};(function () {    var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create });    function preload() {        game.load.image('bunny', 'assets/sprites/bunny.png');    }    function create() {        for (var i = 0.1; i < 2; i += 0.1)        {            new MonsterBunny(game, i);        }    }})();</script>
Link to comment
Share on other sites

  • 7 months later...

Is this broken in current Main branch?  I'm getting "error: undefined is not a function" on line 1719 of Phaser.js this.onTextureUpdate() from this code:

Person = function(game) {    Phaser.Sprite.call(this, game, 0, 0, 'person');};Person.prototype = Object.create(Phaser.Sprite.prototype);Person.prototype.constructor = Person;

invoked by:

 

var person = new Person(game);

 

EDIT: I just stepped through and 'this' on line 1719 of Phaser.js is a 'Person'... I notice on previous calls it was Phaser.Sprite.

EDIT2: the reason I want to do it this way is I want to make a Group from Person (to simplify collisions) and it needs to be a display object to work with Group

Link to comment
Share on other sites

  • 1 year later...
 Share

  • Recently Browsing   0 members

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