MyGame.h中声明动画函数:

class MyGame : public cocos2d::Layer
{
public:
 static Scene* createScene();
    void Updatetime(float t);
   virtual bool init();
   void logic(float dt);

........
   cocos2d::Animate* createAnimate1();
   CREATE_FUNC(MyGame);

}

MyGame.cpp:

#include "MyGame.h"

#include "cocostudio/CocoStudio.h"

#include "ui/CocosGUI.h"

#include "cocos2d.h"

#include<iostream>

using namespace std;

USING_NS_CC;

using namespace cocos2d;

using namespace cocostudio::timeline;

cocos2d::Animate* MyGame::createAnimate1() {

auto animation = Animation::create();

for (int i = 1; i <= 4; i++)  {   //四张图片(用于组成动画)

animation->addSpriteFrameWithFile(StringUtils::format("run%d.png", i));

}

animation->setDelayPerUnit(3.0f / 15.0f);

//回到原始状态

animation->setRestoreOriginalFrame(true);

auto animate = Animate::create(animation);

return animate;

}

Scene* MyGame::createScene() {

// 'scene' is an autorelease object

auto scene = Scene::create();

// 'layer' is an autorelease object

auto layer = MyGame::create();

// add layer as a child to scene

scene->addChild(layer);

// return the scene  return scene; }

bool MyGame::init() {

// 1. super init first

if (!Layer::init())

{

return false;

}

ball = Sprite::create("run1.png");
 ball->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
 this->addChild(ball,2);

//点击btn0,则运行击打棒球动画

btn0->addClickEventListener([&](Ref* pSender) {
  ball->runAction(createAnimate1());
 });

return true;

}

注意:若按钮事件中包含切换场景事件,则需要在按钮事件中添加schedule函数,否则切换场景后,动画还来不及生成,再次点击按钮出发按钮事件,则动画不会执行,即无反应

具体代码如下:

//为按钮添加触摸事件
 btn0->addTouchEventListener([&, btn0, btn1, btn2](Ref* pSender, Widget::TouchEventType type) {
  switch (type)
  {
  case Widget::TouchEventType::BEGAN:
   btn0->setScale(1.02);
   break;
  case Widget::TouchEventType::ENDED:
   btn0->setScale(1);
   btn1->setTouchEnabled(false);
   btn2->setTouchEnabled(false);
   .......

if (r[0] == 3 || delegate1->restartnum == 0)
   {
   ..........

//切换场景时不能单单写一句代码 Director::getInstance()->replaceScene(GameOver::createScene());

//而应该写个schedule函数用于缓冲时间

schedule([&](float ft) {
     unschedule("GameOver");
     Director::getInstance()->replaceScene(GameOver::createScene());
    },1,0,0,"GameOver");
   }
   else
    schedule([&](float ft) {
    unschedule("newGame");
    Director::getInstance()->replaceScene(MyGame::createScene());
   },1, 0, 0, "newGame");
   }
  });

cocos2d-x游戏开发之动画的更多相关文章

  1. iOS cocos2d 2游戏开发实战(第3版)书评

    2013是游戏爆发的一年,手游用户也是飞速暴增.虽然自己不做游戏,但也是时刻了解手机应用开发的新动向.看到CSDN的"写书评得技术图书赢下载分"活动,就申请了一本<iOS c ...

  2. Android游戏开发研究帧动画实现

     1.动画的原则框架        帧的动画帧的动画顾名思义,画就是帧动画. 帧动画和我们小时候看的动画片的原理是一样的,在同样区域高速切换图片给人们呈现一种视觉的假象感觉像是在播放动画,事实上只 ...

  3. (转载)如何学好iphone游戏开发

    转自:http://www.cnblogs.com/zilongshanren/archive/2011/09/19/2181558.html 自从发布<如何学习iphone游戏开发>到 ...

  4. [libGDX游戏开发教程]使用libGDX进行游戏开发(1)-游戏设计

    声明:<使用Libgdx进行游戏开发>是一个系列,文章的原文是<Learning Libgdx Game Development>,大家请周知.后续的文章连接在这里 使用Lib ...

  5. 【Cocos2D研究院之游戏开发】

    http://www.xuanyusong.com/archives/category/ios/cocos2d_game 分类目录归档:[Cocos2D研究院之游戏开发]   201211-19 Co ...

  6. 【读书笔记《Android游戏编程之从零开始》】16.游戏开发基础(动画)

    1. Animation动画   在Android 中,系统提供了动画类 Animation ,其中又分为四种动画效果: ● AlphaAnimation:透明度渐变动画 ● ScaleAnimati ...

  7. 《MFC游戏开发》笔记六 图像双缓冲技术:实现一个流畅的动画

    本系列文章由七十一雾央编写,转载请注明出处.  http://blog.csdn.net/u011371356/article/details/9334121 作者:七十一雾央 新浪微博:http:/ ...

  8. 《MFC游戏开发》笔记五 定时器和简单动画

    本系列文章由七十一雾央编写,转载请注明出处. http://blog.csdn.net/u011371356/article/details/9332377 作者:七十一雾央 新浪微博:http:// ...

  9. cocos2d 游戏开发实战

    文章转自:http://uliweb.clkg.org/tutorial/read/40 6   cocos2d 游戏开发实战 6.1   创建cocos2d项目 6.2   cocos2d v3 & ...

随机推荐

  1. php判断爬虫

    function checkrobot($useragent = ''){ static $kw_spiders = 'Bot|Crawl|Spider|slurp|sohu-search|lycos ...

  2. sphinx续5-主索引增量索引和实时索引

    原文件地址:http://blog.itpub.net/29806344/viewspace-1400942/ 在数据库数据非常庞大的时候,而且实时有新的数据插入,如果我们不更新索引,新的数据就sea ...

  3. 设计模式:职责链模式(Chain Of Responsibility)

    定  义:使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递请求,直到有一个对象处理它为止. 结构图: 处理请求类: //抽象处理类 abs ...

  4. [LeetCode] Jump Game II(贪婪算法)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  5. LightOj 1138 - Trailing Zeroes (III) 阶乘末尾0的个数 & 二分

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1138 题意:给你一个数n,然后找个一个最小的数x,使得x!的末尾有n个0:如果没有输出 ...

  6. 我的工具箱之Putty

    这是类似于SecureCrt的终端仿真软件,个人感觉比SecureCrt差一点. 下载地址是:http://pan.baidu.com/s/1sko0GrF SecureCrt网址在我的工具箱之Sec ...

  7. ifconfig命令(转)

    许多windows非常熟悉ipconfig命令行工具,它被用来获取网络接口配置信息并对此进行修改.Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config).通常需 ...

  8. 由一个Servlet 看java入门常犯的几个错误

    安装完java环境后,cmd-javac 报错           ------------->环境变量配错了,最后全配成系统变量,ok了 能浪费一天的时间 写一个最简单的Servlet ,to ...

  9. vsftp 定时任务同步

    yum install db4-utils.x86_64 -y yum install ftp vsftp lftp 在客户端 和服务端都安装vsftp服务 并配置虚拟账号 上传包内文件 [root@ ...

  10. nssstring 转换大小写

    //7.将字符串中的字母转换成大写 NSString * str10 = @"china"; NSString * tmpStr1 = [str10 uppercaseString ...