一、Cocos2d-x 3.x 坐标系
来源:http://www.cocos.com/doc/article/index?type=cocos2d-x&url=/doc/cocos-docs-master/manual/framework/native/v3/coordinate-system/zh.md
1、使用笛卡尔坐标系中定义的右手系原点在左下角,x向右,y向上,z向外,OpenGL坐标系为笛卡尔右手坐标系
2、屏幕坐标系和Cocos2d坐标系
* 标志坐标系使用和OpenGL不同的坐标系,而Cocos2d使用和OpenGL相同的坐标系
* iOS、Android、Windows phone在开发应用时使用的是标志屏幕坐标系,原点为屏幕左上角,x向右,y向下
3、世界坐标系和本地坐标系
* 世界坐标系也叫绝对坐标系,是游戏开发中建立的概念。因此,“世界”指游戏世界。
* cocos2d中的元素是有父子关系的层级结构,通过Node的setPosition设定元素的位置,使用的是相对与其父节点的本地坐标系而非世界坐标系。最后在绘制的时候cocos2d会把这些元素的本地坐标映射成世界坐标系坐标。
* 本地坐标系也叫相对坐标系,是和节点相关联的坐标系。每个节点都有独立的坐标系,当节点移动或改变方向时,和该节点关联的坐标系将随之移动或改变方向。
4、锚点
* 将一个节点添加到父节点里面时,需要设置其在父节点上的位置,本质上是设置节点的锚点在父节点坐标系上的位置。
* Anchor Point的两个参数都在0~1之间。它们表示的并不是像素点,而是乘数因子。(0.5,0,5)表示Anchor Point位于节点长度乘0.5和宽度乘0.5的地方,即节点中心。
* 在Cocos2d-x中Layer的Anchor Point为默认值(0,0)并且默认忽略锚点,所以要设置ignoreAnchorPointForPosition(false),其他Node的默认值为(0.5,0.5)
5、VertexZ,PositionZ 和 zOrder
* VertexZ是OpenGL坐标系中的Z值
* PositionZ是Cocos2d-x坐标系中的Z值
* zOrder是Cocos2d-x本地坐标系中Z值
* 在实际开发中我们只需要关注zOrder
* 通过setPositionZ接口来设置PositionZ,PositionZ的值即为OpenGL的z值VertexZ
* 同样节点的PositionZ也是决定了该节点的渲染顺序,值越大,但是与zOrder不同的区别在于,PositionZ是全局渲染顺序即在根节点上的渲染顺序,而zOrder则是局部渲染顺序,即该节点在其父节点上的渲染顺序,值越大在其父节点上渲染越靠前
6、触摸点
————————————————————————————————————————————————————————————————————————————————
二、Cocos2dx中的宏
1、CC_SYNTHESIZE(type,variableName,functionName);//用于简单变量的初始化,自动创建getter、setter方法
2、CC_SYNTHESIZE_RETAIN(type,variableName,functionName);//可以用于自己创建的类变量,并且可以保留一次引用计数
3、CCASSERT(true,”sdfsdfs”)//前面的参数如果是true,则程序继续执行,如果是false,程序中断
三、Cocos2dx序列帧动画
http://www.cocos.com/doc/article/index?type=cocos2d-x&url=/doc/cocos-docs-master/manual/framework/native/v3/frame-animation/zh.md
1、手动添加 Animation、Animate、Sprite
【注】使用Animation类描述一个动画,而精灵显示动画的动作则是一个Animate对象。动画动作Animate是精灵显示动画的动作,它有一个动画对象创建,并由精灵执行。
* 创建方法:
* 手动添加序列帧到Animation类
* 创建Animation对象,往其中添加动画序列帧,设置动画隔间时间、播放参数,将Animation通过Animate转换成Action,然后使用,精灵的runAction播放Animate
* 使用文件初始化Animation类
2、文件添加 AnimationCache、Animate、Sprite
* 将创建好的动画plist文件添加到动画缓存AnimatioCache中,plist文件中包含了序列帧的相关信息
auto cache = AnimationCache::getInstance();
cache->addAnimationWithFile(“animation/animation-2.plist”);
auto animation2 = cache->getAnimation(“dance_1”);
auto nation = Animate::create(animation2);
Sprite tamara = Sprite::create();
tamara->runAction(Sequence::create(action2,action2->reverse(),null));
* 用动画缓存初始化Animation实例
* 用Animate实例来播放序列帧动画
3、动画缓存
四、基础知识
1、typedef和define
* typedef int p;用p可以表示int,是在编译器运行的时候执行
* define 是宏定义,在编译之前执行的,
五、引入CocoStudioUI
1、#include “cocos2d.h”
#include “CocosGUI.h”
#include “CocoStudio.h”
#include “CCSGUIReader.h”
USING_NS_CC;
using namespace ui;
using namespace std;
using namespace cocostudio;
2、Mac中在Header FilePaths路径下,添加"$(SRCROOT)/../cocos2d/cocos/editor-support/cocostudio”,引入类库
3、Windows下,需要引入CocoStudio的引用
4、
const char *TEST_CUSTOMER_FONT_UI_PATH = "TestCustomFont.json";//测试自定义字体场景
custom = static_cast<Layout *>(cocostudio::GUIReader::getInstance()->widgetFromJsonFile(TEST_CUSTOMER_FONT_UI_PATH));
addChild(custom, 1);
uiPanel = dynamic_cast<Layout *>(custom->getChildByName("uiPanel"));
_testTxt = dynamic_cast<Text *>(uiPanel->getChildByName("Custom"));
_testTxt->setFontName("FZY4JW--GB1-0.ttf");
_testTxt->setText(_testTxt->getStringValue());
六、添加自定义字体
1、在Mac下需要将rtf字体文件添加在resources中,再将rtf字体添加在info.plist文件中的Fonts provided by application 项里面
2、在需要显示的字体中调用xxxx.ttf字体全名
string intWithThousandthNotation(int source) {
bool isNegative = false;
if(source < 0) isNegative = true;
string ret = std::to_string(abs(source));
int len = ret.length();
for (int i = len - 3; i > 0; i -= 3) {
if (ret.at(i - 1) != '-') {
ret.insert(i, ",");
}
}
if(isNegative){
ret = "-" + ret;
}
return ret;
}
/**
* 截取多余字符串转变成点号
*/
string cutExcessStringConvertdot(const string &str, Text *text) {
string ret = str;
int length = str.length();
std::string tempStr = str;
Size size = Size(0, 0);
int fontSize = text->getFontSize();
int with = text->getSize().width;
int cutIndex = 0;
int chinsesIndex = 0;
for (int i = 0; i < length;) {
std::string substring = tempStr.substr(i, 1);
if ((substring.c_str()[0] & 0x80) != 0) //是汉字
{
substring = tempStr.substr(i, 3);
i += 3;
chinsesIndex = i;
}
else {
i++;
}
auto tempLabel = LabelTTF::create(substring.c_str(), "", fontSize);
tempLabel->setHorizontalAlignment(TextHAlignment::LEFT);
Size tmpLsize = tempLabel->getContentSize();
size.width += tmpLsize.width;
//显示宽度已经大于容器大小
if (size.width >= with) {
//求出截取的索引位置
cutIndex = i;
break;
}
}
//说明有截取 并且有字符串可以被替换成 ..
if (cutIndex != 0 && cutIndex - 3 > 0) {
//截取位置不能是中文
if (cutIndex - chinsesIndex >= 3) {
ret.replace(cutIndex - 3, str.length() - cutIndex + 3, "...");
}
else {
ret.replace(cutIndex - (cutIndex - chinsesIndex), str.length() - cutIndex + (cutIndex - chinsesIndex), "...");
}
}
log("==== show NickName:%s", ret.c_str());
return ret;
}