Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Touch events for empty space inside a starling sprite

I have a Starling Sprite which I want to get touch events for. A am using

content.addEventListener(TouchEvent.TOUCH, onTouch); 

function onTouch(e:TouchEvent) {

    var touch:Touch = e.getTouch(content) as Touch;
    if (touch) {
        //do something
    }
}

However, this is only working when the children of the Sprite are touched and not on the space in between them. I am thinking that if I create an alpha'd Image and stick underneath it should then pick up the touch events, but is there a better way?

I was also thinking about listening for the touch on the stage and doing the equivalent of a hitTestPoint().

I came up with

var hitTest = (touch && content.hitTest(this.globalToLocal(new Point(touch.globalX, touch.globalY))));

But this doesn't seem to work either, the touch event is only working when a child of the content Sprite is touched.

Solution:

In the end I used an alpha'd Quad as suggested by Cherniv

bg = new Quad(width, height);
bg.alpha = 0;
addChildAt(bg, 0);
like image 739
Tom Avatar asked May 24 '13 12:05

Tom


1 Answers

Try to use lightweight transparent Quad (no need for an Image) in your Sprite .

like image 142
Ivan Chernykh Avatar answered Oct 02 '22 03:10

Ivan Chernykh