iOS开发中使用cocos2d添加触摸事件的方法
CCLayer类是用来接收触摸输入的。不过你要首先启用这个功能才可以使用它。你通过设置isTouchEnabled为YES来让层接收触摸事件:
self.isTouchEnabled = YES;
此项设定最好在init方法中设置。你可以在任何时间将其设置为NO或者YES。
一旦启用isTouchEnabled属性,许多与接收触摸输入相关的方法将会开始被调用。这些事件包括:当新的触摸开始的时候,当手指在触摸屏上移动的时候,还有在用户手指离开屏幕以后。很少会发生触摸事件被取消的情况,所以你可以在大多数情况下忽略它,或者使用ccTouchesEnded方法来处理。
当手指首次触摸到屏幕时调用的方法:
-(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
取消事件的情况很少发生,所以在大多数情况下它的行为和触摸结束时相同。
因为触摸事件由Cocoa TouchAPI接收,所以触摸的位置必须被转换为OpenGL的坐标。
以下是一个用来转换坐标的方法:
-(CGPoint) locationFromTouches:(NSSet *)touches
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView: [touch view]];
return [[CCDirector sharedDirector] convertToGL:touchLocation];
}
默认情况下,层接收到的事件和苹果UIResponder类接收到的是一样的。cocos2d也支持有针对性的触摸处理。和普通处理的区别是:它每次只接收一次触摸,而UIResponder总是接收到一组触摸。有针对性的触摸事件处理只是简单的把一组触摸事件分离开来,这样就可以根据游戏的需求提供所需的触摸事件。更重要的是,有针对性的处理允许你把某些触摸事件从队列里移除。这样的话,如果触摸发生在屏幕某个指定的区域,你会比较容易识别出来;识别出来以后你就可以把触摸标记为已经处理,并且其它所有的层都不再需要对这个区域再次做检查。
在你的层中添加以下方法可以启用有针对性的触摸事件处理:
-(void) registerWithTouchDispatcher
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:kCCMenuTouchPriority swallowsTouches:YES];
}
注:如果你把registerWithTouchDispatcher方法留空,你将不会接收到任何触摸事件!如果你想保留此方法,而且使用它的默认处理方式,你必须调用[super registerWithTouchDispatcher]这个方法。
现在,你将使用一套有点不一样的方法来代替默认的触摸输入处理方法。它们几乎完全一样,除了一点:用 (UITouch *)touch 代替 (NSSet *)touches 作为方法的第一个参数:
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {}-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {}
-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {}
-(void) ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event {}
这里很重要的一点是:ccTouchBegan返回的是一个布尔值(BOOL)。如果你返回了YES,那就意味着你不想让当前的触摸事件传导到其它触摸事件处理器。你实际上是“吞下了”这个触摸事件。
下面来看一个完整的例子:
在自己的layer里面,添加
[self setIsTouchEnabled:YES];
以下方法是cocos2d类库的方法:
-(void) setIsTouchEnabled:(BOOL)enabled{
if( isTouchEnabled_ != enabled ) {
isTouchEnabled_ = enabled;
if( isRunning_ ) {
if( enabled )
[self registerWithTouchDispatcher];
else {
CCDirector *director = [CCDirector sharedDirector];
[[director touchDispatcher] removeDelegate:self];
}
}
}
}
//这句是关键
-(void) registerWithTouchDispatcher
{
CCDirector *director = [CCDirector sharedDirector];
[[director touchDispatcher] addStandardDelegate:self priority:0];
}
接下来就实现方法:
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
// for( UITouch *touch in touches ) {
// CGPoint location = [touch locationInView: [touch view]];
//
// location = [[CCDirector sharedDirector] convertToGL: location];
// CGPoint touchPos = location;
//
// NSLog(@"point==%@",NSStringFromCGPoint(touchPos));
// }
UITouch* touch = [touches anyObject];
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL: location];
//self.position = location;
NSLog(@"point==%@",NSStringFromCGPoint(location));
}
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
self.isMoving = FALSE;
}
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
// you can set up a check here if you're not interested in handling every touch.
// For example if your player is already moving, return no...
BOOL handleTouch = FALSE;
if (!self.isMoving) {
handleTouch = TRUE;
self.isMoving = TRUE;
}
return handleTouch;
}
您可能感兴趣的文章:
- COCOS2d中添加UIButton不响应触摸事件---iOS开发之最灵异事件之2
- 【iOS-Cocos2d游戏开发之十一】使用Box2d物理系统以及在cocos2d框架添加Box2d物理系统lib包的方法
- 【iOS-Cocos2d游戏开发之十一】使用Box2d物理系统以及在cocos2d框架添加Box2d物理系统lib包的方法
- 【iOS-Cocos2d游戏开发之十一】使用Box2d物理系统以及在cocos2d框架添加Box2d物理系统lib包的方法
- 【iOS-Cocos2d游戏开发之十一】使用Box2d物理系统以及在cocos2d框架添加Box2d物理系统lib包的方法 推荐
- 【iOS-Cocos2d游戏开发之十一】使用Box2d物理系统以及在cocos2d框架添加Box2d物理系统lib包的方法
- 举例讲解iOS应用开发中hitTest触摸事件的编写方法
- ios UITouch 触摸事件的使用方法
- cocos2d-x 中的pvr.ccz压缩图片的使用方法【iOS游戏开发征文】
- iOS开发:使用响应链方法实现传值及事件传递
- 【Cocos2d游戏开发之十一】使用Box2d物理系统以及在cocos2d框架添加Box2d物理系统lib包的方法
- 游戏开发随手记:cocos2d ccLayer响应触摸事件方法
- iOS开发过程中触摸事件的处理(hitTest的使用)
- iOS开发封装带有Button的UIView控件,使用代理给button添加点击事件
- 【Cocos2d游戏开发之十一】使用Box2d物理系统以及在cocos2d框架添加Box2d物理系统lib包的方法
- cocos2d-x 中的pvr.ccz压缩图片的使用方法【iOS游戏开发征文】
- iOS开发封装带有Button的UIView控件,使用target-action模式给button添加点击事件
- iOS开发封装带有Button的UIView控件,使用block给button添加点击事件
- 【iOS-Cocos2d(2.x) 游戏开发之一】自定义CCSprite中-(id) init 重写需注意& 2.x版本中注册触屏事件代码&简述cocos2d/x/Unity3D引擎使用感想!
- [ IOS-Cocos2d-x 游戏开发之一] -cocos2d-x触摸事件优先级的探究与实践