环境配置安装可以在博客搜我就不写了 C++需要有入门的水平 学习连接: 官方文档
本人环境Cocos 2d-x 4.0 & vs2022
基本概念 导演(Director):Cocos2d-x 使用导演的概念,Director是一个共享的单例对象,可以在代码中的任何地方调用。 一般用于执行控制场景替换和转换。
场景(Scene)在游戏开发过程中要有 一个主菜单,几个关卡和一个结束场景,主菜单场景,这个场景是由很多小的对象拼接而成,所有的对象组合在一起,形成了最终的结果. 场景是被 渲染器(renderer) 画出来的。渲染器负责渲染精灵和其它的对象进入屏幕。
场景图(Scene Graph)一种安排场景内对象的数据结构,它把场景内所有的 节点(Node) 都包含在一个 树(tree) 上。(场景图虽然叫做"图",但实际使用一个树结构来表示)。 Cocos2d-x 使用 中序遍历,先遍历左子树,然后根节点,最后是右子树.另一点要考虑的是,z-order(设置顺序) 为负的元素,z-order 为负的节点会被放置在左子树,非负的节点会被放在右子树。实际开发的过程中,你可以按照任意顺序添加对象,他们会按照你指定的 z-order 自动排序.
通过 Scene 的 addChild() 方法构建场景图 // Adds a child with the z-order of -2, that means // it goes to the "left" side of the tree (because it is negative) scene->addChild(title_node, -2); // When you don't specify the z-order, it will use 0 scene->addChild(label_node); // Adds a child with the z-order of 1, that means // it goes to the "right" side of the tree (because it is positive) scene->addChild(sprite_node, 1);精灵(Sprite)
精灵是您在屏幕上移动的对象,它能被控制。可以被配置的属性,比如:位置,旋转角度,缩放比例,透明度,颜色 等等。
//Sprite的创建
auto mySprite = Sprite::create("mysprite.png");
// this is how to change the properties of the sprite
mySprite->setPosition(Vec2(500, 0)); // 设置位置
mySprite->setRotation(40); // 设置旋转角度
mySprite->setScale(2.0); // 设置缩放比例 2.0是放大俩倍
mySprite->setAnchorPoint(Vec2(0, 0)); // vec2(x,y) 设置坐标
动作(Action)
- 让精灵在场景中移动 , 还可以创建一个动作 序列(Sequence) ,让精灵按照这个序列做连续的动作,在动作过程中你可以改变精灵的位置,旋转角度,缩放比例等等。
auto mySprite = Sprite::create("Blue_Front1.png");
//先移动 到 x = 50 的位置 , 然后在2s内 移动到 y = 10 的位置
auto moveBy = MoveBy::create(2, Vec2(50,10));
mySprite->runAction(moveBy);
// 在2s内移动到特定的位置
auto moveTo = MoveTo::create(2, Vec2(50,10));
mySprite->runAction(moveTo);
序列(Sequence)
多个动作按照特定顺序的一个排列,当然反向执行这个序列也是可以的。
auto mySprite = Node::create(); // move to point 50,10 over 2 seconds auto moveTo1 = MoveTo::create(2, Vec2(50,10)); // move from current position by 100,10 over 2 seconds auto moveBy1 = MoveBy::create(2, Vec2(100,10)); // move to point 150,10 over 2 seconds auto moveTo2 = MoveTo::create(2, Vec2(150,10)); // create a delay auto delay = DelayTime::create(1); mySprite->runAction(Sequence::create(moveTo1, delay, moveBy1, delay.clone(), moveTo2, nullptr)); // 结尾是空指针 myNode->runAction(Spawn::create(moveTo1, moveBy1, moveTo2, nullptr)); // 使用 spawn对象 让 动作同时执行节点关系
被附属和附属的关系 , 父节点做什么 , 子节点就做什么. 不是所有的父节点属性都会被自动应用到子节点,如改变父节点的锚点只会影响转换效果(比例缩放,位置变化,角度旋转,变形等),不会影响子节点锚点,子节点的锚点总会是左下角 (0,0)。
日志输出- (debug 使用) : log()
// a simple string
log("This would be outputted to the console");
// a string and a variable
string s = "My variable";
log("string is %s", s);
// a double and a variable
double dd = 42;
log("double is %f", dd);
// an integer and a variable
int i = 6;
log("integer is %d", i);
// a float and a variable
float f = 2.0f;
log("float is %f", f);
// a bool and a variable
bool b = true;
if (b == true)
log("bool is true");
else
log("bool is false");



