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. MYSQL启动报1067错误,系统日志中是“服务 mysql 意外停止” Mysql日志中则是:“Plugin \'FEDERATED\' is disabled”

    MYSQL启动报1067错误,系统日志中是"服务 mysql 意外停止" Mysql日志中则是:"Plugin \'FEDERATED\' is disabled&quo ...

  2. 一种swift编码风格指南(供参考,by linkedin)

    http://www.cocoachina.com/swift/20160701/16894.html

  3. 【转】Servlet与web.xml的配置

    Web.xml常用元素<web-app><display-name></display-name>定义了WEB应用的名字<description>< ...

  4. Naming Service 与 Zookeeper

      命名服务是指通过指定的名字来获取资源或者服务的地址,提供者的信息.利用Zookeeper很容易创建一个全局的路径,而这个路径就可以作为 一个名字,它可以指向集群中的集群,提供的服务的地址,远程对象 ...

  5. php--yii2.0的安装

    1.php.ini中去掉php_openssl.dll前面的“;” 2.注意phpstudy中php版本使用5.4n 3.环境OK后,使用自己的域名访问下yii2.0中advanced中的requir ...

  6. 遍历ModelState中存储的错误信息

    在服务器端验证中,有时我们添加了一个ModelError,然后还需要将该信息以JS的形式返回到客户端.如: [HttpPost] public ActionResult Index(LogOnMode ...

  7. jQuery实现隔行变色

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  8. 【转】ViewPager实现一个页面多个Item的显示

    转自:http://billyyuan.iteye.com/blog/1941538 ViewPager实现一个页面多个Item的显示 博客分类: android   代码在: https://cod ...

  9. 【转】Android各种Adapter的用法

    转自:http://my.oschina.net/u/658933/blog/372151 Android各种Adapter的用法 发表于5个月前(2015-01-27 10:56)   阅读(143 ...

  10. php回调函数

    1.array_filter($arr, "odd") <?php $arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9); function odd ...