Tom
Tom

Reputation: 12659

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);

Upvotes: 6

Views: 1611

Answers (2)

Ivan Chernykh
Ivan Chernykh

Reputation: 42166

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

Upvotes: 3

Siva
Siva

Reputation: 359

i hope it would be helpfull to you.

content.addEventListener(TouchEvent.TOUCH, onTouch); 

    function onTouch(e:TouchEvent) {

        var touch:Touch = e.getTouch(stage) as Touch;
        if(touch && touch.phase == TouchPhase.BEGAN){
            //do something
        }
    }

    or 
    addEventListener(TouchEvent.TOUCH, onTouch); 

    function onTouch(e:TouchEvent) {
       var touch:Touch = e.getTouch(sprite(eve.target)) as Touch;
        if(touch && touch.phase == TouchPhase.BEGAN){
            //do something
        }
    }

Upvotes: 0

Related Questions