查看官方文档可知,cocos2d支持两种不同的处理触摸事件的方法
分别为:standard touch delegate 和 Targeted touch delegate
1、standard touch delegate
1.1定义
@protocol CCStandardTouchDelegate <NSObject> @optional - (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; - (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; - (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; - (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event; @end
1.2使用
在cclayer子类中加入::即可触发事件。
self.isTouchEnabled = YES;
2、Targeted touch delegate
2.1定义
@protocol CCTargetedTouchDelegate <NSObject> - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event; @optional // touch updates: - (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event; - (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event; - (void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event; @end
2.2使用
- (void)onEnter { [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES]; [super onEnter]; } - (void)onExit
{
[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];[super onExit];
}
但是官方的说法是cclayer子类必须重写registerWithTouchDispatcher,我这种方法是在非cclayer中实现响应才会用到的,但我自己测试并不可行,不知到是不是文档太旧了,还是如何。