你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> 詳解iOS游戲開辟中Cocos2D的坐標地位關系

詳解iOS游戲開辟中Cocos2D的坐標地位關系

編輯:IOS開發綜合

接觸Cocos2D有段時光了,明天特地研討了下Cocos2D坐標系中各類地位關系,anchor屬性,CCNode坐標和地圖坐標轉換。

    先看一段代碼:

-(id) init 

    // always call "super" init 
    // Apple recommends to re-assign "self" with the "super" return value 
    if( (self=[super init])) { 
        CCTMXTiledMap *gameWorld = [CCTMXTiledMap tiledMapWithTMXFile:@"PositionText.tmx"]; 
        [self addChild:gameWorld]; 
        CGSize WinSize = [[CCDirector sharedDirector] WinSize]; 
        CCLOG(@"gameWorld.mapSize.width:%.2f  gameWorld.mapSize.height:%.2f",gameWorld.mapSize.width, gameWorld.mapSize.height); 
        CCLOG(@"gameWorld.tileSize.width: %.2f  gameWorld.tileSize.height:%.2f", gameWorld.tileSize.width, gameWorld.tileSize.height); 
        CCLOG(@"WinSize.width:%.2f  winSize.height:%.2f", winSize.width, winSize.height); 
         
        CCSprite *pointSprite = [CCSprite spriteWithFile:@"point.png"]; 
        [gameWorld addChild:pointSprite z:2 tag:1]; 
        pointSprite.position = ccp(20,20); 
        pointSprite.anchorPoint = ccp(0.5, 0.5); 
         
         
        CCSprite *rectSprite = [CCSprite spriteWithFile:@"myrect.png"]; 
        [gameWorld addChild:rectSprite z:1 tag:1]; 
        rectSprite.position = ccp(20,20); 
        rectSprite.anchorPoint = ccp(0.5, 0.5);      
    } 
    return self; 
}

1、加載地圖:

CCTMXTiledMap *gameWorld = [CCTMXTiledMap tiledMapWithTMXFile:@"PositionText.tmx"]; 

2、獲得手機屏幕年夜小


CGSize winSize = [[CCDirector sharedDirector] winSize]; 

3、地圖格子數:

gameWorld.mapSize (10,10)    
 
4、地圖格子年夜小:

gameWorld.tileSize    (20,20)
 
5、全部地圖年夜小:

     gameWorld.mapSize * gameWorld.tileSize;

     固然這裡所說的是地圖格子是個正方形,非正方形也輕易啦。
6、anchor屬性

     1) 添加一個精靈,這個精靈是個像素為1*1的白色圖片,設置坐標為20,20,即在地圖的第一個格子的右上角,設置anchorPoint為(0.5, 0.5)
     2) 再添加一個精靈,這個精靈是個像素為20*20的藍色圖片,設置坐標為20,20,即在地圖的第一個格子的右上角,異樣設置anchorPoint為(0.5, 0.5)

         運轉後果是矩形精靈的中間和在點精靈的地位,即矩形精靈的錨點為第一個格子的右上角(20,20)坐標 處
     去失落兩個精靈的anchorPoint屬性

          運轉後果和下面雷同

     設置rectSprite的anchorPoint為ccp(0,0)

          運轉後果是矩形精靈的左下角與點精靈重合。
     設置rectSprite的anchorPoint為ccp(1,1)

          運轉後果是矩形精靈的右上角與點精靈重合。
     同理設置ccp(0.5, 1) , ccp(1, 0.5)等

     由下面可以得出:
         1)anchorPoint屬性默許為ccp(0.5, 0.5)

         2)當設置(0,0)時是以左下角為錨點

         3)當設置(1, 1)時是以右上角角為錨點

         4)由此可以本身推論究竟該把錨點設置在哪裡

    Cocos2D應用的是OpenGL坐標系

    OpenGL坐標系:原點在左下角, X軸向右,Y軸向上

    IPhone屏幕坐標系:原點在左上角,X軸向右,Y軸向下
   很簡略的斷定辦法:因為Cocos2D的坐標系的原點在左下角。所以精靈外部坐標原點也是左下角,即當anchorPoint為(0, 0)時即以左下角為錨點,為(1,1)時就以右上角為錨點,固然(0.5, 0.5)就是以精靈中間為錨點了。由此可以推算出-2,-3....    2, 3....   等值時精靈錨點坐標。


7、坐標轉換

-(id) init 

    // always call "super" init 
    // Apple recommends to re-assign "self" with the "super" return value 
    if( (self=[super init])) { 
        CCTMXTiledMap *gameWorld = [CCTMXTiledMap tiledMapWithTMXFile:@"PositionText.tmx"]; 
        [self addChild:gameWorld]; 
        CGSize winSize = [[CCDirector sharedDirector] winSize]; 
        CCLOG(@"gameWorld.mapSize.width:%.2f  gameWorld.mapSize.height:%.2f",gameWorld.mapSize.width, gameWorld.mapSize.height); 
        CCLOG(@"gameWorld.tileSize.width: %.2f  gameWorld.tileSize.height:%.2f", gameWorld.tileSize.width, gameWorld.tileSize.height); 
        CCLOG(@"winSize.width:%.2f  winSize.height:%.2f", winSize.width, winSize.height); 
         
        CCSprite *pointSprite = [CCSprite spriteWithFile:@"point.png"]; 
        [gameWorld addChild:pointSprite z:2 tag:1]; 
        pointSprite.position = ccp(20,20); 
        //pointSprite.anchorPoint = ccp(0.5, 0.5); 
         
         
        CCSprite *rectSprite = [CCSprite spriteWithFile:@"myrect.png"]; 
        [gameWorld addChild:rectSprite z:1 tag:1]; 
        rectSprite.position = ccp(40,40); 
        rectSprite.anchorPoint = ccp(2, 2);  
         
        [self setIsTouchEnabled:YES]; 
    } 
    return self; 

 
- (void) registerWithTouchDispatcher { 
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:INT_MIN + 1 swallowsTouches:YES]; 

 
- (BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { 
    CGPoint point = [touch locationInView: [touch view]]; 
    CCLOG(@"Screen coordX: %.2f coordY: %.2f", point.x, point.y); 
     
    point = [[CCDirector sharedDirector] convertToGL: point]; 
    CCLOG(@"OpenGL coordX: %.2f coordY: %.2f", point.x, point.y); 
 
    point = [self convertToNodeSpace: point]; 
    CCLOG(@"CCNode1 coordX: %.2f coordY: %.2f", point.x, point.y); 
 
    point = [self convertTouchToNodeSpace: touch]; 
    CCLOG(@"CCNode2 coordX: %.2f coordY: %.2f", point.x, point.y); 
         
        point = [[CCDirector sharedDirector] convertToUI:point]; 
        CCLOG(@"UIView coordX: %.2f coordY: %.2f", point.x, point.y); 
        
        point = [rectSprite convertTouchToNodeSpaceAR:touch]; 
        CCLOG(@"TouchAR coordX: %.2f coordY: %.2f", point.x, point.y); 
        return YES; CCNode 

 
- (void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent*)event { 
     

 
- (void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent*)event { 
     


     下面的代碼添加了UIView的事宜處置。因為屏幕和Cocos2D采取分歧的坐標系,所以我們須要停止坐標轉換。

     1)起首獲得屏幕坐標

     2)將屏幕坐標轉換為OpenGL的坐標

     3)將OpenGL的坐標轉換為Cocos2D的坐標。

     從運轉成果可以看出Node1和Node2打印出的坐標雷同。由於Cocos2D給我們寫了covertTouchToNodeSpace辦法,可以看看它的源碼:

- (CGPoint)convertTouchToNodeSpace:(UITouch *)touch 

    CGPoint point = [touch locationInView: [touch view]]; 
    point = [[CCDirector sharedDirector] convertToGL: point]; 
    return [self convertToNodeSpace:point]; 
}

     至於為何OpenGL和Node1輸入既然雷同,為何還要應用convertToNodeSpace,因為才能無限,願望年夜牛們能給告知我謎底。
    UIView輸入的是將Cocos2D坐標轉換為屏幕即quartz坐標。
   4) convertTouchToNodeSpaceAR是將觸摸點轉換為絕對於rectSprite的坐標


8、斷定觸摸點能否在制訂的精靈上

- (BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { 
    CGPoint point = [touch locationInView: [touch view]]; 
    CCLOG(@"Screen coordX: %.2f coordY: %.2f", point.x, point.y); 
     
    point = [[CCDirector sharedDirector] convertToGL: point]; 
    CCLOG(@"OpenGL coordX: %.2f coordY: %.2f", point.x, point.y); 
 
    point = [self convertToNodeSpace: point]; 
    CCLOG(@"CCNode1 coordX: %.2f coordY: %.2f", point.x, point.y); 
 
    point = [self convertTouchToNodeSpace: touch]; 
    CCLOG(@"CCNode2 coordX: %.2f coordY: %.2f", point.x, point.y); 
     
    //point = [[CCDirector sharedDirector] convertToUI:point]; 
    //CCLOG(@"UIView coordX: %.2f coordY: %.2f", point.x, point.y); 
    CCLOG(@"%d", rectSprite.textureRect.size.width); 
     
    CGRect rect = [rectSprite textureRect]; 
    rect = CGRectMake(0, 0, rect.size.width, rect.size.height); 
    CCLOG(@"orgX: %.2f  orgY: %.2f  width:%.2f  height: %.2f",rect.origin.x, rect.origin.y, rect.size.width, rect.size.height); 
     
    point = [rectSprite convertTouchToNodeSpaceAR:touch]; 
    CCLOG(@"TouchAR coordX: %.2f coordY: %.2f", point.x, point.y); 
    if(CGRectContainsPoint(rect, point)) { 
        CCLOG(@"You touched in the rectSprite"); 
    } 
    return YES;  


9、獲得觸摸的是哪一個tile,並轉變該tile.

CCSprite *rectSprite; 
CCTMXTiledMap *gameWorld; 
int speed = 10; 
-(id) init 

    // always call "super" init 
    // Apple recommends to re-assign "self" with the "super" return value 
    if( (self=[super init])) { 
        gameWorld = [CCTMXTiledMap tiledMapWithTMXFile:@"PositionText.tmx"]; 
        [self addChild:gameWorld]; 
        CGSize winSize = [[CCDirector sharedDirector] winSize]; 
        CCLOG(@"gameWorld.mapSize.width:%.2f  gameWorld.mapSize.height:%.2f",gameWorld.mapSize.width, gameWorld.mapSize.height); 
        CCLOG(@"gameWorld.tileSize.width: %.2f  gameWorld.tileSize.height:%.2f", gameWorld.tileSize.width, gameWorld.tileSize.height); 
        CCLOG(@"winSize.width:%.2f  winSize.height:%.2f", winSize.width, winSize.height); 
         
        CCSprite *pointSprite = [CCSprite spriteWithFile:@"point.png"]; 
        [gameWorld addChild:pointSprite z:2 tag:1]; 
        pointSprite.position = ccp(20,20); 
        pointSprite.anchorPoint = ccp(0.5, 0.5); 
         
         
        rectSprite = [CCSprite spriteWithFile:@"myrect.png"]; 
        [gameWorld addChild:rectSprite z:0 tag: 3]; 
        rectSprite.position = ccp(40, 40); 
        rectSprite.anchorPoint = ccp(0, 0); 
                 
        [self setIsTouchEnabled:YES]; 
    } 
    return self; 

 
- (void) registerWithTouchDispatcher { 
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:INT_MIN + 1 swallowsTouches:YES]; 

 
- (BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { 
    CGPoint point = [touch locationInView: [touch view]]; 
    point = [self convertTouchToNodeSpace: touch]; 
    CCLOG(@"CCNode2 coordX: %.2f coordY: %.2f", point.x, point.y); 
 
    CGPoint tilePos = [self tilePosition:point]; 
    if(tilePos.x == -1 || tilePos.y == -1) return NO; 
    CCTMXLayer *ly = [gameWorld layerNamed:@"Layer 0"]; 
    if([ly tileGIDAt:tilePos] != 3) { 
        [ly setTileGID:0 at: tilePos]; 
    } 
    return YES;  

 
- (void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent*)event { 
     

 
- (void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent*)event { 
     

 
 
- (CGPoint) tilePosition:(CGPoint)pos { 
    CGPoint point; 
    CCTMXLayer *ly = [gameWorld layerNamed:@"Layer 0"]; 
    if(ly== nil) { 
        CCLOG(@"Error: Layer not found!"); 
        return ccp(-1, -1); 
    } 
    CGSize layerSize = [ly layerSize]; 
    CGSize tileSize = [gameWorld tileSize]; 
    int x = pos.x / tileSize.width; 
    int y = layerSize.height - pos.y / tileSize.height; 
    if((x >= 0) && (x < layerSize.width) && (y >= 0) && (y < layerSize.height)) { 
        point = ccp(x, y); 
    } else { 
        point = ccp(-1, -1); 
    } 
    if(point.x < 0) return ccp(-1, -1); 
    if(point.y < 0) return ccp(-1, -1); 
    if(point.x >= layerSize.width) return ccp(-1, -1); 
    if(point.y >= layerSize.height) return ccp(-1, -1); 
    CCLOG(@"%d, %d", x, y); 
    return point; 

【詳解iOS游戲開辟中Cocos2D的坐標地位關系】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved