Phyron Posted September 1, 2014 Share Posted September 1, 2014 Hi, I have a code that you can see full code in this topic: http://www.html5gamedevs.com/topic/8876-delete-sprite-in-group/ I have a object BasicGame, with propieties BasicGame.Game = function (game) {this.powerIcons;this.ballLimit = 5;}BasicGame.Game.prototype = {crearNave: function () { this.naveY = this.game.world.height-50; this.nave = new NaveSprite( this.game,this.game.world.width/2, this.naveY); }} Nave is a new object. var NaveSprite = function(game, _x, _y){ Phaser.Sprite.call(this, game, _x, _y, 'nave',1); this.game.physics.enable(this, Phaser.Physics.ARCADE); this.enableBody = true; this.body.immovable = true; this.game.add.existing(this); this.naveY = _y;}NaveSprite.prototype = Object.create(Phaser.Sprite.prototype);NaveSprite.prototype.constructor = NaveSprite; NaveSprite.prototype.update = function() { this.naveY = ballLimit; //HERE I have to access the "ballLimit" var }I need to acces a var of his parent (ballLimit), this is the problem. I log window.BasicGame and console say:BasicGame: Object Boot: function (game) { Game: function (game) { arguments: null caller: null length: 1 name: "" prototype: Object applypower: function (){I find applypower and other prototyped methods of basicgame, but cant find a propieties. console.log(window.BasicGame.Game) returned the function, no the vars. Link to comment Share on other sites More sharing options...
lewster32 Posted September 1, 2014 Share Posted September 1, 2014 This is a common source of confusion when working with object oriented style code in the early stages. There are several ways to do this, including making globally accessible properties, however that method is frowned upon. A better way to do this is to simply pass the parent object to the child objects (this is how Phaser makes the 'game' object available in many of its child objects) like so:crearNave: function () { this.naveY = this.game.world.height-50; // Note the second argument is the state itself this.nave = new NaveSprite(this.game, this, this.game.world.width/2, this.naveY);}Then in your NaveSprite object:var NaveSprite = function(game, state, _x, _y){ Phaser.Sprite.call(this, game, _x, _y, 'nave',1); // Store a reference to the state that created this object here - this // is not a copy, but a direct reference - if things change on the state // they change in this property too. this.state = state; this.game.physics.enable(this, Phaser.Physics.ARCADE); this.enableBody = true; this.body.immovable = true; this.game.add.existing(this); this.naveY = _y;}NaveSprite.prototype = Object.create(Phaser.Sprite.prototype);NaveSprite.prototype.constructor = NaveSprite; NaveSprite.prototype.update = function() { this.naveY = this.state.ballLimit;}Another way to do this would be to store the ballLimit property on the NaveSprite object as a 'static' like so:var NaveSprite = function(game, _x, _y){ Phaser.Sprite.call(this, game, _x, _y, 'nave',1); this.game.physics.enable(this, Phaser.Physics.ARCADE); this.enableBody = true; this.body.immovable = true; this.game.add.existing(this); this.naveY = _y;}NaveSprite.prototype = Object.create(Phaser.Sprite.prototype);NaveSprite.prototype.constructor = NaveSprite;// 'static' property, accessible globally but attached to the object it refers toNaveSprite.ballLimit = 5; NaveSprite.prototype.update = function() { this.naveY = NaveSprite.ballLimit;}With this method you can access and alter NaveSprite.ballLimit anywhere in your code. Link to comment Share on other sites More sharing options...
Phyron Posted September 1, 2014 Author Share Posted September 1, 2014 THANKS! Another problem I've found is with timer event. This is an example:if(power == 1){this.nave++;this.game.time.events.add(Phaser.Timer.SECOND * 4, this.deletePower, this);}if(power==2){this.nave--;this.game.time.events.add(Phaser.Timer.SECOND * 4, this.deletePower, this);}this.deletePower has 2 identical if's: if power= 1 this.nave++, for restore state. I need to pass a value to the deletePower function to know which destroy. this.deletePower(power) send me a error. Link to comment Share on other sites More sharing options...
lewster32 Posted September 1, 2014 Share Posted September 1, 2014 Anonymous functions help here:this.game.time.events.add(Phaser.Timer.SECOND * 4, function() { this.deletePower(power);}, this); Link to comment Share on other sites More sharing options...
Phyron Posted September 1, 2014 Author Share Posted September 1, 2014 Good idea lewster32 1 Link to comment Share on other sites More sharing options...
Recommended Posts