关于cocos2dx坐标及几个重要函数说明

关于cocos2dx坐标及几个重要函数说明,第1张

概述关于cocos2dx的坐标系的文章很多,本文不再赘述,只是我一直不明白那几个函数是怎么变换的,今日专门研究了一下,以下是自己的理解。 auto sprite1 = Sprite::create("CloseNormal.png"); sprite1->setPosition(Point(20,40)); sprite1->setAnchorPoint(Point(0,0));

关于cocos2dx的坐标系的文章很多,本文不再赘述,只是我一直不明白那几个函数是怎么变换的,今日专门研究了一下,以下是自己的理解。

 auto sprite1 = Sprite::create("Closenormal.png");    sprite1->setposition(Point(20,40));    sprite1->setAnchorPoint(Point(0,0));    this->addChild(sprite1);  //此时添加到的是世界坐标系,也就是OpenGL坐标系        auto sprite2 = Sprite::create("Closenormal.png");    sprite2->setposition(Point(-5,-20));    sprite2->setAnchorPoint(Point(1,1));    this->addChild(sprite2); //此时添加到的是世界坐标系,也就是OpenGL坐标系        //将 sprite2 这个节点的坐标ccp(-5,-20) 转换为 sprite1节点 下的本地(节点)坐标系统的 位置坐标    auto  point1 = sprite1->convertToNodeSpace(sprite2->getposition());        //将 sprite2 这个节点的坐标ccp(-5,-20) 转换为 sprite1节点 下的世界坐标系统的 位置坐标    auto  point2 = sprite1->convertToWorldspace(sprite2->getposition());        log("position = (%f,%f)",point1.x,point1.y); //(-25,-60)    log("position = (%f,point2.x,point2.y); //(15,20)
这段代码大家应该比较熟悉,我这里说明我对这两个函数的理解

1、sprite1->convertToNodeSpace(sprite2->getposition());

说明如下图所示,将sprite2的点转换为sprite1节点坐标系下的点,即sprite2的参考原点为sprite1的点。可以比作将sprite2加入sprite1节点下面



2、 sprite1->convertToWorldspace(sprite2->getposition());

如下图所示,相当于将sprite2所在坐标系添加到了sprite1下面,



总结:convertToNode 是参考点的转移,convertToWorld是坐标系的转移。

总结

以上是内存溢出为你收集整理的关于cocos2dx坐标及几个重要函数说明全部内容,希望文章能够帮你解决关于cocos2dx坐标及几个重要函数说明所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1000138.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-21
下一篇2022-05-21

发表评论

登录后才能评论

评论列表(0条)

    保存