Question

If anyone else having the same issue, I am facing it in few of my projects but never bother finding it cause.

Every thing works fine while adding listener for touchevents:

btn.addEventListener(TouchEvent.TOUCH,function(e:TouchEvent){
            var t:Touch = e.getTouch(stage);

            if(t.phase == TouchPhase.ENDED)
            {

                    resetBall();

            }
        });

but sometime if somehow my mouse hover through that object my project stops, and throws the following error.

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at Function/Game.as$0:anonymous()[...src\Game.as:45]
    at starling.events::EventDispatcher/invokeEvent()[/Users/redge/Dropbox/Development/starling/starling/src/starling/events/EventDispatcher.as:141]
    at starling.events::TouchEvent/dispatch()[/Users/redge/Dropbox/Development/starling/starling/src/starling/events/TouchEvent.as:174]
    at starling.events::TouchProcessor/processTouches()[/Users/redge/Dropbox/Development/starling/starling/src/starling/events/TouchProcessor.as:186]
    at starling.events::TouchProcessor/advanceTime()[/Users/redge/Dropbox/Development/starling/starling/src/starling/events/TouchProcessor.as:135]
    at starling.core::Starling/advanceTime()[/Users/redge/Dropbox/Development/starling/starling/src/starling/core/Starling.as:379]
    at starling.core::Starling/nextFrame()[/Users/redge/Dropbox/Development/starling/starling/src/starling/core/Starling.as:369]
    at starling.core::Starling/onEnterFrame()[/Users/redge/Dropbox/Development/starling/starling/src/starling/core/Starling.as:568]
Was it helpful?

Solution

The standard is to check if the touch object returned from getTouch() is null before checking for the phase property.

var touch:Touch = e.getTouch( stage );
if ( touch ) {
    if( touch.phase == TouchPhase.ENDED ) {
       resetBall();
    }
}

From the starling wiki:

private function onTouch(event:TouchEvent):void
{
    var touch:Touch = event.getTouch(this, TouchPhase.BEGAN);
    if (touch)
    {
        var localPos:Point = touch.getLocation(this);
        trace("Touched object at position: " + localPos);
    }
}

http://wiki.starling-framework.org/manual/touch_events

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top