cocos2dx——实现儿童节贺卡01

有些时间没有用到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()));//淡入切换场景,可以使用多种效果
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值