【问题标题】:Starling mouseover detectionStarling 鼠标悬停检测
【发布时间】:2013-02-19 17:57:57
【问题描述】:

我正在制作可点击对象的点击游戏。当玩家将鼠标移到对象上时,光标旁边会出现一个工具提示。它几乎可以与下面的代码一样工作:

 private  function added():void
        {
            removeEventListener(Event.ADDED, added);
            this.addEventListener(TouchEvent.TOUCH, onTouch);
        }

protected function onTouch(e:TouchEvent):void
        {
            var touchHover:Touch = e.getTouch(this, TouchPhase.HOVER);

            if (touchHover)
            {
                trace("show");
                //mouse is hovered over this object. Therefore call Hovertext:
                if (Game.hoverText.message != name_)
                    Game.hoverText.message = name_
            }
            else
            {
                //mouse leaves the object
                trace("hide");
                Game.hoverText.hideMessage(name_);
            }

        }

但是,它有一个我不明白的奇怪问题。如果我将鼠标移到对象上然后将其向下移动仍然停留在对象上,它会在每隔一帧左右触发隐藏功能。当我将光标向右移动时会发生同样的事情,但向上或向左移动时不会。

所以我的问题是我的代码有什么问题?这甚至是检测鼠标何时滚过对象以及何时滚开的最佳方法吗?

编辑:我一直在经历以下迭代,每个迭代都有相同的问题:

var touch:Touch = event.getTouch(this);

        if (touch == null) {
            // Set Object alpha to 0;
            //trace("pois");
            Game.hoverText.hideMessage(name_);
        }
        else if (touch.phase == TouchPhase.HOVER) {
            // Set Object alpha to 1;
            //trace("paalla");
            if (Game.hoverText.message != name_)
                Game.hoverText.message = name_;
        }
        else {
            // for a phase BEGIN/MOVE/STATIONARY case
            // see if the touch is over the bounds of the tile (assuming 'this' is the tile)
            HELPER_POINT.x = touch.globalX;
            HELPER_POINT.y = touch.globalY;
            this.globalToLocal(HELPER_POINT, HELPER_POINT);
            if(this.hitTest(HELPER_POINT, true) != null)
            {
                // Set Object alpha to 1; over tile
                trace("paalla");
            }
            else
            {
                // Set Object alpha to 0; not over tile
                trace("pois");
            }

}

var touchHover:Touch = e.getTouch(this);

        if (touchHover && touchHover.phase == TouchPhase.HOVER)
        {
            trace("show");
            //mouse is hovered over this object. Therefore call Hovertext:
            if (Game.hoverText.message != name_)
                Game.hoverText.message = name_
        }

        if (touchHover == null)
        {
            //mouse leaves the object
            trace("hide");
            Game.hoverText.hideMessage(name_);
        }

这里还有用于说明问题的 swf: http://www.students.tut.fi/~salmi26/ScorpionBox.html

【问题讨论】:

  • 我需要您显示附加事件侦听器的代码。
  • 我在“添加”函数中进行了编辑,该函数在将对象添加到舞台时调用。
  • 尝试使用TouchPhase.BEGAN 而不是TouchPhase.HOVER。让我知道它是否有效。
  • 当一个人想要检测鼠标按下时,不使用BEGAN阶段吗? “TouchPhase.BEGAN:手指刚刚触摸到屏幕,或者鼠标按键被按下。” wiki.starling-framework.org/manual/touch_events

标签: actionscript-3 hover mouseover starling-framework


【解决方案1】:
private function isPressed(event:TouchEvent):void
{
    var touch:touch = event.getTouch(this);

    if(touch.phase == TouchPhase.BEGAN){
      trace("show");
      //mouse is hovered over this object. Therefore call Hovertext:
      if (Game.hoverText.message != name_)
        Game.hoverText.message = name_
    } else if(touch.phase == TouchPhase.ENDED){
        trace("release");

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

【讨论】:

  • 我试过了,但根本无法正常工作。 Starling wiki 似乎说 BEGAN 仅用于印刷机等,而不是悬停。不过我会尽快调查。
  • 你是用鼠标还是真触控?
  • 我现在正在使用鼠标。
  • 您必须改用鼠标事件侦听器和 MouseEvents。到时候就可以了。
【解决方案2】:
import starling.events.Touch;
import starling.events.TouchEvent;
import starling.events.TouchPhase;

private var touches:Vector.<Touch>;
private var touch:Touch;
private var touchStage:Touch;

private function onTouch(e:TouchEvent=null):void {

        touches= e.getTouches(DisplayObject(e.target));
        if (touches.length == 1)
        {
            touch = touches[0];

            if (touch.phase == TouchPhase.BEGAN){
                m_TouchTarget = touch.target;
                //HERE IS A MOUSE DOWN
            }

            if (touch.phase == TouchPhase.ENDED){
                m_TouchEndedPoint = new Point(touch.globalX, touch.globalY);

                    if (stage.hitTest(m_TouchEndedPoint, true) == m_TouchTarget)
                    {
                        //HERE IS A MOUSE UP
                    }
            }
            if (touch.phase == TouchPhase.MOVED){
                m_TouchEndedPoint = new Point(touch.globalX, touch.globalY);
                //HERE IS A MOUSE OUT

                    if (stage.hitTest(m_TouchEndedPoint, true) != m_TouchTarget)
                    {
                        //HERE IS A MOUSE OVER
                    }
            }

        }
    }

【讨论】:

    猜你喜欢
    • 2023-01-09
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 2011-08-04
    • 2018-09-03
    • 1970-01-01
    • 2017-05-17
    • 2012-07-06
    相关资源
    最近更新 更多