1. 程式人生 > >10.cocos2d坐標系

10.cocos2d坐標系

als 顏色 獲取 image font pre pub ttext seq

一、笛卡兒坐標系

  OpenGl坐標系為笛卡兒右手系。x向右,y向上,z向外。在cocos2d-lua中坐標系原點在屏幕的左下角,x向右,y向上,z則是指的zorder(層級)。

二、世界坐標系,本地坐標系

  世界坐標系的原點固定在屏幕的左下角。
  本地坐標是和節點相關的坐標系,每個節點都有獨立的坐標系,是以節點左下角為原點。當節點移動或改變方向時,和該節點關聯的坐標系將隨之移動或者改變方向。

三、錨點

  錨點Anchor Point的兩個參數範圍在0-1 之間,他們是乘數因子。比如(0.5,0.5)表示錨點位於節點長乘於0.5,寬*0.5的位置。可以設置錨點,如果沒設置,默認在(0.5,0.5);

  下面給個代碼看看效果:

 1        //創建大的精靈
 2     CCSprite *big = CCSprite::create();
 3     //設置背景顏色
 4     big->setColor(Color3B(0,0,255));
 5     //設置錨點為左下角
 6     big->setAnchorPoint(ccp(0, 0));
 7     //設置大小
 8     big->setTextureRect(CCRectMake(0, 0, 150, 150));
 9     //設置位置
10     big->setPosition(ccp(100, 100));
11     //加載
12
addChild(big);

技術分享圖片

  如果不忽略錨點

 1         //創建大的精靈
 2     CCSprite *big = CCSprite::create();
 3     //設置背景顏色
 4     big->setColor(Color3B(0,0,255));
 5     //設置錨點為左下角
 6     //big->setAnchorPoint(ccp(0, 0));
 7     big->ignoreAnchorPointForPosition(false);
 8     //設置大小
 9     big->setTextureRect(CCRectMake(0
, 0, 150, 150)); 10 //設置位置 11 big->setPosition(ccp(100, 100)); 12 //加載 13 addChild(big);

技術分享圖片

  忽略錨點

 1 //創建大的精靈
 2     CCSprite *big = CCSprite::create();
 3     //設置背景顏色
 4     big->setColor(Color3B(0,0,255));
 5     //設置錨點為左下角
 6     //big->setAnchorPoint(ccp(0, 0));
 7     big->ignoreAnchorPointForPosition(true);
 8     //設置大小
 9     big->setTextureRect(CCRectMake(0, 0, 150, 150));
10     //設置位置
11     big->setPosition(ccp(100, 100));
12     //加載
13     addChild(big);

技術分享圖片

4、世界坐標和本地坐標的轉化

convertToNodeSpace//將世界坐標轉換為本地坐標系
convertToWorldSpaceAR//將本地坐標系轉換為世界坐標系

總的來說世界坐標就是相對於窗口的坐標,本地坐標就是相對於某個節點的坐標

整體代碼:

T5Coordinate.h
 1 #pragma once
 2 #include "cocos2d.h"
 3 USING_NS_CC;
 4 
 5 class T5Coordinate :public CCLayer
 6 {
 7 public:
 8     static CCScene *scene();
 9     CREATE_FUNC(T5Coordinate);
10     bool init();
11 
12 
13     bool onTouchBegan(CCTouch *pTouch, CCEvent *pEvent);//創建點擊事件
14 };
T5Coordinate.cpp
  1 #include "T5Coordinate.h"
  2 
  3 
  4 CCScene *T5Coordinate::scene()
  5 {
  6     CCScene *scene = CCScene::create();
  7     T5Coordinate *layer = T5Coordinate::create();
  8     scene->addChild(layer);
  9     return scene;
 10 }
 11 
 12 bool T5Coordinate::init()
 13 {
 14     CCLayer::init();
 15     setTouchEnabled(true);//打開觸摸開關
 16     setTouchMode(kCCTouchesOneByOne);//單點
 17 
 18     //創建大的精靈
 19     CCSprite *big = CCSprite::create();
 20     //設置背景顏色
 21     big->setColor(Color3B(0,0,255));
 22     //設置錨點為左下角
 23     //big->setAnchorPoint(ccp(0, 0));
 24     big->ignoreAnchorPointForPosition(true);
 25     //設置大小
 26     big->setTextureRect(CCRectMake(0, 0, 150, 150));
 27     //設置位置
 28     big->setPosition(ccp(100, 100));
 29     //加載
 30     addChild(big);
 31 
 32     //創建小的精靈
 33     CCSprite *little = CCSprite::create();
 34     //設置背景顏色
 35     little->setColor(Color3B(255, 0, 255));
 36     //設置錨點為左下角
 37     little->setAnchorPoint(ccp(0, 0));
 38     //設置大小
 39     little->setTextureRect(CCRectMake(0, 0, 50, 50));
 40     //設置位置
 41     little->setPosition(ccp(100, 100));
 42     //加載到大的精靈裏面,以大的精靈的錨點為坐標原點
 43     big->addChild(little);
 44 
 45     //世界坐標轉化為本地坐標
 46     CCPoint toWorld = big->convertToWorldSpace(little->getPosition());
 47 
 48     CCSprite *little2 = CCSprite::create();
 49     little2->setColor(Color3B(255, 158, 255));
 50     little2->setAnchorPoint(ccp(0, 0));
 51     little2->setTextureRect(CCRectMake(0, 0, 50, 50));
 52     little2->setPosition(ccp(0, 0));
 53     big->addChild(little2);
 54 
 55     //將本地坐標轉化為世界坐標 56     //CCPoint toNode = big->convertToNodeSpace(little2->getPosition());
 57 
 58     //創建動作
 59     //移動到的位置
 60     CCMoveBy *by = CCMoveBy::create(1, ccp(400, 0));
 61     //從移動到的位置回來
 62     CCMoveBy *by2 = (CCMoveBy *)by->reverse();
 63     //創建動作
 64     CCSequence *seq = CCSequence::create(by, by2, NULL);
 65     //把動作給大的精靈
 66     big->runAction(CCRepeatForever::create(seq));
 67 
 68     CCMoveBy *l1by = CCMoveBy::create(1, ccp(0, -100));
 69     CCMoveBy *l1by2 = (CCMoveBy *)l1by->reverse();
 70     CCSequence *l1seq = CCSequence::create(l1by, l1by2, NULL);
 71     little->runAction(CCRepeatForever::create(l1seq));
 72 
 73     CCMoveBy *lby = CCMoveBy::create(1, ccp(0, 100));
 74     CCMoveBy *lby2 = (CCMoveBy *)lby->reverse();
 75     CCSequence *lseq = CCSequence::create(lby, lby2, NULL);
 76     little2->runAction(CCRepeatForever::create(lseq));
 77 
 78     return true;
 79 }
 80 
 81 bool T5Coordinate::onTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
 82 {
 83     //獲取GL坐標
 84     CCPoint pGl = pTouch->getLocation();
 85     //獲取UI坐標
 86     CCPoint pUi = pTouch->getLocationInView();
 87     //世界坐標轉化為相對坐標
 88     CCPoint pnode = this->convertToNodeSpace(pGl);
 89     //UI->GL坐標轉換
 90     CCPoint ptoGl = CCDirector::sharedDirector()->convertToGL(pUi);
 91     //GL->UI坐標的轉換
 92     CCPoint ptoUi = CCDirector::sharedDirector()->convertToUI(pGl);
 93 
 94     //存儲結果並且顯示
 95     char Gl[100];
 96     char Ui[100];
 97     char toGl[100];
 98     char toUi[100];
 99     char node[100];
100     char res[400];
101     sprintf(Gl, "GL坐標:x = %f   y = %f", pGl.x, pGl.y);
102     sprintf(Ui, "UI坐標: x = %f  y = %f", pUi.x, pUi.y);
103     sprintf(toGl, "toGL坐標:x = %f   y = %f", ptoGl.x, ptoGl.y);
104     sprintf(toUi, "toUi坐標:x = %f   y = %f", ptoUi.x, ptoUi.y);
105     sprintf(node , "相對坐標:x = %f   y = %f", pnode.x, pnode.y);
106     sprintf(res, "%s\n%s\n%s\n%s\n%s", Gl, Ui,toGl,toUi,node);
107     MessageBoxA(0, res, "當前點擊坐標", 0);
108     return true;
109 }



10.cocos2d坐標系