Jump to content

Javascript - #PixiJs Error Undefined


Richardcsts
 Share

Recommended Posts

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!

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...