加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos 2d 简单动画实现

发布时间:2020-12-14 21:11:06 所属栏目:百科 来源:网络整理
导读:动画的主题是14张图片,一般来讲需要找比较相似的图片连续的播放,以达到一种迷惑人眼的效果,下面这段程序主要基于 Cocos 2d 游戏开发平台实现,中间涉及到了 声音播放引擎, 以及一些 图层 精灵的处理 。 addChild(layer); // return the scene return sce
动画的主题是14张图片,一般来讲需要找比较相似的图片连续的播放,以达
到一种迷惑人眼的效果,下面这段程序主要基于 Cocos 2d 游戏开发平台
实现,中间涉及到了 声音播放引擎, 以及一些 图层 精灵的处理 。
#include "HelloWorldScene.h"

USING_NS_CC;

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();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();

    //添加背景图片
    CCSprite* bk = CCSprite::create("1.jpg");// 注意此处的文件需放到 项目的Resource 目录下
    addChild(bk);
    bk->setPosition(ccp(winSize.width / 2,winSize.height / 2));


    //添加背景音乐
    CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("吻别.mp3",true);

    Vector<SpriteFrame *> aniframe;// 使用一个 Vector 存放 SpriteFrame 指针

    CCSprite *sprite;

    char str[20];// 定义一个字符数组 用于存放 图片名称

    for(int i = 1; i <= 14; i++)
    {
        sprintf(str,"%d.jpg",CCRectMake(0,0,639,423));
        if(i == 1)
        {
            sprite = CCSprite::createWithSpriteFrame(frame);
            sprite->setPosition(ccp(winSize.width / 2,winSize.height / 2));
            addChild(sprite);
        }
        aniframe.insert(NULL,frame);

    }
    for(int i = 14; i >= 1; i--)
    {
        sprintf(str,frame);
    }


    // 生成一个动画实体,并将上面添加的图片放入其中 

    CCAnimation *animation = CCAnimation::createWithSpriteFrames(aniframe,0.2f);// 没0.2s切换一次

    // 将动画的实体放到一个动画中去
    CCAnimate *animate = CCAnimate::create(animation);
    // 执行动画
    sprite ->runAction(CCRepeatForever::create(animate));
    return true;
}

待续未完。。。。。。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读