FriarBabs Posted November 11, 2015 Share Posted November 11, 2015 Can the 'create' function in Phaser.Group be successfully over-ridden? Is there an alternative? I am looking for a way to have a function called at the start of my object's life-cycle and still be able to access the functions I have built into the class. I am used to Unity3D where there is always an option to do this. There is certain code that I need to execute at the start of my object's life-cycle, and additionally whenever the level ID is set in my function. Am I being clear, or have I worded this too confusingly? Thanks much!FriarDude.FallingShitFactory = function (game) { // Class variables this.levelID = 0; this.active = false; this.spawnTimer = null; this.percentShitQuantities = new Array(); this.HORIZONTAL_MARGIN = 64; // // The constructor can take an optional parameter (levelID, 0 by default) if (arguments.length == 2) { this.levelID = arguments[1]; } // // Constructor Phaser.Group.call(this, game); game.time.events.loop(1000, this.spawnShit, this); console.log('construct'); return this; //};FriarDude.FallingShitFactory.prototype = Object.create(Phaser.Group.prototype);FriarDude.FallingShitFactory.prototype.constructor = FriarDude.FallingShitFactory;FriarDude.FallingShitFactory.prototype.create = function() { // Load Level Data console.log('create'); this.setLevelID(this.levelID); //} Link to comment Share on other sites More sharing options...
jmp909 Posted November 11, 2015 Share Posted November 11, 2015 try thishttp://jsfiddle.net/9kg0w8xu/3/ Blah.CustomGroup.prototype.create = function(x,y,key, frame, exists) { // call group create Phaser.Group.prototype.create.call(this, x, y, key, frame, exists) this.setLevelID(this.levelID);} Link to comment Share on other sites More sharing options...
FriarBabs Posted November 12, 2015 Author Share Posted November 12, 2015 This is the correct answer for the info that I provided. I was a real dummy and didn't realize that I wasn't actually creating an object in my group, which activates the 'create' method for the group. Is there a method that is called when the Group itself is added to the scene? EDIT: I fixed my issue. 'Constructors' in JS's prototypical inheritance pattern are fake, methods within the prototype can still be accessed from within the constructor. That was my overall goal. Link to comment Share on other sites More sharing options...
jmp909 Posted November 12, 2015 Share Posted November 12, 2015 I'm not sure there's a onAdded / onAddedToWorld type of event . You could feature request it I guess , if you can make a suitable use caseBut is there any case where you don't control the add and therefore couldn't just call the required function after it ?http://phaser.io/docs/2.4.4/Phaser.Events.html Link to comment Share on other sites More sharing options...
Recommended Posts