zoukankan      html  css  js  c++  java
  • Q&As:1.cocos2d-html5如何获得鼠标划过事件

    不喜欢按部就班学东西,感觉各种框架各种技术就应该是拿到手用的,这应该是导致我现在学了这么多却没一样精通的缘故吧。

    发现自己喜欢在QQ群回答一些菜鸟的问题,就算自己不清楚也会乐意看代码帮助解决╮(╯_╰)╭,就当是别人推着我前进吧,干脆开一个分类专门记录回答别人提问的。

    Q1:想用cocos2d-html5做个背包系统,需要鼠标划过背包中道具的时候弹出道具的信息,问如何获得这个鼠标划过事件?

    答案是没有一个简单的api可以做到这一点。需要修改cocos2d的代码并且自己做事件分发。

    cc._addEventListener(element, "mousemove", function (event) {
                    if(!selfPointer._mousePressed)
                        return;
    
                    var pos = selfPointer.getHTMLElementPosition(element);
                    var location = selfPointer.getPointByEvent(event, pos);
    
                    if(!supportTouches)
                        selfPointer.handleTouchesMove([selfPointer.getTouchByXY(location.x, location.y, pos)]);
    
                    var mouseEvent = selfPointer.getMouseEvent(location,pos,cc.EventMouse.MOVE);
                    mouseEvent.setButton(event.button);
                    cc.eventManager.dispatchEvent(mouseEvent);
    
                    event.stopPropagation();
                    event.preventDefault();
                }, false);

    以上是c2d h5的代码,CCInputManager.js,这里向canvas注册了mousemove的事件侦听。该函数第一句就是屏蔽了鼠标非按下状态的鼠标滑动事件。所以,要做到提问中的功能,注掉这句先。剩下的就是自己做事件分发了,判断鼠标位置是否在道具的矩形内。

  • 相关阅读:
    python中用exit退出程序
    习题5-2 使用函数求奇数和 (15分)
    习题5-1 符号函数 (10分)
    练习5-3 数字金字塔 (15分)
    练习5-2 找两个数中最大者 (10分)
    练习5-1 求m到n之和 (10分)
    ubuntu使用教程
    图解HTTP 上
    Sublime Text 3 插件
    两千行PHP学习笔记
  • 原文地址:https://www.cnblogs.com/LoadingChan/p/3688470.html
Copyright © 2011-2022 走看看