Richardcsts Posted July 10, 2019 Share Posted July 10, 2019 Hoje eu estava aprendendo a mexer com a biblioteca Pixi.js, e estava recriando um joguinho que vi na playstore, e eu recebi um undefined mas não entendi o porquê. function Block(blockType_, x, y, id){ this.id = 0; this.blockType = blockType_ this.x = x this.y = y this.timeToBreak = new Date().getTime()-2000 //O undefined é desta variável this.hitpoints = 50; this.sprite = new PIXI.Sprite( PIXI.loader.resources[this.blockType.url].texture ) this.sprite.interactive = true; this.show = function(){ if(this.hitpoints > 0){ this.sprite.position.set(this.x, this.y) app.stage.addChild(this.sprite) } } this.sprite.on("mousedown", function(){ console.log(this.timeToBreak) //Recebi o undefined ao printar no console do navegador }) Se alguém poder me ajudar e me explicar, fico imensamente agradecido! ivan.popelyshev 1 Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted July 10, 2019 Share Posted July 10, 2019 this.sprite.on("mousedown", () => { console.log(this.timeToBreak) }) this.sprite.on("mousedown", (function(){ console.log(this.timeToBreak) }).bind(this)) this.sprite.on("mousedown", function(){ console.log(this.timeToBreak) }, this) Learn what means "bound function" in javascript. Welcome to our forum! Richardcsts 1 Quote Link to comment Share on other sites More sharing options...
Richardcsts Posted July 10, 2019 Author Share Posted July 10, 2019 9 minutes ago, ivan.popelyshev said: this.sprite.on("mousedown", () => { console.log(this.timeToBreak) }) this.sprite.on("mousedown", (function(){ console.log(this.timeToBreak) }).bind(this)) this.sprite.on("mousedown", function(){ console.log(this.timeToBreak) }, this) Learn what means "bound function" in javascript. Welcome to our forum! Thank you so much!!! ❤️ i love this forum ivan.popelyshev 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.