Naushika Posted July 23, 2015 Share Posted July 23, 2015 Hi! I am trying to do a new plugin but I don't know why this piece of code doesn't work. I reduced my real code to a basic case,so it will be easy to understand. I have a problem with the functions setTexture and setColor. Although I pass as a parameter the object that I want to modify, when the function returns the object is inmutable, the state is the same that it was before calling the function. For example "this.bitmap" is an empty object after call "setTexture()", but I worked over him inside the function. Someone can help me with this? I think maybe it will be a scope problem but I can't find an explanation.You can see the working code in the following fiddle: https://jsfiddle.net/p78bzn9w/5/ . In the console you can also view the behaviour of the values that I described or you can see the code here: Phaser.Plugin.Virtual = function(game, parent) { Phaser.Plugin.call(this, game, parent); this.game = game; }; Phaser.Plugin.Virtual.prototype = Object.create(Phaser.Plugin.prototype); Phaser.Plugin.Virtual.prototype.constructor = Phaser.Plugin.Virtual; Phaser.Plugin.Virtual.prototype.setup = function (player, buttons){ this.bitmap = {}; //here this.bitmap is {}, I pass as the first parameter this.setTexture(this.bitmap, '#4BAFE3', 30, 30); //after return setTexture, this.bitmap is {} again but it would be a bitmap this.arrow = {}; //here this.arrow is {}, I pass as the first parameter too this.setColor(this.arrow, 10, 20, this.bitmap , 0.5, 90); //the same problem as before, this.arrow is {} but it would be a button } Phaser.Plugin.Virtual.prototype.setTexture = function (item, color, width, height){ //I create a bitmap in item //item is this.bitmap var item = this.game.add.bitmapData(width, height); item.ctx.fillStyle = color; item.ctx.fillRect(0,0,width, height); //here item is a bitmap }; Phaser.Plugin.Virtual.prototype.setColor = function (item, x, y, bitmap, alpha){ //I create a button in item //item is "this.arrow". item = this.game.add.button( x, y, bitmap); item.alpha = alpha; item.fixedToCamera = true; //here item is a button };Thanks so much! Link to comment Share on other sites More sharing options...
Recommended Posts