德裕AS3触摸事件preSS和持有 [英] Starling AS3 touch event press and hold

查看:138
本文介绍了德裕AS3触摸事件preSS和持有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找,而现在对于如何codeA按钮,允许用户以preSS和保持一​​个明显的例子。虽然发生这种情况,我想执行一些code。

我已经实现了TouchEvent.Touch和touchPhase.Began但它只是触发一次。

我无法找到如何实现这一明确的解释。任何帮助是AP preciated。

  BTN press.addEventListener(TouchEvent.TOUCH,为pressed);

私有函数pressed(事件:的TouchEvent){
    VAR摸:触摸= event.getTouch(BTN preSS,TouchPhase.BEGAN);
    如果(触摸)
    {
        跟踪(pressed);
    }
}
 

解决方案

德裕的TouchEvent TouchPhase 可以是有点混乱在第一。该 TouchPhase.BEGAN 仅触发,当用户开始触摸屏幕。如果你想执行一些code 而手指上的按钮,开始执行对 TouchPhase.BEGAN 相和停止执行的在 TouchPhase.ENDED 相(也只触发一次,当用户停止触摸屏幕)。

例如,你的情况,你可以这样做:

  BTN press.addEventListener(TouchEvent.TOUCH,为pressed);

私有函数pressed(事件:的TouchEvent):无效
{
    VAR触摸:触摸= event.getTouch(BTN preSS);

    如果(touch.phase == TouchPhase.BEGAN)//手指向下
    {
        跟踪(pressed刚才);

        //做你的东西
        的addEventListener(Event.ENTER_FRAME,onButtonHold);
    }

    如果(touch.phase == TouchPhase.ENDED)//在手指
    {
        跟踪(释放);

        //停止做的东西
        removeEventListener(Event.ENTER_FRAME,onButtonHold);
    }
}

私有函数onButtonHold(五:事件):无效
{
    跟踪(做的东西,而按钮pressed!);
}
 

希望这有助于澄清事情有点!

I have been search for a while now for a clear example of how to code a button that allows the user to press and hold. While this happens I would like to execute some code.

I have implemented the TouchEvent.Touch and the touchPhase.Began but it only fires once.

I cant find a clear explanation on how to implement this. Any help is appreciated.

btnPress.addEventListener(TouchEvent.TOUCH, isPressed);

private function isPressed(event:TouchEvent){
    var touch:touch = event.getTouch(btnPress, TouchPhase.BEGAN);
    if(touch)
    {
        trace("pressed");
    }
}

解决方案

Starling TouchEvent and TouchPhase can be a little confusing at first. The TouchPhase.BEGAN only fires once, when the user starts touching the screen. If you want to execute some code while the finger is on the button, start executing on the TouchPhase.BEGAN phase and stop executing on the TouchPhase.ENDED phase (which fires also only once, when the user stops touching the screen).

For example, in your case you could do something like:

btnPress.addEventListener(TouchEvent.TOUCH, isPressed);

private function isPressed(event:TouchEvent):void
{
    var touch:Touch = event.getTouch(btnPress);

    if(touch.phase == TouchPhase.BEGAN)//on finger down
    {
        trace("pressed just now");

        //do your stuff
        addEventListener(Event.ENTER_FRAME, onButtonHold);
    }

    if(touch.phase == TouchPhase.ENDED) //on finger up
    {
        trace("release");

        //stop doing stuff
        removeEventListener(Event.ENTER_FRAME, onButtonHold);
    }
}

private function onButtonHold(e:Event):void
{
    trace("doing stuff while button pressed!");
}

Hope this helps clarify things a little!

这篇关于德裕AS3触摸事件preSS和持有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆