zoukankan      html  css  js  c++  java
  • Cocosd-x的坐标系

    OpenGL 坐标系 :   原点在屏幕左下角,x 轴向右,y 轴向上。

    UI坐标体系       :   原点在屏幕左上角,x 轴向右,y 轴向下。

    屏幕坐标系:    UI

    世界坐标系:  也叫绝对坐标系 :   GL,是窗口的坐标系,原点在窗口的左下角。

    本地坐标系: 

    节点坐标系:  是Node的坐标体系,GL。每个节点都有自己的坐标系,

          所以和世界体系不同的是,它的原点是节点的左下角。

    !!!当一个节点调用setPosition时, 使用的参数是它的父节点(渲染树)的坐标体系。

    !!!CCLayer,CCScene默认大小和窗口一样,所以它的坐标体系和世界坐标体系重合。

    GL坐标和世界坐标之间的转化:
    CCDirector::getInstance()->convertToUI(参数);
    CCDirector::getInstance()->convertToGL(参数);

    世界坐标 和 节点坐标 的转化(常用)
    CCNode::convertToNodeSpace(CCPoint ptInWorld);
    CCNode::convertToWorldSpace(CCPoint ptInNode);

    按照锚点为原点进行转化的方法
    CCNode::convertToNodeSpaceAR(CCPoint ptInWorld);
    CCNode::convertToWorldSpaceAR(CCPoint ptInNode);

    .h文件

     1 #ifndef __example2_H__
     2 #define __example2_H__
     3 
     4 #include<cocos2d.h>
     5 USING_NS_CC;
     6 
     7 class example2 : public CCLayerColor
     8 {
     9 public:
    10     static CCScene* scene();
    11     CREATE_FUNC(example2);
    12     bool init();
    13     bool onTouchBegan(Touch* touch, Event* event);
    14     Sprite* _sprite;
    15 };
    16 #endif
    View Code

    .cpp文件

     1 #include "example2.h"
     2 
     3 CCScene* example2::scene()
     4 {
     5     CCScene* s = CCScene::create();
     6     example2* layer = example2::create();
     7     s->addChild(layer);
     8     return s;
     9 
    10 }
    11 bool example2::init()
    12 {
    13     CCLayerColor::initWithColor(ccc4(255,255,0,255));
    14 
    15 
    16     CCSize size = CCDirector::getInstance()->getWinSize();
    17     CCLayerColor* layer = CCLayerColor::create(ccc4(255,0,255,255),size.width/2,size.height/2);
    18     addChild(layer);
    19 
    20     CCSprite* sprite = CCSprite::create("CloseNormal.png");
    21     _sprite = sprite;
    22     layer->addChild(sprite);
    23     sprite->setPosition(ccp(100,100));
    24 
    25     layer->setPosition(ccp(100,100));
    26 
    27     //CCPoint psSpriteInWorld = layer->convertToWorldSpace(sprite->getPosition());
    28     //CCPoint psSpriteInworld1 = layer->convertToNodeSpace(sprite->getPosition()); 错误写法
    29     //CCLog("x = %f,y = %f",psSpriteInWorld.x,psSpriteInWorld.y);
    30     //CCLog("x = %f,y = %f", psSpriteInWorld1.x, psSpriteInWorld1.y);
    31 
    32     setTouchEnabled(true);
    33     setTouchMode(kCCTouchesOneByOne);
    34 
    35     return true;
    36 }
    37 
    38 
    39 bool example2::onTouchBegan(CCTouch* touch, CCEvent*)
    40 {
    41     CCPoint ptInUI = touch->getLocationInView();
    42     CCPoint ptInGL = touch->getLocation();
    43 
    44     CCPoint ptUIConvert = CCDirector::getInstance()->convertToGL(ptInUI);
    45     CCLog("x = %f, y = %f",ptInGL.x,ptInGL.y);
    46     CCLog("x = %f, y = %f",ptUIConvert.x,ptUIConvert.y);
    47     //判断是否点中了精灵
    48     //获取精灵的矩形,获取触摸点,判断触摸点是否在矩形内。
    49 
    50     CCRect rc = _sprite->boundingBox();//获取精灵的矩形,属于UI坐标系
    51     CCPoint touchPoint = touch->getLocation();//GL
    52     //转化为同一个体系才好判断
    53     //if (rc.containsPoint(_sprite->getParent()->convertToNodeSpace(touchPoint))) 下面的更简单
    54     if (rc.containsPoint(_sprite->getParent()->convertTouchToNodeSpace(touch)))
    55     {//直接把touch传进去,而不用使用touch->getLocation()方法了
    56         
    57         CCLog("ok I am in log");
    58     }
    59     return true;
    60 }
    View Code

    一个疑问: bool ccTouchBegan(Touch* touch, Event* event);提示 无法重写CCLayer的该方法,应该已经设置为fianl 。

    3.0之前不是final的吗?

  • 相关阅读:
    mysql命令集锦
    linux 删除文件名带括号的文件
    linux下的cron定时任务
    struts2文件下载的实现
    贴一贴自己写的文件监控代码python
    Service Unavailable on IIS6 Win2003 x64
    'style.cssText' is null or not an object
    "the current fsmo could not be contacted" when change rid role
    远程激活程序
    新浪图片病毒
  • 原文地址:https://www.cnblogs.com/tom987690183/p/4380720.html
Copyright © 2011-2022 走看看