首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

替CCSprite添加touch事件

2012-09-25 
为CCSprite添加touch事件?#import Foundation/Foundation.h#import cocos2d.h@interface SpTouchable

为CCSprite添加touch事件

?

#import <Foundation/Foundation.h>#import "cocos2d.h"@interface SpTouchable : CCSprite <CCTargetedTouchDelegate>{}- (BOOL)containsTouchLocation:(UITouch *)touch;@end

?实现类:

? ?#import "SpTouchable.h"

@implementation SpTouchable- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event  {  if ( ![self containsTouchLocation:touch] )       {          return NO;      }  NSLog(@"tag : %d",self.tag);return YES;  }  - (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event  {  //  CGPoint touchPoint = [touch locationInView:[touch view]];  //  touchPoint = [[CCDirector sharedDirector] convertToUI:CGPointMake(touchPoint.x, touchPoint.y)];  // self.position = CGPointMake(touchPoint.x, touchPoint.y);  } - (void) onEnter  {      [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];      [super onEnter];  }  - (void) onExit  {      [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];      [super onExit];  } - (CGRect)rect  {      CGSize s = [self.texture contentSize];      return CGRectMake(-s.width / 2, -s.height / 2, s.width, s.height);  }  - (BOOL)containsTouchLocation:(UITouch *)touch  {  //NSLog(@"location");    return CGRectContainsPoint(self.rect, [self convertTouchToNodeSpaceAR:touch]);  }  @end

热点排行