cocos2d-x test学习[1]

controller.cpp

1 std::function<TestScene*()> callback;//一个是返回值,一个是参数。返回值是TestScene*,参数是()里的东西
1 Controller g_aTestNames[] = {
2 
3     //
4     // TESTS MUST BE ORDERED ALPHABETICALLY
5     //     violators will be prosecuted
6     //
7     { "ActionManager", [](){return new ActionManagerTestScene(); } },

这里是lambda的语法。

C++11 的 lambda 表达式规范如下:

  • [ capture ] ( params ) mutable exception attribute -> ret { body }   
  • [ capture ] ( params ) -> ret { body }      
  • [ capture ] ( params ) { body }    
  • [ capture ] { body }     

如果 lambda 代码块中包含了 return 语句,则该 lambda 表达式的返回类型由 return 语句的返回类型确定。
另外,capture 指定了在可见域范围内 lambda 表达式的代码内可见得外部变量的列表,具体解释如下:

  •     [a,&b] a变量以值的方式呗捕获,b以引用的方式被捕获。
  •     [this] 以值的方式捕获 this 指针。
  •     [&] 以引用的方式捕获所有的外部自动变量。
  •     [=] 以值的方式捕获所有的外部自动变量。
  •     [] 不捕获外部的任何变量。

此外,params 指定 lambda 表达式的参数。

 ActionManagerTest.cpp

1 grossini->runAction( Sequence::create( 
2                                                 MoveBy::create(1, Vec2(150,0)),
3                                                 CallFuncN::create(CC_CALLBACK_1(LogicTest::bugMe,this)),
4                                                 nullptr) 
5                         );
1 auto pCallback = CallFunc::create(CC_CALLBACK_0(StopActionTest::stopAction,this)); // 注意这里没有传进参数就是0咯

CC_CALLBACK_N (摘自:http://my.oschina.net/u/555701/blog/219844

1 // new callbacks based on C++11
2 #define CC_CALLBACK_0(__selector__,__target__, ...) std::bind(&__selector__,__target__, ##__VA_ARGS__)
3 #define CC_CALLBACK_1(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, ##__VA_ARGS__)
4 #define CC_CALCC_CALLBACK_1(HelloWorld::menuCloseCallback,this)LBACK_2(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, ##__VA_ARGS__)
5 #define CC_CALLBACK_3(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3 ##__VA_ARGS__)

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

CC_CALLBACK_1不事先指定的有一个,也就是可以传进一个参数。不事先指定的参数就在LogicTest::bugMe的参数列表上。 这个个数和具体的函数个数是一致的。

但是下面这个还是没有理解,得之后再看下引擎的接口应该就知道了。

 1     auto action = Sequence::create(
 2         Place::create(Vec2(200,200)),
 3         Show::create(),
 4         MoveBy::create(1, Vec2(100,0)),
 5         CallFunc::create( CC_CALLBACK_0(ActionSequence2::callback1,this)),
 6         CallFunc::create( CC_CALLBACK_0(ActionSequence2::callback2,this,_grossini)),
 7         CallFunc::create( CC_CALLBACK_0(ActionSequence2::callback3,this,_grossini,0xbebabeba)),
 8         nullptr);
 9 
10     _grossini->runAction(action);

CC_CALLBACK_0已经不需要了,直接用std::bind就行了。

1     auto action1 = Sequence::create(
2                         MoveBy::create(2, Vec2(200,0)),
3                         CallFunc::create( std::bind(&ActionCallFunction::callback1, this) ),
1 l->setPosition(VisibleRect::center().x, VisibleRect::top().y-75);

Cocos2d引擎家族,包括Cocos2d-x,Cocos2d-JS的坐标系统是继承于OpenGL的右手笛卡尔坐标系(Right-handed Cartesian Coordinate System)。

Cocos2d坐标系的特点是:

  •     原点为屏幕左下角
  •     向右为X轴正方向
  •     向上为Y轴正方向
 1     auto pMove1 = MoveBy::create(2, Vec2(200, 0));
 2     auto pMove2 = MoveBy::create(2, Vec2(-200, 0));
 3     auto pSequenceMove = Sequence::createWithTwoActions(pMove1, pMove2);
 4     auto pRepeatMove = RepeatForever::create(pSequenceMove);
 5     pRepeatMove->setTag(kTagSequence); 
 6     
 7     auto pScale1 = ScaleBy::create(2, 1.5f);
 8     auto pScale2 = ScaleBy::create(2, 1.0f/1.5f);
 9     auto pSequenceScale = Sequence::createWithTwoActions(pScale1, pScale2);
10     auto pRepeatScale = RepeatForever::create(pSequenceScale);
11     pRepeatScale->setTag(kTagSequence); // 用同一个tag
1 void StopAllActionsTest::stopAction(float time)
2 {
3     auto sprite = getChildByTag(kTagGrossini);
4     sprite->stopAllActionsByTag(kTagSequence);
5 }

这里将pRepeatMove和pRepeatScale设置成同一个tag,然后stopAllActionsByTag就可以停止这两个运用了。

 ActionEaseTest.cpp

 1  // rotate and jump
 2     auto jump1 = JumpBy::create(4, Vec2(-s.width+80, 0), 100, 4);
 3     auto jump2 = jump1->reverse();
 4     auto rot1 = RotateBy::create(4, 360*2);
 5     auto rot2 = rot1->reverse();
 6     
 7     auto seq3_1 = Sequence::create(jump2, jump1, nullptr);
 8     auto seq3_2 = Sequence::create( rot1, rot2, nullptr);
 9     auto spawn = Spawn::create(seq3_1, seq3_2, nullptr);
10     auto action = Speed::create(RepeatForever::create(spawn), 1.0f);

Spawn是同时动作,Sequence是顺序动作。

 CocosDenshionTest.cpp

 1 class Button : public Node//, public TargetedTouchDelegate
 2 {
 3 public:
 4     static Button *createWithSprite(const char *filePath)
 5     {
 6         auto b = new (std::nothrow) Button();
 7         if (b && !b->initSpriteButton(filePath)) {
 8             delete b;
 9             b = nullptr;
10         }
11         b->autorelease();
12         return b;
13     }

自定义button类,继承自Node。

1 Button *btnPlay = Button::createWithText("play");
2     btnPlay->onTriggered([]() {
3         SimpleAudioEngine::getInstance()->playBackgroundMusic(MUSIC_FILE, true);
4     });

点击触发事件用OnTriggered和匿名函数。

1     // preload background music and effect
2     SimpleAudioEngine::getInstance()->preloadBackgroundMusic( MUSIC_FILE );//用于预加载背景音乐,其中 pszFilePath 为音乐文件所在的目录位置。
3     SimpleAudioEngine::getInstance()->preloadEffect( EFFECT_FILE ); //用于预加载音效文件,其中 pszFilePath 为音效文件所在的目录位置。

加载音乐和音效通常是一个耗时的过程,为了防止由即时加载产生的延迟导致实际播放与游戏不协调的现象发生,在播放音效和背景音乐之前,需要预加载音乐文件。

原文地址:https://www.cnblogs.com/linyx/p/4286162.html