第八章动作和动画-动画

Cocos2d-x学习笔记


动画又可以分为场景过渡动画和帧动画。

帧动画

帧动画就是按照一定时间间隔、一定的顺序、一帧一帧地显示图片。在Cocos2d-x中播放帧动画设计两个类:AnimateAnimationAnimation是动画类,它保存很多动画帧,Animate类是动作类继承于ActionInterval类,属于间隔动作类,它的作用是将Animateion定义的动画转换成动作进行执行。

实例

HelloWorld.h文件

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::Layer
{
    bool isPlaying;//播放标识,ture说明正在播放,false说明停止播放
    cocos2d::Sprite * sprite;
public:
    static cocos2d::Scene* createScene();

    virtual bool init();

    // a selector callback
    void onAction(cocos2d::Ref * pSender);

    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__

HelloWorld.cpp文件

#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();

    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("run.plist");

    auto background = Sprite::createWithSpriteFrameName("background.png");
    background->setAnchorPoint(Vec2::ZERO);
    this->addChild(background, 0);

    sprite = Sprite::createWithSpriteFrameName("h1.png");
    sprite->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
    this->addChild(sprite);

    isPlaying = false;

    //toggle菜单
    auto goSprite = Sprite::createWithSpriteFrameName("go.png");
    auto stopSprite = Sprite::createWithSpriteFrameName("stop.png");
    auto goToggleMenuItem = MenuItemSprite::create(goSprite, goSprite);
    auto stopToggleMenuItem = MenuItemSprite::create(stopSprite, stopSprite);
    auto toggleMenuItem = MenuItemToggle::createWithCallback(CC_CALLBACK_1(HelloWorld::onAction, this), goToggleMenuItem, stopToggleMenuItem, NULL);
    toggleMenuItem->setPosition(Director::getInstance()->convertToGL(Vec2(930, 540)));
    auto mn = Menu::create(toggleMenuItem, NULL);
    mn->setPosition(Vec2::ZERO);
    this->addChild(mn);

    return true;
}

void HelloWorld::onAction(Ref * pSender)
{
    if (!isPlaying)
    {
        //动画开始
        Animation * animation = Animation::create();
        //循环将精灵帧加入到animation对象中
        for (int i = 1; i <= 4; i++)
        {
            __String * frameName = __String::createWithFormat("h%d.png", i);
            log("frameName = %s", frameName->getCString());
            SpriteFrame * spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(frameName->getCString());
            animation->addSpriteFrame(spriteFrame);

            animation->setDelayPerUnit(0.15f);//设置两个帧播放时间
            animation->setRestoreOriginalFrame(true);//动画执行后还原初状态
        }
        Animate * action = Animate::create(animation);
        sprite->runAction(RepeatForever::create(action));
        //动画结束

        isPlaying = true;
    }
    else
    {
        sprite->stopAllActions();
        isPlaying = false;
    }
}
本文档旨在帮助开发者搭建STM8单片机的开发环境,并创建基于标准库的工程项目。通过本文档,您将了解如何配置开发环境、下载标准库、创建工程以及进行基本的工程配置。 1. 开发环境搭建 1.1 软件准备 IAR Embedded Workbench for STM8: 这是一个集成开发环境,具有高度优化的C/C++编译器全面的C-SPY调试器。它为STM8系列微控制器提供全面支持。 STM8标准库: 可以从STM官网下载最新的标准库文件。 1.2 安装步骤 安装IAR: 从官网下载并安装IAR Embedded Workbench for STM8。安装过程简单,按照提示点击“下一步”即可完成。 注册IAR: 注册过程稍微繁琐,但为了免费使用,需要耐心完成。 下载STM8标准库: 在STM官网搜索并下载最新的标准库文件。 2. 创建标准库工程 2.1 工程目录结构 创建工作目录: 在自己的工作目录下创建一个工程目录,用于存放IAR生成的文件。 拷贝标准库文件: 将下载的标准库文件拷贝到工作目录中。 2.2 工程创建步骤 启动IAR: 打开IAR Embedded Workbench for STM8。 新建工程: 在IAR中创建一个新的工程,并将其保存在之前创建的工程目录下。 添加Group: 在工程中添加几个Group,分别用于存放库文件、自己的C文件其他模块的C文件。 导入C文件: 右键Group,导入所需的C文件。 2.3 工程配置 配置芯片型号: 在工程选项中配置自己的芯片型号。 添加头文件路径: 添加标准库的头文件路径到工程中。 定义芯片宏: 在工程中定义芯片相关的宏。 3. 常见问题与解决方案 3.1 编译错误 错误1: 保存工程时报错“ewp could not be written”。 解决方案: 尝试重新创建工程,不要在原路径下删除工程文件再创建。 错误
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值