Dang_Khoa Posted October 4, 2013 Share Posted October 4, 2013 I want to inheritance a Phaser.Sprite (spritesheet) and add the child object to the game state, how can I do that? For examle:var arrow = new Arrow(game); // Arrow inheritance Phaser.Sprite // x, y, key is already defined in Arrow objectgame.add.sprite(arrow) darcys22 1 Link to comment Share on other sites More sharing options...
xerver Posted October 4, 2013 Share Posted October 4, 2013 You are looking to add a pre-existing object: game.add.existing(arrow) Dang_Khoa and darcys22 2 Link to comment Share on other sites More sharing options...
Dang_Khoa Posted October 4, 2013 Author Share Posted October 4, 2013 You are looking to add a pre-existing object: game.add.existing(arrow)Thank you, rolnaaba, you solved one problem, the second one is I don't know how to inheritance Phaser.Sprite (spritesheet) Link to comment Share on other sites More sharing options...
rich Posted October 4, 2013 Share Posted October 4, 2013 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> darcys22 and Dang_Khoa 2 Link to comment Share on other sites More sharing options...
rich Posted October 4, 2013 Share Posted October 4, 2013 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> Dang_Khoa 1 Link to comment Share on other sites More sharing options...
Dang_Khoa Posted October 4, 2013 Author Share Posted October 4, 2013 Cool, it worked, thank you very much Link to comment Share on other sites More sharing options...
InsaneHero Posted May 21, 2014 Share Posted May 21, 2014 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 More sharing options...
odelia Posted December 1, 2015 Share Posted December 1, 2015 What if I want the inherited object to be added to a sprite? can I do that? Link to comment Share on other sites More sharing options...
Recommended Posts