Search code examples
flashactionscript-3airhittest

Action Script 3 touch drag with hitTest


I'm using Action Script 3 to make android game using "air for android"

I make a ball, gole and StatusTxt.

and i have end every thing in the design Correctly.

in the Code:

  Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT; 
ball.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin); 
ball.addEventListener(TouchEvent.TOUCH_END, onTouchEnd);

function onTouchBegin(e:TouchEvent) { 
    e.target.startTouchDrag(e.touchPointID);
}  
function onTouchEnd(e:TouchEvent) { 
    e.target.stopTouchDrag(e.touchPointID); 
}

if(gole.hitTestObject(ball))
        {
            StatusTxt.text = "You hit it.";
        }

but the StatusTxt has not changed Why??


Solution

  • Your test is outside the event. You could add it in the onTouchEnd function. If you want to test it outside of the TOUCH_END event you could use a ENTER_FRAME event.

    So it would end up looking something like this:

    function onTouchEnd(e:TouchEvent) 
    {
            e.target.stopTouchDrag(e.touchPointID); 
    
            if(gole.hitTestObject(ball))
            {
                StatusTxt.text = "You hit it.";
            }
    }
    

    or with the ENTER_FRAME like this:

    addEventListener(Event.ENTER_FRAME, onEnterFrame);
    
    function onEnterFrame(e:Event) : void
    {
            if(gole.hitTestObject(ball))
            {
                StatusTxt.text = "You hit it.";
            }
    }