Jump to content

onInputDown passing incorrect sprite


Recommended Posts

Hi all,

I'm having a problem with input events on my sprites.
I have a grid of tiles, with an added child sprite. I've added an onInputDown event handler onto each tile.

All works well, mostly... but then after a few clicks it seems that the sprite passed to my handler doesn't change - it remains the last sprite clicked.

Here's some code:

var pos = {
	x: 0,
	y: 0
var cols 			= 10;
var rows 			= 10;
var numberOfTiles 		= cols * rows;
var tileWidth 			= 65;
var tileHeight 			= 65;

// Place tiles
for (var i = 1; i <= numberOfTiles; i++) {
	var thisTile		= this.game.add.sprite(pos.x, pos.y, 'tile', 1);
	var thisSprite 		= thisTile.addChild(this.game.make.sprite(0,0, 'tile', 4));
	thisTile.inputEnabled 	= true;
	thisTile.events.onInputDown.add(this.onDown, this);
	pos.x += tileWidth;

	// New column
	if (i % cols === 0) {
		pos.y += tileHeight;
		pos.x = 0;

This sets out the tiles with no problems. Frame 1 for my background and frame 4 for the foreground.

In my onDown function I tween the child sprite down to scale: 0 and alpha: 0.
I also do a bit of calculation to work out which tile is clicked.

onDown: function(sprite) {
  var thisRow = (sprite.position.y / tileHeight);
  var thisCol = (sprite.position.x / tileWidth);
  var thisTile = ((thisRow * rows) + thisCol) + 1;

  this.game.add.tween(sprite.children[0].scale).to({ x: 0, y: 0}, 500, Phaser.Easing.Linear.None, true);
  this.game.add.tween(sprite.children[0]).to({alpha: 0}, 500, Phaser.Easing.Linear.None, true);

It seems this works a few times, but then seemingly at random the sprite being passed to the onDown function remains stuck regardless of which sprite I click.

I can't be 100% sure, but I think this problem only occurs when I have a child sprite - does that sound right?
I've no clue what's going on. Can anyone help?

Edited by GaryS
Code update
Link to comment
Share on other sites

is the tween being added to the game? It might be a context issue but I can't tell

thisTile.events.onInputDown.add(this.onDown, this.game);

is thisSprite getting input enabled by mistake? What logs if you put console.log(sprite.parent) in your onDown function? Can you declare var thisSprite after the event handler to see if that makes a difference?

Link to comment
Share on other sites

Yes, the tween is added to this.game - Sorry, I've updated the code to reflect this.

It's definitely the parent sprite (thisTile) that is getting input enabled. If I log the parent, I get what looks to be an instance of the game world (name: __world).

I've tried moving the var thisSprite declaration, to no avail.

I'm not sure about child sprites and the like, but it certainly seems like this issue only occurrs when I'm using tweens... if I remove the tween, I can no longer replicate the issue.

I don't understand a great deal about how tweens work - perhaps I'm doing something wrong in that regard?

Link to comment
Share on other sites

...Actually, on further testing it seems the issue only occurrs when I use the scale tween... if I remove that and leave the alpha tween, I can no longer replicate the problem.

Perhaps something about scaling the child sprite down to zero is changing the way the parent's input handler is working?

Link to comment
Share on other sites


  • Recently Browsing   0 members

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