Cocos2d-x   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了了解Cocos2d-x整个引擎框架——helloworld大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

目的

———了解生命周期

初始化 -》启动完成(启动导演单例 -》 显示视图-》显示场景)

-》从前台进入后台

-》从后台进去前台


———了解开发 编译 运行 调试

1 执行顺序?

(显示视图《-显示层《-显示菜单)


2 如何在界面显示控件?

创建精灵-》设置位置-》添加到父节点(addChild)


3 设置的控件在界面上显示不全?

1> getContentSize.width() or getContentSize.height() ,显示的坐标根据内容高宽做调整。

2> setAnchorPoint() 设置锚点(即,修改控件的中心点),显示的坐标根据内容高宽做调整。


6addChild后,如何调整显示顺序?谁在谁的上层显示?

void Node::addChild(Node *child, int zOrder)//  zOrder小的显示在底层,zOrder大的显示在顶层。  顶层会覆盖显示底层的控件。

5 调试

1> 在visual studio中

vsIDE自带, 断点 + 单步调试 + 变量值查看

2> 通用,使用CCLog 打印日志


_________________________扩展阅读_________________________

CC_CALLBACK原理及应用

c++ 11基础 :

std::function

类模版std::function是一种通用、多态的函数封装。std::function的实例可以对任何可以调用的目标进行存储、复制、和调用操作,这些目标包括函数、lambda表达式、绑定表达式、以及其它函数对象等。

用法示例:

①保存自由函数

?
1
2
3
4
5
6
7
8
void printA( int a)
{
@H_289_197@ cout<<a<<endl;
}
std::function< ( a)>func;
func=printA;
func(2);

运行输出: 2

②保存lambda表达式

2
()>func_1=[](){Cout<< "Helloworld" <<endl;};
func_1();

运行输出:Hello world

③保存成员函数

8
9
10
struct Foo{
Foo( num):num_(num){}
print_add( i) const {Cout<<num_+i<< '\n' ;}
num_;
};
//保存成员函数
( Foo&, )>f_add_display=&Foo::print_add;
Foofoo(2);
f_add_display(foo,1);

运行输出: 3

bind bind是一组用于函数绑定的模板。在对某个函数进行绑定时,可以指定部分参数或全部参数,也可以不指定任何参数,还可以调整各个参数间的顺序。对于未指定的参 数,可以使用占位符_1、_2、_3来表示。_1表示绑定后的函数的第1个参数,_2表示绑定后的函数的第2个参数,其他依次类推

下面通过程序例子了解一下用法:

?
10
11
@H_673_402@ 12
13
14
15
16
17
18
19
20
21
22
@H_450_424@ 23
24
25
26
27
28
29
30
31
32
33
34
35
@H_598_450@ 36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<iostream>
using namespace std;
class A
{
public :
fun_3( k,monospace!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important; BACkground:none!important">m)
{
cout<<k<< "" <<m<<endl;
}
};
fun( x,monospace!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important; BACkground:none!important">y,monospace!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important; BACkground:none!important">z)
{
cout<<x<< <<y<< <<z<<endl;
}
fun_2( &a,monospace!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important; BACkground:none!important">&b)
{
a++;
b++;
cout<<a<< <<b<<endl;
}
@H_480_607@ @H_844_213@main( argc, const char *argv[])
{
auto f1=bind(fun,1,2,3); //表示绑定函数fun的第一,二,三个参数值为:123
f1(); //print:123
f2=bind(fun,placeholders::_1,placeholders::_2,3);
@H_944_646@//表示绑定函数fun的第三个参数为3,而fun的第一,二个参数分别有调用f2的第一,二个参数指定
f2(1,2); //print:123
f3=bind(fun,3);
@H_944_646@//表示绑定函数fun的第三个参数为3,而fun的第一,二个参数分别有调用f3的第二,一个参数指定
@H_944_646@//注意:f2和f3的区别。
f3(1,0)!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important; BACkground:none!important">//print:213
@H_343_675@
n=2;
@H_814_209@m=3;
f4=bind(fun_2,n,placeholders::_1);
f4(m); //print:34
cout<<m<<endl; //print:4说明:bind对于不事先绑定的参数,通过std::placeholders传递的参数是通过引用传递的
cout<<n<<endl; //print:2说明:bind对于预先绑定的函数参数是通过值传递的
Aa;
f5=bind(&A::fun_3,a,placeholders::_2);
f5(10,20); //print:1020
,monospace!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important; BACkground:none!important">)>fc=std::bind(&A::fun_3,std::placeholders::_1,std::placeholders::_2);
fc(10,0)!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important; BACkground:none!important">//print:1020
return 0;
}

CC_CALLBACK

一、通过 HelloWorldScene 中的 closeItem 开始

在cocos2d-x 2.x 版本中:

5
CCMenuItemImage*pCloseItem=CCMenuItemImage::create(
"CloseNormal.png" "CloseSELEcted.png" this @H_814_209@menu_SELEctor(HelloWorld::menuCloseCallBACk));

在cocos2d-x 3.0 版本中:

?
4
closeItem=MenuItemImage::create(
CC_CALLBACK_1(HelloWorld::menuCloseCallBACk,monospace!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important; BACkground:none!important">));


HelloWorld::menuCloseCallBACk(Object*pSender)
Director::geTinstance()->end();
#if(CC_TARGET_PLATFORM==CC_PLATFORM_IOS)
exit (0);
#endif
}

注意到在3.0版本中使用到 CC_CALLBACK_1 这样一个宏定义。

?
5
//newcallBACksbasedonC++11
#defineCC_CALLBACK_0(__SELEctor__,__target__,...)std::bind(&__SELEctor__,##__VA_ARGS__)
#defineCC_CALLBACK_1(__SELEctor__,##__VA_ARGS__)
#defineCC_CALCC_CALLBACK_1(HelloWorld::menuCloseCallBACk,this)LBACK_2(__SELEctor__,std::placeholders::_2,##__VA_ARGS__)
#defineCC_CALLBACK_3(__SELEctor__,std::placeholders::_3##__VA_ARGS__)

这里主要注意两点:一是std::bind,二是##_VA_ARGS_; ##_VA_ARGS_是可变参数宏

原来还有 CC_CALLBACK_0 1 2 3;而其中又有什么区别呢?

1、首先我们看看3.0版本中MenuItemImage的create方法:

?
1 @H_403_962@menuItemImage*MenuItemImage::create(std::string&normalImage,monospace!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important; BACkground:none!important">std::string&SELEctedImage,monospace!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important; BACkground:none!important">ccMenuCallBACk&callBACk)

其中的回调参数是 ccMenuCallBACk

1
typedef
(Object*)>ccMenuCallBACk

来这里使用到了 C++ 中的 function 语法。

注意到 在CC_CALLBACK_ 的宏定义的中使用到的是 C++ 的 bind 语法,怎么不一致了呢?-- 见下面第四点 function


2、看回CC_CALLBACK_ 的宏定义

原来CC_CALLBACK_ 的宏定义中后面的 0 1 2 3分别表示的是 不事先指定回调函数参数的个数。

例如说CC_CALLBACK_ 1 表示的是,回调函数中不事先指定参数是一个,而事先指定的回调函数的参数 可以任意多个

而且要注意到其中 不指定回调函数参数 和 指定回调函数参数 的顺序,注意不事先指定的在前,事先指定的在后

下面通过例子说明这一点:

假设回调函数:

//aSELEctorcallBACk
@H_814_209@menuCloseCallBACk(Object*pSender,monospace!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important; BACkground:none!important">a,monospace!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important; BACkground:none!important">b);
@H_253_1058@
9
HelloWorld::menuCloseCallBACk(Object*pSender,monospace!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important; BACkground:none!important">b)
std::cout<<a<< <<b<<std::endl;
Director::geTinstance()->end();
#if(CC_TARGET_PLATFORM==CC_PLATFORM_IOS)
(0);
#endif
}

注意到在回调函数中输出 a b

?
4
:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; font-family:Consolas,2));


注意中其中 指定了两个参数 1 2

运行,在 点击closeItem 的时候,就会输出这两个事先指定的参数 1 2。

那么,不事先指定的参数是在什么时候传入的呢?

?
17 @H_314_1146@menuItem::activate()
if (_enabled)
{
(_callBACk)
{
_callBACk( );
}
(kScriptTypeNone!=_scriptTypE)
{
BasicScriptDatadata( );
ScriptEventscriptEvent(kMenuClickedEvent,&data);
ScriptENGIneManager::geTinstance()->getScriptENGIne()->sendEvent(&scriptEvent);
}
}
}

注意到其中的 _callBACk(this); 对了,这个时候就传入了 这个不事先指定的回调函数参数。

这样,closeItem 的回调函数的void HelloWorld::menuCloseCallBACk(Object* pSender,int a,int b) 的三个参数都知道了。

第一个 不事先指定,在menu item调用 activate 的时候,_callBACk(this) 传入,this 也即是这个 menu item;第二、三个参数是事先指定的 1,2。


已经知道CC_CALLBACK_ 的宏定义是 std::bind 那么我们可以直接使用std::bind。

如下:

?
4 @H_614_1262@std::bind(&HelloWorld::menuCloseCallBACk,2));

大佬总结

以上是大佬教程为你收集整理的了解Cocos2d-x整个引擎框架——helloworld全部内容,希望文章能够帮你解决了解Cocos2d-x整个引擎框架——helloworld所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:cocoscocos2ddhelloworldx了解引擎整个框架
猜你在找的Cocos2d-x相关文章
其他相关热搜词更多
phpJavaPython程序员load如何string使用参数jquery开发安装listlinuxiosandroid工具javascriptcap