cocos2dx 持续学习(一) 声音、精灵、动作

来源:互联网 发布:mac怎么做手机铃声 编辑:程序博客网 时间:2024/04/30 16:50

参考学习资料:Cocos2d-x+3.x游戏开发之旅 

记录声音,控件及基础动作的学习,为便于记录,Cocos2d在之后会简称为coco。

(一)播放声音

播放声音需要包含 SimpleAudioEngine.h

程序示例:

CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("sky_city.mp3",true);   摘自上面的教程书籍

playBackgroundMusic适用于播放长音乐,对于一些战斗,技能最好采用 playEffect

(二)精灵

Sprite

程序示例:

Sprite* dog=Sprite::create("dog.png");

dog.setPosition(Point(100,300));

this->addChild(dog);

(三)基础运动:

MoveTo    移动到参数指定的坐标位置

MoveBy   移动参数指定的偏移量

缩放:

ScaleTo   缩放到指定倍数

ScaleBy   在现在缩放的基础上,再执行指定缩放倍数

闪烁:

Blink  Blink* blink=Blink::create(3.0f,3) ; 第一个参数为动作执行的时间,第二个参数为闪烁次数

跳跃:

JumpBy

重复执行指定动作:

RepeatForever

组合运动:

Sequence 和 Spawn

MoveBy* moveBy = MoveBy::create(2.2f, Point(40, 20));

JumpBy* jumpBy = JumpBy::create(3.0f, Point(50, 1), 100, 5);

RotateBy* rotateBy = RotateBy::create(2.5f, 220, 10);

Action* actions = Sequence::create(moveBy, jumpBy, rotateBy, NULL);  摘自书中源码

(四) 动作监听

coco 支持lambda函数的形式进行函数回调。

示例代码:

Size visibleSize = Director::getInstance()->getVisibleSize();

Sprite* sprite= Sprite::create("sprite.png");
sprite->setPosition(Point(100, 200));
this->addChild(sprite);

MoveTo* moveToHome = MoveTo::create(10.0f, Point(visibleSize.width / 2, visibleSize.height / 2));

auto callbackFunc = [=](){ 
sprite->setScale(2.0);
};


CallFunc* callFunc = CallFunc::create(callbackFunc);

Action* actions = Sequence::create(moveToHome, callFunc, NULL);


sprite->runAction(actions);


其中,形如[](){}; 构成lambda函数。具体参见《Cocos2d-x+3.x游戏开发之旅 》中详细介绍。



原创粉丝点击