使用hitTestObject停止拖动触摸

时间:2015-02-25 15:11:52

标签: actionscript-3

如何使用hitTestObject停止触摸或鼠标拖动事件?

就像,拖动object1然后点击object2。当object1命中object2时,drag-event应该停止。

当石头击中玩家时,为什么它不停止拖动模拟?

onGame:

control:Controller = new Controller();

addEventListener(Event.ENTER_FRAME, loop);

private function loop (e:Event):void {
  if(player.hitTestObject(stone) {
    Controller.btn = 1;
  }
}

控制器:

public static var btn:Number;

analog.addEventListener(TouchEvent.TOUCH_BEGIN, pressed);
analog.addEventListener(TouchEvent.TOUCH_MOVE, moving);
analog.addEventListener(TouchEvent.TOUCH_END, unpress);

private function pressed(e:TouchEvent):void {
  btn = 0;
  analog.startTouchDrag(e:touchPointID);
}

private function moving(e:TouchEvent):void {
  if(btn == 1) {
     analog.stopTouchDrag(e.touchPointID);
  {
}
private function unpress(e:TouchEvent):void {
  analog.stopTouchDrag(e.touchPointID);
  btn = 0;
}

1 个答案:

答案 0 :(得分:0)

有很多方法可以实现这一目标,这是一种非常快速和肮脏的方法:

import flash.events.MouseEvent;

object1.addEventListener(MouseEvent.MOUSE_DOWN, onTouch);
object1.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
object1.addEventListener(MouseEvent.MOUSE_UP, onRelease);

function onTouch(e:MouseEvent):void 
{
    object1.startDrag();
}

function onRelease(e:MouseEvent):void 
{
    object1.stopDrag();
}

function onMove(e:MouseEvent):void 
{
    if(object1.hitTestObject(object2))
    {
        object1.stopDrag();
    }
}

编辑:在回复您的评论时,您可以尝试使用此代码(虽然未经测试)

import flash.events.TouchEvent;

object1.addEventListener(TouchEvent.TOUCH_BEGIN, onTouch);
object1.addEventListener(TouchEvent.TOUCH_MOVE, onMove);
object1.addEventListener(TouchEvent.TOUCH_END, onRelease);

function onTouch(e:TouchEvent):void 
{
    object1.startTouchDrag(e.touchPointID);
}
function onRelease(e:TouchEvent):void 
{
    object1.stopTouchDrag(e.touchPointID);
}

function onMove(e:TouchEvent):void 
{
    if(object1.hitTestObject(object2))
    {
        object1.stopTouchDrag(e.touchPointID);
    }
}