首页 技术 正文
技术 2022年11月14日
0 收藏 507 点赞 3,074 浏览 2142 个字

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”);
   }
  });

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,943
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,469
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,282
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,097
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,729
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,766