Iphone Cocos2d 坐标系
2011-09-24 15:47
239 查看
基本的两个坐标系:屏幕坐标系和GL坐标系。
屏幕坐标系:x轴朝右,y轴朝下。默认原点在左上角。
GL坐标系 :x轴朝右,y轴朝上。默认原点在左下角。
调用CCNode类的setPosition函数,它使用的就是GL坐标系。
在处理触摸事件时CCTouch对象中的坐标就是屏幕坐标系。
讲屏幕坐标系转换成GL坐标系调用:convertTouchToNodeSpace方法,它是CCNode的成员方法:
其实现如下
- (CGPoint)convertTouchToNodeSpace:(UITouch *)touch
{
CGPoint point = [touch locationInView: [touch view]];
point = [[CCDirector sharedDirector] convertToGL: point];
return [self convertToNodeSpace:point];
}
调用时:
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchPoint =[self convertTouchToNodeSpace:touch];
return YES;
}
cocos2d的事件在CCLayer中处理。
1.CCLayer的isTouchEnabled,和registerWithTouchDispatcher 默认是不响应,需要手动设置
self.isTouchEnabled = YES;
-(void) registerWithTouchDispatcher
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self
priority:1
swallowsTouches:YES];
[[CCTouchDispatcher sharedDispatcher] addStandardDelegate:self
priority:0];
[super registerWithTouchDispatcher];
}
2.CCLayer需要实现CCStandardTouchDelegate,CCTargetedTouchDelegate两个协议
CCStandardTouchDelegate是 实现多点触控,委托方法如下:
@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;
CCTargetedTouchDelegate是 一点触控,委托方法如下:
- (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;
屏幕坐标系:x轴朝右,y轴朝下。默认原点在左上角。
GL坐标系 :x轴朝右,y轴朝上。默认原点在左下角。
调用CCNode类的setPosition函数,它使用的就是GL坐标系。
在处理触摸事件时CCTouch对象中的坐标就是屏幕坐标系。
讲屏幕坐标系转换成GL坐标系调用:convertTouchToNodeSpace方法,它是CCNode的成员方法:
其实现如下
- (CGPoint)convertTouchToNodeSpace:(UITouch *)touch
{
CGPoint point = [touch locationInView: [touch view]];
point = [[CCDirector sharedDirector] convertToGL: point];
return [self convertToNodeSpace:point];
}
调用时:
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchPoint =[self convertTouchToNodeSpace:touch];
return YES;
}
cocos2d的事件在CCLayer中处理。
1.CCLayer的isTouchEnabled,和registerWithTouchDispatcher 默认是不响应,需要手动设置
self.isTouchEnabled = YES;
-(void) registerWithTouchDispatcher
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self
priority:1
swallowsTouches:YES];
[[CCTouchDispatcher sharedDispatcher] addStandardDelegate:self
priority:0];
[super registerWithTouchDispatcher];
}
2.CCLayer需要实现CCStandardTouchDelegate,CCTargetedTouchDelegate两个协议
CCStandardTouchDelegate是 实现多点触控,委托方法如下:
@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;
CCTargetedTouchDelegate是 一点触控,委托方法如下:
- (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;
相关文章推荐
- Cocos2d-X中的坐标系
- cocos2d-x缺少Default-568h@2x.png导致iPhone app 上下黑白的解决办法
- cocos2d-x 坐标系
- Cocos2d-x坐标系介绍
- 【iOS-Cocos2d游戏开发】Cocos2d-iPhone动作Action-组合动作
- Cocos2d-X 坐标系(3.2版本)
- 【iOS-Cocos2d游戏开发】cocos2d 坐标系使用
- cocos2d-x iphone真机测试出现闪屏现象
- cocos2d坐标系
- 关于cocos2d中的坐标系
- Cocos2d-x坐标系介绍
- Cocos2d-x之坐标系学习
- cocos2d 坐标系的理解
- cocos2d-iphone 区分ipad和iphone
- 【玩转cocos2d-x之十】cocos2d-x坐标系
- cocos2d-iPhone 点滴
- cocos2d-x缺少Default-568h@2x.png导致iPhone app 上下黑白的解决办法
- 安装Cocos2D-iPhone的方法
- iPhone开发之游戏篇(3)--- cocos2d的Actions构成
- 【Cocos2d-x游戏开发】浅谈游戏中的坐标系