Jump to content

Proper Sprite Button Events management


ccgbagnet
 Share

Recommended Posts

I've just started learning pixi.js 2 days ago and I'm stuck with this.

I'm trying to register mouse button events on 3 different buttons which I generate this way.

function initializeButtons(bg: PIXI.Graphics) {
	let xPos = (canvasWidthHeight/2) - 130;
	for(let i = 0; i < 3; i++) {
		let temp = PIXI.Sprite.fromImage(BOX_SPRITES[0]);
		temp.scale.set(0.5);
		temp.anchor.set(0.5, 0.5);

		temp.position.x = xPos;
		temp.position.y = canvasWidthHeight / 2;

		temp.interactive = true;
		temp.buttonMode = true;

		temp.on('pointerdown', function() {onClick(i);} );


		rewardBoxes.push(temp);
		bg.addChild(temp);

		xPos += 120;
	}
}

However, everytime I try to click on any button, it's always the last button that gets clicked. What is the proper way to achieve this? Thank you!

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...