Jump to content

Problem with sprite.events.onInputDown


Binary Moon
 Share

Recommended Posts

I have a grid of sprites that I am using for a menu in my current project. I've set up click, over, and out events and the over and out events work fine but the click ones aren't registering. Does anyone have any idea what I might be doing wrong?

 

I've added a simplified version of my code below.

function icon_click() { console.log('click'); }function icon_over() { console.log('over'); }function icon_out() { console.log('out'); }function create() {	var list_icons = game.add.group();	var x_position = 0;	var y_position = 0;	for( var x = 0; x < 12; x ++ ) {		for( var y = 0; y < 8; y ++ ) {			x_position = ( x * 74 ) - ( 74 / 2 );			y_position = ( y * 63 ) + 3;			var sprite = game.add.sprite( x_position, y_position, 'icon' );			sprite.inputEnabled = true;			sprite.events.onInputDown.add( icon_click, { 'url': 'http://website' } );			sprite.events.onInputOver.add( icon_over, {} );			sprite.events.onInputOut.add( icon_out, {} );			list_icons.add( sprite );		}	}}

Thanks in advance!

Link to comment
Share on other sites

I believe changing the line to this will make it work as you want it to:

sprite.events.onInputDown.add( function() { icon_click('http://website'); }, this );

Then icon_click will work like so:

function icon_click(url) { console.log(url); }

A common requirement is passing parameters along with event callbacks, but doing it this way means you get to run arbitrary code, including calling functions with any parameters you want.

Link to comment
Share on other sites

Thanks for the pointers - so if I understand correctly the best thing to do would be to pass the sprite as the context so I can use the sprite properties?

sprite.url = 'http://binarymoon.co.uk/';sprite.events.onInputDown.add( function() {	icon_click( this.url );}, sprite );

This works fine for over and out events but click still doesn't register. It doesn't matter if I use this or sprite - even just console.logging a string doesn't show anything.

 

I should add that each image will have a unique url so I need a way to make them different per sprite.

Link to comment
Share on other sites

I'm wondering if this is a bug with phaser. If I call only onInputDown then it works fine - but I want to use onInputDown, onInputOver, and onInputOut - and when I use all three, Over and Out work but Down seems to be ignored.

 

Edit:

Further experimentation and I may have found the culprit. I am using this.bringToTop(); onInputOver so that the current icon pops to the top (there's also a scale transition to show it's the active icon). If I remove bringToTop then it works.

 

I wonder if bringing it to the top is removing/ cancelling the down event?

Link to comment
Share on other sites

It's hard to say without knowing more about how the hit-test stuff works; I certainly wouldn't assume this would be problematic as even though the sprite is changing index, it isn't leaving the pointer - and your method of bringing the button to the top when you do a scale tween to show it's active is a common thing to do, so I would imagine if it'd been bugged this would've been seen and caught already.

Link to comment
Share on other sites

I've carried on working on this. I moved it all over to use the states and this is still happening.

 

To summarize the above, when I use sprite.bringToTop() the sprites onInputDown event gets cancelled/ ignored. If i remove that single command then the event still works.

 

Very confused now :(

Link to comment
Share on other sites

  • 2 weeks later...
 Share

  • Recently Browsing   0 members

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