mandarin Posted November 25, 2013 Share Posted November 25, 2013 Hi. I'd like to create a component that extends Phaser.Group so that I can add elements to it, and then add the entire component to anywhere in my game. But, it seems like I'm misunderstanding something. This is my code:GameName.Entity = function(game) { Phaser.Group.call(this, game);};GameName.Entity.prototype = Object.create(Phaser.Group.prototype);GameName.Entity.prototype.constructor = GameName.Entity;GameName.SomeStage = function(game) {};GameName.SomeStage.prototype = { var entity = new GameName.Entity(this.game); // Throws: Uncaught TypeError: Cannot set property '_iPrev' of undefined this.game.add.existing(entity); }};What am I doing wrong? Link to comment Share on other sites More sharing options...
rich Posted November 25, 2013 Share Posted November 25, 2013 At a guess I'd say this.game isn't defined (in your SomeStage object). Here's an example that I know works:window.onload = function() { // Here is a custom group // It will automatically create 30 sprites of the given image when created. MonsterGroup = function (game, image, action) { Phaser.Group.call(this, game); for (var i = 0; i < 30; i++) { var sprite = this.create(game.world.randomX, game.world.randomY, image); if (action == 'bounce') { game.add.tween(sprite).to({ y: sprite.y - 100 }, 2000, Phaser.Easing.Elastic.Out, true, 0, 1000, true); } else if (action == 'slide') { game.add.tween(sprite).to({ x: sprite.x + 200 }, 4000, Phaser.Easing.Elastic.Out, true, 0, 1000, true); } } }; MonsterGroup.prototype = Object.create(Phaser.Group.prototype); MonsterGroup.prototype.constructor = MonsterGroup; var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create }); var customGroup1; var customGroup2; function preload() { game.load.image('ufo', 'assets/sprites/ufo.png'); game.load.image('baddie', 'assets/sprites/space-baddie.png'); } function create() { customGroup1 = new MonsterGroup(game, 'ufo', 'bounce'); customGroup2 = new MonsterGroup(game, 'baddie', 'slide'); }}(); small fish and haden 2 Link to comment Share on other sites More sharing options...
mandarin Posted November 25, 2013 Author Share Posted November 25, 2013 That's it. Such a silly mistake. :-)Thanks! Link to comment Share on other sites More sharing options...
omarojo Posted July 3, 2015 Share Posted July 3, 2015 Is there a way to instanciate this new group subclass, but not add it to the game the moment is instanciated ? customGroup1 = new MonsterGroup(game, 'ufo', 'bounce'); That shouldnt add the group to the game.. until I say so. Is there a way to do this ? Link to comment Share on other sites More sharing options...
substandardgaussian Posted July 4, 2015 Share Posted July 4, 2015 Phaser's GameObjectCreator can do that. It's accessed throughgame.makeas opposed togame.add, and essentially functions identically except it doesn't automatically add whatever you made to the game world. EDIT: Just realized you were referring to a custom group, not a regular phaser group. When you do the Phaser.Group.call in the constructor, you have to make sure the second parameter to the Group constructor ("parent") is set to null. Otherwise the Phaser.Group constructor will add the group automatically. An 'undefined' parameter defaults to adding the group to the world, you have to explicitly pass "null". Link to comment Share on other sites More sharing options...
omarojo Posted July 7, 2015 Share Posted July 7, 2015 Phaser's GameObjectCreator can do that. It's accessed throughgame.makeas opposed togame.add, and essentially functions identically except it doesn't automatically add whatever you made to the game world. EDIT: Just realized you were referring to a custom group, not a regular phaser group. When you do the Phaser.Group.call in the constructor, you have to make sure the second parameter to the Group constructor ("parent") is set to null. Otherwise the Phaser.Group constructor will add the group automatically. An 'undefined' parameter defaults to adding the group to the world, you have to explicitly pass "null". Sorry I didn't undertand. So you mean ..Phaser.Group.call(this, null);My codeWarpGroup = function () { Phaser.Group.call(this, null); this.threads = []; for(var i= 0; i<9; i++){ this.threads[i] = this.create(i*30, 0, 'thread'); } };WarpGroup.prototype = Object.create(Phaser.Group.prototype);WarpGroup.prototype.constructor = WarpGroup;WarpGroup.prototype.doSomething = function(){ }EDIT: I got it... it was Phaser.Group.call(this, game, null); WarpGroup = function (game) { Phaser.Group.call(this, game, null);...In main game ...this.warp = new WarpGroup(this.game);this.game.add.existing(this.warp);Thanks for your support Link to comment Share on other sites More sharing options...
EvilB Posted August 11, 2015 Share Posted August 11, 2015 I have a related question... in omarojos example, is it possible to remove the custom group? I have a similar set-up, but removing the group with something like:this.game.destroy(this.warp);does not work for me. It might be a scoping issue in my case, but before I crack my head I'd like to be sure that removing a custom group is at least possible.All help appreciated! Link to comment Share on other sites More sharing options...
Recommended Posts