加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2d-x基本概念

发布时间:2020-12-14 17:28:20 所属栏目:百科 来源:网络整理
导读:游戏引擎是一种特殊的软件,它提供游戏开发时需要的常见功能;引擎会提供许多组件,使用这些组件能缩短开发时间,让游戏开发变得更简单;专业引擎通常会能比自制引擎表现出更好的性能。游戏引擎通常会包含渲染器、2D/3D图形元素、碰撞检测、物理引擎、声音、

游戏引擎是一种特殊的软件,它提供游戏开发时需要的常见功能;引擎会提供许多组件,使用这些组件能缩短开发时间,让游戏开发变得更简单;专业引擎通常会能比自制引擎表现出更好的性能。游戏引擎通常会包含渲染器、2D/3D图形元素、碰撞检测、物理引擎、声音、控制器支持、动画等部分。

一个游戏界面一般由菜单(Menu)、几个精灵(Sprite)和几个标签(Label)组成。

导演(director)

一个常见的Director任务是控制场景替换和转换。Director是一个共享的单例对象,可以在代码中的任何地方调用。

场景(Scene)

场景是被渲染器(renderer)画出来的,渲染器负责渲染精灵和其它的对象进入屏幕。

场景图(Scene Graph)

场景图是一种安排场景内对象的数据结构,它把场景内所有的节点(Node)都包含在一个树上。cocos2d-x使用中序遍历。
另一点需要考虑的是,z-order为负的元素,z-order为负的节点会被放置在左子树,非负的节点会被放在右子树
// 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);

由于是中序遍历,所以z-order值大的节点对象会后绘制,值小的节点对象先绘制。

精灵(Sprite)

精灵是你在屏幕上能控制的对象。Sprite很容易被创建,它有一些可以被配置的属性。
// This is how to create a 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); // sets both the scale of the X and Y axis uniformly

mySprite->setAnchorPoint(Vec2(0,0));
锚点(anchor point), 所有的节点(Node)都有锚点值,锚点是节点对象在计算坐标位置时的一个基准点。0,0是精灵的左下角。

动作(Action)

创建一个场景,在场景里面增加精灵只是完成一个游戏的第一点,接下来我们要解决的问题就是,怎么让精灵动起来。动作(Action)就是用来解决这个问题的,你还可以创建一个动作序列(Sequence),让精灵按照这个序列做连续的动作。
auto mySprite = Sprite::create("Blue_Front1.png");

// Move a sprite 50 pixels to the right,and 10 pixels to the top over 2 seconds.
auto moveBy = MoveBy::create(2,Vec2(50,10));
mySprite->runAction(moveBy);

// Move a sprite to a specific location over 2 seconds.
auto moveTo = MoveTo::create(2,10));
mySprite->runAction(moveTo);

序列(Sequence)

序列就是多个动作按照特定的顺序的一个排列,当然反向执行这个序列也是可以的。
auto mySprite = Node::create();

// move to point 50,10 over 2 seconds
auto moveTo1 = MoveTo::create(2,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));

通过引擎中的Spawn对象,可以让多个动作同时被解析执行。
auto myNode = Node::create();

auto moveTo1 = MoveTo::create(2,10));
auto moveBy1 = MoveBy::create(2,10));
auto moveTo2 = MoveTo::create(2,10));

myNode->runAction(Spawn::create(moveTo1,nullptr));

节点关系

如果两节点被添加到一个父子关系中,那么父节点的属性变化会被自动应用到子节点中。需要注意的是, 不是所有的父节点属性都会被自动应用到子节点。

日志输出

有时,在你的游戏正在运行的时候,为了了解程序的运行过程或是为了查找一个BUG,你想看一些运行时信息,可以使用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");

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读