源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

iOS开发中使用cocos2d添加触摸事件的方法

  • 时间:2021-05-04 08:55 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:iOS开发中使用cocos2d添加触摸事件的方法
CCLayer类是用来接收触摸输入的。不过你要首先启用这个功能才可以使用它。你通过设置isTouchEnabled为YES来让层接收触摸事件:
[u]复制代码[/u] 代码如下:
self.isTouchEnabled = YES;
此项设定最好在init方法中设置。你可以在任何时间将其设置为NO或者YES。 一旦启用isTouchEnabled属性,许多与接收触摸输入相关的方法将会开始被调用。这些事件包括:当新的触摸开始的时候,当手指在触摸屏上移动的时候,还有在用户手指离开屏幕以后。很少会发生触摸事件被取消的情况,所以你可以在大多数情况下忽略它,或者使用ccTouchesEnded方法来处理。 当手指首次触摸到屏幕时调用的方法:
[u]复制代码[/u] 代码如下:
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent*)event
手指在屏幕上移动时调用的方法:
[u]复制代码[/u] 代码如下:
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent*)event
当手指从屏幕上提起时调用的方法:
[u]复制代码[/u] 代码如下:
-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent*)event
当触摸事件被取消时调用的方法:
[u]复制代码[/u] 代码如下:
-(void) ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent*)event
取消事件的情况很少发生,所以在大多数情况下它的行为和触摸结束时相同。 因为触摸事件由Cocoa TouchAPI接收,所以触摸的位置必须被转换为OpenGL的坐标。 以下是一个用来转换坐标的方法:
[u]复制代码[/u] 代码如下:
-(CGPoint) locationFromTouches:(NSSet *)touches  {      UITouch *touch = [touches anyObject];      CGPoint touchLocation = [touch locationInView: [touch view]];      return [[CCDirector sharedDirector] convertToGL:touchLocation];  } 
默认情况下,层接收到的事件和苹果UIResponder类接收到的是一样的。cocos2d也支持有针对性的触摸处理。和普通处理的区别是:它每次只接收一次触摸,而UIResponder总是接收到一组触摸。有针对性的触摸事件处理只是简单的把一组触摸事件分离开来,这样就可以根据游戏的需求提供所需的触摸事件。更重要的是,有针对性的处理允许你把某些触摸事件从队列里移除。这样的话,如果触摸发生在屏幕某个指定的区域,你会比较容易识别出来;识别出来以后你就可以把触摸标记为已经处理,并且其它所有的层都不再需要对这个区域再次做检查。 在你的层中添加以下方法可以启用有针对性的触摸事件处理:
[u]复制代码[/u] 代码如下:
-(void) registerWithTouchDispatcher  {      [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:kCCMenuTouchPriority swallowsTouches:YES];  }
注:如果你把registerWithTouchDispatcher方法留空,你将不会接收到任何触摸事件!如果你想保留此方法,而且使用它的默认处理方式,你必须调用[super registerWithTouchDispatcher]这个方法。 现在,你将使用一套有点不一样的方法来代替默认的触摸输入处理方法。它们几乎完全一样,除了一点:用 (UITouch *)touch 代替 (NSSet *)touches 作为方法的第一个参数:
[u]复制代码[/u] 代码如下:
-(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里面,添加
[u]复制代码[/u] 代码如下:
[self setIsTouchEnabled:YES];
以下方法是cocos2d类库的方法:
[u]复制代码[/u] 代码如下:
-(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]; }
接下来就实现方法:
[u]复制代码[/u] 代码如下:
- (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;     }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部