Search the Community
Showing results for tags 'state variable'.
-
Hello everyone. I'm learning about extending classes so excuse me if I'm asking a silly question. What I want is to get a variable from the state and use it in the extending class constructor and functions. The state looks similar to this MyGame.Game = function(){ this.tileSize = 80; }; MyGame.Game.prototype = { create: function(){ for(var i = 0; i < 8; i++){ for(var j = 0; j < 8; j++){ var tile = new Tile(this, i, j); this.add.existing(tile); } } } }; Then, I've a extended class like this: Tile = function(game, column, row){ var posX = game.tileSize * column; var posY = -game.tileSize + (game.tileSize * row); Phaser.Sprite.call(this, game, posX, posY, "tile"); this.game = game; }; Tile.prototype = Object.create(Phaser.Sprite.prototype); Tile.prototype.constructor = Tile; It works fine, it creates the tilegrid and I'm able to use the "tileSize" value I've defined in the state function. The problem happens when I try to add some animations to the tile class. If I add the "this.animations.add('shine'...)" command in the constructor, I get an error (this.animations is not defined) I've searched in the forum and I've found a solution, instead of "var tile = new Tile(this, i, j)" I should use "var tile = new Tile(this.game, i, j)" That's correct, if I do that, I can add the animations, but then I cannot use the state variable "tileSize". Is there a way to do both things at the same time? Thanks a lot. Liranan.
- 3 replies
-
- extended class
- state variable
-
(and 1 more)
Tagged with: