6

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);
Charles
  • 50,943
  • 13
  • 104
  • 142
Tom
  • 12,591
  • 13
  • 72
  • 112

2 Answers2

3

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

Josh Tynjala
  • 5,235
  • 3
  • 23
  • 25
Ivan Chernykh
  • 41,617
  • 13
  • 134
  • 146
  • I went with this in the end. If no-one posts a better solution I will accept your answer. Cheers – Tom May 24 '13 at 15:18
  • @Tom there is some kind of logic in this `touch'` behaviour , but it is sad that it has no optional behaviour as you have noted . i have tried the `isTouching` - it is not helping here – Ivan Chernykh May 24 '13 at 15:47
  • This is the approach that Daniel Sperl, Starling's creator, usually recommends to people in the Starling forums. On the native display list, the usual approach many people use is `graphics.drawRect()` with an invisible fill, so the Starling approach is roughly the same. There is one alternative in Starling: override `hitTest()` in a subclass and provide your own custom behavior. – Josh Tynjala May 24 '13 at 22:20
  • @joshtynjala thank you Josh! (yeah , we know who are Daniel and who are you because the Starling community is not so big and its easy to remember who actually creating the framework ;) ) – Ivan Chernykh May 25 '13 at 05:54
0

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
        }
    }
Siva
  • 359
  • 3
  • 15