Jump to content

pointerOver bug?


dst
 Share

Recommended Posts

Not sure if this is a bug or not but I'm getting some weird issues when using sprite.input.pointerOver(). I have an array of sprites that I loop over in my onMouseTap function (listener is attached using game.input.onTap.add). I check for pointerOver() and move the sprite to a random position on the stage. When I then click away from the sprite pointerOver must still be returning true as the sprite moves again. Not only that but clicking multiple sprites moves all the ones already clicked.

Haven't looked into Phaser's inner workings yet so I'm not sure if this is a bug or that I'm just using it incorrectly. Hopefully someone with more knowledge can advise!

 

Demo here: https://dl.dropboxusercontent.com/u/4352121/pointerOverTest/index.html

And here's the code:

var game = new Phaser.Game(320, 480, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });var diamonds;function preload() {    game.load.image("diamond", "diamond.png");}function create() {    diamonds = [];    var diamond;    for (var i = 0; i < 3; i++) {        diamond = game.add.sprite(0, 0, "diamond");        diamond.anchor.setTo(.5, .5);        diamond.x = Math.floor(Math.random() * 320 + 1);        diamond.y = Math.floor(Math.random() * 480 + 1);        diamond.inputEnabled = true;        diamonds[i] = diamond;    }    game.input.onTap.add(onMouseTap, this);}function update() {}function render() {}function onMouseTap() {    for (var i = 0; i < diamonds.length; i++) {        var diamond = diamonds[i];        if (diamond.input.pointerOver()) {            diamond.x = Math.floor(Math.random() * 320 + 1);            diamond.y = Math.floor(Math.random() * 480 + 1);        }    }}
Link to comment
Share on other sites

pointerOver is really an internal method (and probably ought to be documented as such actually) used as part of the hit test sequence. Here is how I would re-write your code above (note I swapped to use a tween just so you can visually see the repositioning happen)

var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create });function preload() {    game.load.image("diamond", "assets/sprites/diamond.png");}function create() {    var diamond;    for (var i = 0; i < 16; i++)    {        diamond = game.add.sprite(game.world.randomX, game.world.randomY, "diamond");        diamond.anchor.set(0.5);        diamond.inputEnabled = true;        diamond.events.onInputDown.add(clickedDiamond, this);    }}function clickedDiamond(diamond) {    game.add.tween(diamond).to({ x: game.world.randomX, y: game.world.randomY }, 250, Phaser.Easing.Linear.None, true);}
Link to comment
Share on other sites

Thanks Rich. Your solution is the same as my initial attempt but I couldn't find a way to get a reference to the clicked object in the callback (hence why I was using pointerOver). I see that the callback is now passed a reference to the object, is that something new in 2.0?

Link to comment
Share on other sites

  • 10 months later...
 Share

  • Recently Browsing   0 members

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