Jump to content

[SOLVED] Detect mouseOver/Out collision - moving sprites


Krystiian
 Share

Recommended Posts

So I'm having some troubles while trying to swap two Sprites;

 1. When the mouse is over the sprite

 2. When the mouse isn't over the sprite

Everything is working fine while moving the Pointer,

I'm using a group that contains many sprites 

    sprite.inputEnabled = true;
    sprite.events.onInputOver.add(over, this);
    sprite.events.onInputOut.add(nover, this);
function over(sprite){
    sprite.loadTexture("spriteTargeted", 0);
    sprite.over = true;
}

function nover(sprite){
    sprite.loadTexture("spriteNotTargeted", 0);
    sprite.over = false;
}

Those sprites will be falling from the top of the screen, if the pointer IS MOVED over one of those sprites, they will be swaped correctly.

 

The problems

  1.  If the pointer is PLACED ('Not Targeted' sprite will be displayed) in one pixel without being moved and one of those falling sprites collides with the mouse pointer, the 'Targeted Sprite' would not be displayed and the 'Targeted' sprite will remain displayed.
  2. If the pointer is PLACED over one sprite ('Targeted Sprite will be displayed) and the pointer remains without any other iteration, the falling sprite will leave the pointer 'boundingBox' the 'Not Targeted' sprite would not be displayed and the 'Targeted' sprite will remain displayed.
  3. If the pointer is PLACED over one sprite ('Targeted Sprite will be displayed) and the user ALT+TAB to any other window and move the pointer, when you ALT+TAB back, the 'Targeted Sprite' will remain.

I Found a solution looping over all those childrens and checking if the Pointer coordinates are inside the 'boundingbox' of one of those sprites and then swapping the sprites but its quite hard-coded and many calculations are being used when the group contains too many elements.

Is there any efficient method to solve this problem?

Thank you

Edited by Krystiian
title
Link to comment
Share on other sites

On 3/9/2016 at 4:34 AM, symof said:

This should be what your are looking for http://phaser.io/examples/v2/buttons/action-on-click

 

It's not, because the Sprite is static so the problem does not exist.

The problem starts when the Sprite is not static like in this example: http://phaser.io/examples/v2/input/pixel-perfect-click-detection
If you put your mouse in one pixel over the Rabbit, it's gonna be detected as 'over', but if you keep the pointer in the same position (without moving it) and wait a bit till the rabbit rotates enough to leave the pointer collision(theorically), the collision should not be detected anymore but it persist, so im trying to figure out one way to solve it without the need to loop every sprite every frame.

 

Anyway, thank you for your reply.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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