最近在根据网上的各种教程模仿的做一些以前比较出名的小游戏,刚刚完成了微信的飞机大战,想做一下flappy bird ,在网上也找到了合适的教程,教程进去的第一章就是讲资源,flappy bird 的资源也是做在一张大图上但是他跟往常的不一样,图片内的数据都记录在一个对应的txt文件内,这一点让我非常不习惯,我一般喜欢用plist来加载,这样比较方便,也比较适合cocos2d-x,所以我琢磨这把这些图片一张一张提取出来,在做成plist,但是不能用photoshop去截取啊,这要到猴年马月,所以我就用cocos2d-x自带的截屏功能,结合它自己的图片数据,让程序自己截取,这样省了不少时间。
首先来看看flappy bird 自带的资源格式:
这是它的图片
下面是记录图片数据的txt文本框
bg_day 288 512 0.0 0.0 0.28125 0.5
bg_night 288 512 0.28515625 0.0 0.28125 0.5
bird0_0 48 48 0.0 0.9472656 0.046875 0.046875
bird0_1 48 48 0.0546875 0.9472656 0.046875 0.046875
bird0_2 48 48 0.109375 0.9472656 0.046875 0.046875
bird1_0 48 48 0.1640625 0.9472656 0.046875 0.046875
bird1_1 48 48 0.21875 0.6308594 0.046875 0.046875
bird1_2 48 48 0.21875 0.6816406 0.046875 0.046875
bird2_0 48 48 0.21875 0.7324219 0.046875 0.046875
bird2_1 48 48 0.21875 0.7832031 0.046875 0.046875
bird2_2 48 48 0.21875 0.8339844 0.046875 0.046875
black 32 32 0.5703125 0.40234375 0.03125 0.03125
blink_00 10 10 0.26953125 0.6660156 0.009765625 0.009765625
blink_01 10 10 0.26953125 0.7167969 0.009765625 0.009765625
blink_02 10 10 0.26953125 0.7675781 0.009765625 0.009765625
brand_copyright 126 14 0.86328125 0.17773438 0.123046875 0.013671875
button_menu 80 28 0.90234375 0.05078125 0.078125 0.02734375
button_ok 80 28 0.90234375 0.08203125 0.078125 0.02734375
button_pause 26 28 0.23632812 0.59765625 0.025390625 0.02734375
button_play 116 70 0.6855469 0.22851562 0.11328125 0.068359375
button_rate 74 48 0.90234375 0.0 0.072265625 0.046875
button_resume 26 28 0.65234375 0.27734375 0.025390625 0.02734375
button_score 116 70 0.8027344 0.22851562 0.11328125 0.068359375
button_share 80 28 0.5703125 0.27734375 0.078125 0.02734375
font_048 24 44 0.96875 0.11328125 0.0234375 0.04296875
font_049 16 44 0.265625 0.8847656 0.015625 0.04296875
font_050 24 44 0.5703125 0.30859375 0.0234375 0.04296875
font_051 24 44 0.59765625 0.30859375 0.0234375 0.04296875
font_052 24 44 0.625 0.30859375 0.0234375 0.04296875
font_053 24 44 0.65234375 0.30859375 0.0234375 0.04296875
font_054 24 44 0.5703125 0.35546875 0.0234375 0.04296875
font_055 24 44 0.59765625 0.35546875 0.0234375 0.04296875
font_056 24 44 0.625 0.35546875 0.0234375 0.04296875
font_057 24 44 0.65234375 0.35546875 0.0234375 0.04296875
land 336 112 0.5703125 0.0 0.328125 0.109375
medals_0 44 44 0.23632812 0.50390625 0.04296875 0.04296875
medals_1 44 44 0.23632812 0.55078125 0.04296875 0.04296875
medals_2 44 44 0.21875 0.8847656 0.04296875 0.04296875
medals_3 44 44 0.21875 0.9316406 0.04296875 0.04296875
new 32 14 0.21875 0.9785156 0.03125 0.013671875
number_context_00 12 14 0.26953125 0.6308594 0.01171875 0.013671875
number_context_01 12 14 0.26953125 0.6484375 0.01171875 0.013671875
number_context_02 12 14 0.26953125 0.6816406 0.01171875 0.013671875
number_context_03 12 14 0.26953125 0.69921875 0.01171875 0.013671875
number_context_04 12 14 0.26953125 0.7324219 0.01171875 0.013671875
number_context_05 12 14 0.26953125 0.75 0.01171875 0.013671875
number_context_06 12 14 0.26953125 0.7832031 0.01171875 0.013671875
number_context_07 12 14 0.26953125 0.80078125 0.01171875 0.013671875
number_context_08 12 14 0.26953125 0.8339844 0.01171875 0.013671875
number_context_09 12 14 0.26953125 0.8515625 0.01171875 0.013671875
number_context_10 12 14 0.96875 0.16015625 0.01171875 0.013671875
number_score_00 16 20 0.265625 0.59765625 0.015625 0.01953125
number_score_01 16 20 0.265625 0.9316406 0.015625 0.01953125
number_score_02 16 20 0.265625 0.9550781 0.015625 0.01953125
number_score_03 16 20 0.25390625 0.9785156 0.015625 0.01953125
number_score_04 16 20 0.9785156 0.0 0.015625 0.01953125
number_score_05 16 20 0.9785156 0.0234375 0.015625 0.01953125
number_score_06 16 20 0.984375 0.05078125 0.015625 0.01953125
number_score_07 16 20 0.984375 0.08203125 0.015625 0.01953125
number_score_08 16 20 0.5703125 0.47265625 0.015625 0.01953125
number_score_09 16 20 0.60546875 0.40234375 0.015625 0.01953125
pipe2_down 52 320 0.0 0.6308594 0.05078125 0.3125
pipe2_up 52 320 0.0546875 0.6308594 0.05078125 0.3125
pipe_down 52 320 0.109375 0.6308594 0.05078125 0.3125
pipe_up 52 320 0.1640625 0.6308594 0.05078125 0.3125
score_panel 238 126 0.0 0.50390625 0.23242188 0.123046875
text_game_over 204 54 0.765625 0.11328125 0.19921875 0.052734375
text_ready 196 62 0.5703125 0.11328125 0.19140625 0.060546875
title 178 48 0.6855469 0.17773438 0.17382812 0.046875
tutorial 114 98 0.5703125 0.17773438 0.111328125 0.095703125
white 32 32 0.5703125 0.4375 0.03125 0.03125
上面的数据是这样子的(以第一张图片为例子)
图片名字 图片长度 图片宽度 起始坐标 终点坐标
bg_day 288 512 0.0 0.0 0.28125 0.5
坐标都是以百分比的形式(真正的坐标要乘以1024,因为整张资源图片大小为1024)
貌似cocos2d-x的截屏功能不能指定截取屏幕中的一部分,只能规定大小,截取只能在左下角开始,所以我每次截取,都要把要截的图片放在左下角,所以要重新设定资源图片的位置。
下面上代码:
#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();
//加载那张资源图片作为背景
bg=Sprite::create("atlas.png");
bg->setAnchorPoint(Point::ANCHOR_BOTTOM_LEFT);
bg->setPosition(Point::ZERO);
//加载图片的数据
data=FileUtils::getInstance()->getStringFromFile("atlas.txt");
//找到第一个回车字符,记录它的位置
pos=data.find_first_of("\n");
//提取第一行数据
line=data.substr(0,pos);
//把数据定位到第二行
data=data.substr(pos+1);
//初始化图片数目
num=0;
//启动定时器,每一秒提取一张图片(执行一次函数)
this->schedule(schedule_selector(HelloWorld::back1),1.0f,kRepeatForever,0.0f);
this->addChild(bg);
return true;
}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
}
void HelloWorld::back1(float)
{
//记录第几张图片
num++;
//一共71张图片,超出就结束
if (num>71)
{
this->unschedule(schedule_selector(HelloWorld::back1));
log("end");
return;
}
//一张图片的各种数据,名字,长,高,坐标
char name[255];
int width;
int height;
Point start;
Point end1;
//把一行中的数据付给我们定义的变量
sscanf(line.c_str(),"%s %d %d %f %f %f %f",name,&width,&height,&start.x,&start.y,&end1.x,&end1.y);
//图片的名字
std::string name_1=std::string(name)+".png";
log("num=%d ,name=%s",num,name_1.c_str());
//坐标的转换
start.x*=1024;
start.y=1024-start.y*1024-height;
end1.x*=1024;
end1.y*=1024;
//定义截屏对象,截屏的长宽,这里就用我们对应要截取图片的长宽
auto texture=RenderTexture::create(width,height);
//截取开始
texture->begin();
//重新设置资源图片的位置(因为截屏功能貌似只能从屏幕左下角开始,不管我怎么设置texture的坐标,它都是从左下角开始,我是小白,也不懂怎么回事)
bg->setPosition(Point::ZERO-start);
//遍历场景节点对象,填充纹理到texture
Director::getInstance()->getRunningScene()->visit();
//截取结束
texture->end();
//保存名字,格式(默认保存在win32,debug文件夹里)
texture->saveToFile(name_1.c_str(),Image::Format::PNG);
//循环
pos = data.find_first_of("\n");
line = data.substr(0, pos);
data = data.substr(pos + 1);
}
就是简单的一个cocos2d-x例子,就改了它的helloworld的内容
下面是头文件
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::Layer
{
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);
void back1(float);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
private:
cocos2d::Sprite* bg;//资源图片当背景
std::string line;//一张图片对应一行数据
unsigned pos;//回车的位置
std::string data;//所有的图片数据
int num;//图片序号
};
#endif // __HELLOWORLD_SCENE_H__
本人也是小白,所以有些代码写的不是很好,请谅解.....
截取后的图片
下面是flappy bird 的教程链接,有些代码还是参照他的。