有些时间没有用到cocos了,净忙着复习,屌丝过个儿童节貌似更屌丝,还是敲代码吧,看着别人在朋友圈里发照片,我的照片呢?算了,给外甥做个贺卡,也当复习复习cocos了!
先说一说贺卡大体的效果:
1.进入欢迎界面,实现淡入淡出的效果;
2.实现一个幻灯片播放,每张切换都用淡入淡出效果,同时添加粒子特效;
3.幻灯片播放之后,进入祝贺界面:几个字体通过各种动作实现一些效果;
4.添加背景音乐的播放(在windows上不能用mp3,这让我纠结了半天,谁让之前一直不加音乐呢)
下面先实现以下欢迎界面吧,创建项目,直接使用helloworld界面,在这里helloworldScene还是好名字
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::Layer
{
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
void replaceSceneCallback();
};
#endif // __HELLOWORLD_SCENE_H__
接下来实现文件
#include "HelloWorldScene.h"
#include "ThreeScene.h"
#include "SimpleAudioEngine.h"
USING_NS_CC;
using namespace CocosDenshion;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//
// 1. super init first
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
auto bg = Sprite::create("bg.png");
bg->setScale(0.4);
this->addChild(bg);
bg->setPosition(visibleSize.width / 2, visibleSize.height / 2);
bg->setOpacity(50); //设置透明度
auto fadein = FadeIn::create(2.0f); //淡入
auto fadeout = FadeOut::create(2.0f);//淡出
auto callfunc = CallFunc::create(CC_CALLBACK_0(HelloWorld::replaceSceneCallback, this));//回调,动作完成后切换另一个场景
auto sequence = Sequence::create(fadein, fadeout, callfunc,NULL);
bg->runAction(sequence);
return true;
}
void HelloWorld::replaceSceneCallback() {
Director::getInstance()->replaceScene(TransitionFade::create(1.2f, ThreeScene::createScene()));//淡入切换场景,可以使用多种效果
}