使用cocos2d-js-v3.0-rc1 版本开发包含鼠标监听事件的web页面。
事件监听代码如下:
将事件内容封装在 cc.Node 子对象中:
/**
* Created by res on 2014/9/4.
*/
var SystemNode = cc.Node.extend({
_touchListener: null,
_size : cc.size(90, 89),
infoIndex : 0,
systemDataTran1: null,
systemDataTran2: null,
systemDataTran3: null,
relation: null,
dataID: null,
partHeight: 200,
system:null,
_anchorPoint:new Object(),
systemLayer:null,
tip : null,
init:function(id,name,i,length,data,systemLayer){
this._super();
this.systemLayer = systemLayer;
var partNum = Math.round(length/2);
var systemSite = Math.round(i/2)-1;
var upDownFlag = i%2==1;
//判断系统图片
var system_picture = sys_png;
if("eig" == data.type){
system_picture = eig_png;
}
if(id == gloableCenterID){
system_picture = center_png;
}
var system = cc.Sprite.create(system_picture);
this.system = system;
var startWidth = partWidth*partNum/2;
var x = size.width/2-startWidth+(partWidth+40)*systemSite;
var y = size.height/2;
if(upDownFlag){
y = y + this.partHeight;
}else{
y = y - this.partHeight;
}
system.attr({
x: x,
y: y
});
this._anchorPoint.x = system.x-this._size.width/2;
this._anchorPoint.y = system.y-this._size.height/2;
var str = new Object();
this.splitStr(str,name);
var systemName = cc.LabelTTF.create(str.content, "微软雅黑", 18,null,cc.TEXT_ALIGNMENT_CENTER);
systemName.x = x;
systemName.color = cc.color(0, 0, 0);
var titleY =0;
if(upDownFlag){
titleY = y + 56+28;
}else{
titleY = y - 56-28;
}
systemName.y = titleY;
var systemDataTran1 = new SystemDataTrans1(system,0);
var systemDataTran2 = new SystemDataTrans1(system,1);
var systemDataTran3 = new SystemDataTrans1(system,2);
this.systemDataTran1 = systemDataTran1;
this.systemDataTran2 = systemDataTran2;
this.systemDataTran3 = systemDataTran3;
//在系统对象中增加关系
this.relation = data.relation;
this.dataID = id;
this.addChild(systemName);
this.addChild(system,0);
this.addChild(systemDataTran1);
this.addChild(systemDataTran2);
this.addChild(systemDataTran3);
this._touchListener = cc.EventListener.create({
event: cc.EventListener.MOUSE,
swallowTouches: true,
onMouseUp: this.onMouseUp.bind(this),
onMouseMove: this.onMouseMove.bind(this)
});
cc.eventManager.addListener(this._touchListener, this);
},
onMouseMove: function (touch, event) {
var touchPoint = touch.getLocation();
var nsp = this.convertToNodeSpace(touchPoint);
this._hitted = this.hitTest(touchPoint);
var size = cc.director.getWinSize();
if (this._hitted) {
//cc.log("This onMouseMove dataID="+this.dataID);
if(this.tip == null){
var tip = new SystemTipNode();
tip.init(this.dataID,nsp.x,nsp.y);
this.tip = tip;
this.systemLayer.addChild(tip);
}else{
if(this.tip != null){
this.systemLayer.removeChild(this.tip,true);
this.tip = null;
var tip = new SystemTipNode();
tip.init(this.dataID,nsp.x,nsp.y);
this.tip = tip;
this.systemLayer.addChild(tip);
}
}
}else{
//cc.log("This onMouseMove out dataID="+this.dataID);
if(this.tip != null){
this.systemLayer.removeChild(this.tip,true);
this.tip = null;
}
}
},
onMouseUp: function (touch, event) {
var touchPoint = touch.getLocation();
this._hitted = this.hitTest(touchPoint);
if (this._hitted) {
cc.log("This onMouseUp dataID="+this.dataID);
gloableSystemClick(this.dataID);
}
},
/**
* Checks a point if is in widget's space
* @param {cc.Point} pt
* @returns {boolean}
*/
hitTest: function (pt) {
var nsp = this.convertToNodeSpace(pt);
//cc.log("this.nsp.x="+nsp.x);
//cc.log("this.nsp.y="+nsp.y);
var result = false;
var bb = cc.rect( this._anchorPoint.x, this._anchorPoint.y, this._size.width, this._size.height);
//cc.log("x.left="+bb.x);
//cc.log("x.right="+(bb.x + bb.width));
//cc.log("y.left="+bb.y);
//cc.log("y.right="+(bb.y + bb.height));
if (nsp.x >= bb.x && nsp.x <= bb.x + bb.width && nsp.y >= bb.y && nsp.y <= bb.y + bb.height) {
result = true;
}
//cc.log("result="+result);
return result;
},
/**
* 根据长度7换行
* @param str
* @param name
*/
splitStr:function(str,name){
var nameArray = name.split("");
str.content = "";
str.num = 0;
var i = 0;
for(i = 0;i < nameArray.length;i++){
str.content = str.content + nameArray*;
if(i!=0&&i%6==0){
str.content = str.content + "\n";
str.num = str.num + 1;
}
}
}
});
```
在Layer中启动这个Node。功能运行正常。就是在IE11下事件监听功能失效。
想咨询一下是版本的问题还是说IE11下需要使用其他事件?
*