当前位置: 首页 > news >正文

【cocos2D-X】Plist使用 实现 移动精灵多图片动画

  • 要点 : 制作图集要和背景图一起制作 plist文件 要放一个文件夹
    在这里插入图片描述

  • 原图片也是放在一个文件夹

  • 在这里插入图片描述
    在这里插入图片描述

  • 这个代码 数字 与文件名对应 , 如果出现报错 多数就是数值 越界 考虑图片数减少
    在这里插入图片描述

AppDelegate.h

#ifndef  _APP_DELEGATE_H_
#define  _APP_DELEGATE_H_#include "cocos2d.h"/**
@brief    The cocos2d Application.Private inheritance here hides part of interface from Director.
*/
class  AppDelegate : private cocos2d::Application
{
public:AppDelegate();virtual ~AppDelegate();virtual void initGLContextAttrs();/**@brief    Implement Director and Scene init code here.@return true    Initialize success, app continue.@return false   Initialize failed, app terminate.*/virtual bool applicationDidFinishLaunching();/**@brief  Called when the application moves to the background@param  the pointer of the application*/virtual void applicationDidEnterBackground();/**@brief  Called when the application reenters the foreground@param  the pointer of the application*/virtual void applicationWillEnterForeground();
};#endif // _APP_DELEGATE_H_

helloworld.h

#include "cocos2d.h"
#include <vector>
using namespace std;
class MyHelloWorld: public cocos2d::Scene {
public:cocos2d::Vector<cocos2d::SpriteFrame*> getAnimation(const char* format, int count); //addstatic cocos2d::Scene* createScene();virtual bool init();void menuCloseCallback(cocos2d::Ref* pSender);CREATE_FUNC(MyHelloWorld);
};//cpp
/****************************************************************************Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.http://www.cocos2d-x.orgPermission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included inall copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS INTHE SOFTWARE.****************************************************************************/#include "MyHelloWorld.h"
#include <vector>
USING_NS_CC;Scene* MyHelloWorld::createScene()
{return MyHelloWorld::create();
}// Print useful error message instead of segfaulting when files are not there.
static void problemLoading(const char* filename)
{printf("Error while loading: %s\n", filename);printf("Depending on how you compiled you might have to add 'Resources/' in front of filenames in HelloWorldScene.cpp\n");
}Vector<SpriteFrame*> MyHelloWorld::getAnimation(const char* format, int count)
{auto spritecache = SpriteFrameCache::getInstance();Vector<SpriteFrame*> animFrames;char str[100];for (int i = 0; i <= count; i++){sprintf(str, format, i);animFrames.pushBack(spritecache->getSpriteFrameByName(str));}return animFrames;
}// on "init" you need to initialize your instance
bool MyHelloWorld::init()
{//// 1. super init firstif (!Scene::init()){return false;}auto visibleSize = Director::getInstance()->getVisibleSize();Vec2 origin = Director::getInstance()->getVisibleOrigin();SpriteFrameCache::getInstance()->addSpriteFramesWithFile("tong.plist"); // 缓存粒子 auto bg = Sprite::create("bg1.jpg");bg->setScale(0.8);bg->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));this->addChild(bg);auto frames = getAnimation("BucketheadZombie/BucketheadZombie_%d.png", 14); // 调用加载图片auto sprite = Sprite::createWithSpriteFrame(frames.front());bg->addChild(sprite);sprite->setPosition(300, 300);auto animation1 = Animation::createWithSpriteFrames(frames, 1.0f / 13); // 缓存时间 约小则加载越快  sprite->runAction(RepeatForever::create(Animate::create(animation1)));return true;
}void MyHelloWorld::menuCloseCallback(Ref* pSender)
{//Close the cocos2d-x game scene and quit the applicationDirector::getInstance()->end();/*To navigate back to native iOS screen(if present) without quitting the application  ,do not use Director::getInstance()->end() as given above,instead trigger a custom event created in RootViewController.mm as below*///EventCustom customEndEvent("game_scene_close_event");//_eventDispatcher->dispatchEvent(&customEndEvent);}

}


http://www.taodudu.cc/news/show-6105722.html

相关文章:

  • 移动设备上“精灵图”的制作适配
  • cocos2dx 精灵的移动(2)
  • Cocos2d-x 2.0 百例精讲:如何让一个精灵跟随触点移动
  • 在屏幕的任意位置拖拽,控制精灵移动
  • 精灵的移动效果,旋转效果
  • 【JavaScript】实现移动小精灵
  • 让视角随着精灵移动
  • 移动设备上“精灵图”的制作
  • 移动端精灵图的使用
  • Cocos2d-x随记(2)-精灵移动
  • buntu22.04安装WPS中文版(一百一十八)
  • 数数
  • 视网膜数据集(2)Messidor
  • realsense 相机的部分信息获取
  • Linux线程数和系统线程数查看
  • 数的表示与运算
  • 如何判断一个数是否是NaN
  • SQLite获取查询结果数
  • 用while循环写四叶玫瑰数(自幂数)
  • 【算法讲20:Dsu on Tree】树上数颜色 | Lomsat gelral
  • Deep_Learn关于数组和数的操作
  • codeforces1670F Jee, You See?(DP/位运算/前缀和/组合数)
  • SEEM~
  • SEE
  • 对random.seed()的理解
  • 对random.seed()函数的理解
  • delphi 多线程3
  • R统计绘图-corrplot热图绘制细节调整2(更改变量可视化顺序、非相关性热图绘制、添加矩形框等)
  • 安卓串口——谷歌android_serialport_api studio使用以及相关问题
  • 安卓是属于全人类的还是谷歌的私有产品?