如何同时检测触摸两个按钮的事件

时间:2015-11-09 14:16:36

标签: android actionscript-3 flash air

我正在使用Flash AIR for Android编写应用程序。 我有两个按钮,当触摸两个按钮并保持3秒钟时,我怎么能触发? 有任何想法吗?谢谢你。

1 个答案:

答案 0 :(得分:0)

这是一些未经测试的代码,显示了您正在寻找的模式。 我们假设您的按钮名为btn1btn2

//a timer to track 3 seconds of holding down both buttons
var touchTimer:Timer = new Timer(3000, 1);

//a dictionary to track when a button is touched and the corresponding touch id
var touchTracker:Dictionary = new Dictionary();

//add the touch listeners to the buttons
btn1.addEventListener(TouchEvent.TOUCH_BEGIN, btnTouch, false, 0, true);
btn2.addEventListener(TouchEvent.TOUCH_BEGIN, btnTouch, false, 0, true);

//listen for the timer's tick
///it will trigger whatever function should run after 3 seconds of holding down both buttons
touchTimer.addEventListener(TimerEvent.TIMER, holdComplete, false, 0, true);    

//listen for touch end events, so you can reset the timer
stage.addEventListener(TouchEvent.TOUCH_END, globalTouchEnd, false, 0, true);



private function btnTouch(e:TouchEvent):void {
    //associate the touchID with the object touched
    touchTracker[e.touchPointID] = e.currentTarget;

    //and vice-versa just for convenience
    touchTracker[e.currentTarget] = e.touchPointID;

    //if both buttons are being touched and the timer isn't running, start the timer
    if (touchTracker[btn1] && touchTracker[btn2] && !touchTimer.running) {
        touchTimer.start();
    }
}

//delete the records for the item that the touch ended on   
private function globalTouchEnd(e:TouchEvent):void {
    var obj:Object = touchTracker[e.touchPointID];

    //if there is a record for this touch id, delete it
    if (obj) {
        //null the reference the key points to
        touchTracker[e.touchPointID] = null;
        //delete the key
        delete touchTracker[e.touchPointID];

        touchTracker[obj] = null;
        delete touchTracker[obj];
    }

    //if either of the buttons are NOT being touched, reset the timer
    if (!touchTracker[btn1] || !touchTracker[btn2]) {
        touchTimer.reset();
    }
}

//this function runs if the timer is allowed to complete
function holdComplete(e:Event):void {
    trace("you held the buttons for 3 seconds, good for you");
}