Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 斯达林单面法_Actionscript 3_Flash_Onclick_Touch Event_Starling Framework - Fatal编程技术网

Actionscript 3 斯达林单面法

Actionscript 3 斯达林单面法,actionscript-3,flash,onclick,touch-event,starling-framework,Actionscript 3,Flash,Onclick,Touch Event,Starling Framework,我使用的是starling框架,为了模拟onclick方法,我使用以下代码: if(e.getTouch(this).phase == TouchPhase.ENDED){ //Some code } 没关系,但如果鼠标不再在按钮上方,它也会启动,但是我希望只有当鼠标结束时它才会启动。有没有办法做到这一点? 谢谢 在代码中,“this”是一个精灵,根据TouchEvent类的interacticsWith(target:DisplayObject)方法,如果当前正在触摸

我使用的是starling框架,为了模拟onclick方法,我使用以下代码:

if(e.getTouch(this).phase == TouchPhase.ENDED){
            //Some code
}
没关系,但如果鼠标不再在按钮上方,它也会启动,但是我希望只有当鼠标结束时它才会启动。有没有办法做到这一点? 谢谢

在代码中,“this”是一个精灵,根据
TouchEvent
类的
interacticsWith(target:DisplayObject)
方法,如果当前正在触摸目标,则它应该返回true,这与此无关。我无法检验这一理论,但以下几点应该有效:

if (e.getTouch(this).phase == TouchPhase.ENDED && e.interactsWith(this)) {
    //The touch ended on the same DisplayObject as it originated at
}
这个想法怎么样:

if ( e.getTouch( this ).phase == TouchPhase.ENDED ) {
    if ( this.hitTestPoint( stage.mouseX, stage.mouseY, true ) ) {
        // Some code
    }
}

简单的方法是使用starling.display.Button来完成。它们发送触发事件,这基本上就是您想要的。“不那么容易”的方法是通过复制按钮中的实际操作来跟踪您的触摸:

    private function onTouch(event:TouchEvent):void
    {
        var touch:Touch = event.getTouch(this);
        if (!mEnabled || touch == null) return;

        if (touch.phase == TouchPhase.BEGAN && !mIsDown)
        {
            //equivalent to MOUSE_DOWN
            mIsDown = true;
        }
        else if (touch.phase == TouchPhase.MOVED && mIsDown)
        {
            // reset button when user dragged too far away after pushing
            var buttonRect:Rectangle = getBounds(stage);
            if (touch.globalX < buttonRect.x - MAX_DRAG_DIST ||
                touch.globalY < buttonRect.y - MAX_DRAG_DIST ||
                touch.globalX > buttonRect.x + buttonRect.width + MAX_DRAG_DIST ||
                touch.globalY > buttonRect.y + buttonRect.height + MAX_DRAG_DIST)
            {
                mIsDown = false;
            }
        }
        else if (touch.phase == TouchPhase.ENDED && mIsDown)
        {
            mIsDown = false;
            //this is a click
            dispatchEventWith(Event.TRIGGERED, true);
        }
    }
私有函数onTouch(事件:TouchEvent):无效
{
var touch:touch=event.getTouch(this);
如果(!mEnabled | | touch==null)返回;
if(touch.phase==TouchPhase.start&!误下)
{
//相当于鼠标按下
mIsDown=true;
}
else if(touch.phase==TouchPhase.MOVED&&mIsDown)
{
//当用户按下后拖得太远时,重置按钮
var buttonRect:Rectangle=getBounds(stage);
如果(touch.globalXbuttonRect.x+buttonRect.width+最大拖动距离||
touch.globalY>buttonRect.y+buttonRect.height+最大拖动距离)
{
错误下降=错误;
}
}
否则如果(touch.phase==TouchPhase.end&&mIsDown)
{
错误下降=错误;
//这是一个点击
dispatchEventWith(Event.TRIGGERED,true);
}
}

您必须更改按钮Rect代码以反映精灵的形状,但基本上就是这样。

这与之前的操作相同。它不会给你实际的触感,它会给你触感,它是在(我想)你按下按钮(比如onmousedown)编辑帖子以使用当前鼠标位置时发出的;我想建议的是添加第二个条件来验证鼠标确实位于按钮上方,因为您的问题是“如果鼠标不再位于按钮上方,它也会触发…”请将其视为某种伪代码。谢谢你认为这是个好主意。干杯您还可以在鼠标按下部分添加一个超时,以便只选中“快速单击”。但如果你要走到这一步,我建议你使用Gestouch。