Touch events for empty space inside a starling sprite
Asked Answered
U

2

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);
Ulane answered 24/5, 2013 at 12:16 Comment(1)
Did you try isTouching() : doc.starling-framework.org/core/starling/events/… ?Quickwitted
Q
3

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

Quickwitted answered 24/5, 2013 at 12:36 Comment(4)
I went with this in the end. If no-one posts a better solution I will accept your answer. CheersUlane
@Ulane 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 hereQuickwitted
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.Ansate
@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 ;) )Quickwitted
S
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
        }
    }
Seltzer answered 24/5, 2013 at 12:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.