1. 创建监听器
var listener = cc.EventListener.create({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches: true,
onTouchBegan: function (touch, event) {
var locationInNode = selfPointer.convertToNodeSpace(touch.getLocation());
var s = selfPointer.getContentSize();
var rect = cc.rect(0, 0, s.width, s.height);
if (cc.rectContainsPoint(rect, locationInNode)) {
selfPointer.setColor(cc.color.RED);
return true;
}
return false;
},
onTouchMoved: function (touch, event) {
//this.setPosition(this.getPosition() + touch.getDelta());
},
onTouchEnded: function (touch, event) {
selfPointer.setColor(cc.color.WHITE);
if(selfPointer._removeListenerOnTouchEnded)
cc.eventManager.removeListener(selfPointer._listener);
}
});
cc.eventManager.addListener(listener, this);
3. ToggleMenuItem加入监听以及移除监听
var toggleItem = cc.MenuItemToggle.create(cc.MenuItemFont.create("Enabled"), cc.MenuItemFont.create("Disabled"),
function (sender) {
if (enable) {
cc.eventManager.removeListener(listener1);
statusLabel.setString("The sprite could not be touched!");
enable = false;
} else {
cc.eventManager.addListener(listener1, sprite1);
statusLabel.setString("The sprite could be touched!");
enable = true;
}
});
toggleItem.setPosition(origin.x + size.width/2, origin.y + 80);
var menu = cc.Menu.create(toggleItem);
menu.setPosition(0, 0);
menu.setAnchorPoint(0, 0);
this.addChild(menu, 1);
this._listener1 = cc.EventListener.create({
event: cc.EventListener.CUSTOM,
eventName: "game_custom_event1",
callback: function(event){
statusLabel.setString("Custom event 1 received, " + event.getUserData() + " times");
}
});
cc.eventManager.addListener(this._listener1, 1);
var sendItem = cc.MenuItemFont.create("Send Custom Event 1", function(sender){
++selfPointer._item1Count;
var event = new cc.EventCustom("game_custom_event1");
event.setUserData(selfPointer._item1Count.toString());
cc.eventManager.dispatchEvent(event);
});
sendItem.setPosition(origin.x + size.width/2, origin.y + size.height/2);
cc.eventManager.addListener({
event: cc.EventListener.KEYBOARD,
onKeyPressed: function(keyCode, event){
var label = event.getCurrentTarget();
label.setString("Key " + String.fromCharCode(keyCode) + "(" + keyCode.toString() + ") was pressed!");
},
onKeyReleased: function(keyCode, event){
var label = event.getCurrentTarget();
label.setString("Key " + String.fromCharCode(keyCode) + "(" + keyCode.toString() + ") was released!");
}
}, statusLabel);
cc.eventManager.addListener({
event: cc.EventListener.ACCELERATION,
callback: function(acc, event){
var target = event.getCurrentTarget();
var ballSize = target.getContentSize();
var ptNow = target.getPosition();
//cc.log("acc: x = " + acc.x + ", y = " + acc.y);
target.x = SpriteAccelerationEventTest._fix_pos(ptNow.x + acc.x * 9.81,
(cc.visibleRect.left.x + ballSize.width / 2.0), (cc.visibleRect.right.x - ballSize.width / 2.0));
target.y = SpriteAccelerationEventTest._fix_pos(ptNow.y + acc.y * 9.81,
(cc.visibleRect.bottom.y + ballSize.height / 2.0), (cc.visibleRect.top.y - ballSize.height / 2.0));
}
}, sprite);
this.runAction(cc.Sequence.create(cc.DelayTime.create(5.0),
cc.CallFunc.create(function () {
this._spriteSaved = true;
this._sprite.retain();
this._sprite.removeFromParent();
}, this),
cc.DelayTime.create(5.0),
cc.CallFunc.create(function () {
this._spriteSaved = false;
this.addChild(this._sprite);
if(!cc.sys.isNative)
cc.eventManager.addListener(listener1, this._sprite);
this._sprite.release();
}, this)
));
this._event1 = dispatcher.addCustomListener(cc.Director.EVENT_AFTER_UPDATE, this.onEvent1.bind(this));
this._event2 = dispatcher.addCustomListener(cc.Director.EVENT_AFTER_VISIT, this.onEvent2.bind(this));
this._event3 = dispatcher.addCustomListener(cc.Director.EVENT_AFTER_DRAW, function(event) {
selfPointer._label3.setString("Draw: " + selfPointer._count3++);
});
this._event4 = dispatcher.addCustomListener(cc.Director.EVENT_PROJECTION_CHANGED, function(event) {
selfPointer._label4.setString("Projection: " + selfPointer._count4++);
});
cc.eventManager.removeListeners(cc.EventListener.TOUCH_ONE_BY_ONE);