cocos creator中如何判断鼠标是否移动
在Cocos Creator中,可以通过监听鼠标事件来判断鼠标是否移动。以下是一个示例代码:
cc.Class({
extends: cc.Component,
properties: {
lastMousePos: cc.Vec2,
},
onLoad () {
// 监听鼠标移动事件
this.node.on(cc.Node.EventType.MOUSE_MOVE, this.onMouseMove, this);
},
onMouseMove (event) {
// 获取当前鼠标位置
let mousePos = event.getLocation();
// 判断鼠标是否移动
if (this.lastMousePos && this.lastMousePos.equals(mousePos)) {
// 鼠标未移动
console.log("鼠标未移动");
} else {
// 鼠标已移动
console.log("鼠标已移动");
}
// 更新上次鼠标位置
this.lastMousePos = mousePos;
},
});
在上述代码中,我们定义了一个lastMousePos属性来保存上一次鼠标位置。在onLoad方法中,我们通过this.node.on来监听鼠标移动事件,并指定回调函数onMouseMove。在onMouseMove中,我们通过event.getLocation()获取当前鼠标位置,然后与lastMousePos比较,如果相等则表示鼠标未移动,否则表示鼠标已移动。最后,我们更新lastMousePos为当前鼠标位置,以便下一次移动事件的判断
原文地址: https://www.cveoy.top/t/topic/igye 著作权归作者所有。请勿转载和采集!