您的位置:首页 > 移动开发 > Cocos引擎

cocos2dx学习之路--拼图游戏

2016-09-06 15:41 113 查看
作为一名初学者想通过写博客来记录自己学习的过程,第一个例程是一个简单的拼图游戏。
1.头文件CuttingPhotos.h

<span style="font-size:18px;">#pragma once
#include "cocos2d.h"
USING_NS_CC;
class CuttingPhotos:public Layer
{
public:
CuttingPhotos();
~CuttingPhotos();

//行数和列数
static CuttingPhotos*create(const char*photo, unsigned int rows = 3, unsigned int columns = 3);
protected:
unsigned int _row;//行数
unsigned int _column;//列数
unsigned int _sliceWidth;//格子宽度
unsigned int _sliceHeight;//格子高度

std::vector<std::vector<Sprite*>> _slices;//二维数组存储我们的切片;

virtual bool initWithPhoto(const char*photo, unsigned int rows , unsigned int columns );//初始化函数

bool initSlices(const char*photo);//切割图片

void initTouchEventListener();//初始化触摸事件

void move(int x, int y);//移动图片切片
public:
void restart();//重新开始游戏

};</span>
2.源文件CuttingPhotos.cpp
#include "CuttingPhotos.h"

CuttingPhotos::CuttingPhotos():_row(3),_column(3)//构造函数,初始化
{
}

CuttingPhotos::~CuttingPhotos()//析构函数
{

this->removeAllChildren();

}

CuttingPhotos* CuttingPhotos::create(const char*photo, unsigned int rows, unsigned int columns) {

CuttingPhotos*cp = new CuttingPhotos();
if (cp&&cp->initWithPhoto(photo,rows,columns))
{

cp->autorelease();
return cp;
}

CC_SAFE_DELETE(cp);
return nullptr;

}

bool  CuttingPhotos::initWithPhoto(const char*photo, unsigned int rows, unsigned int columns) {

bool result = false;
if (Layer::init())
{

_row = rows;
_column = columns;
if (initSlices(photo))                  //执行初始化函数
{
initTouchEventListener();
restart();
result = true;
}
else
{ result = false; }
}
else
{
result = false;
}

return result;
}

bool  CuttingPhotos::initSlices(const char*photo) {
/*

Cocos引擎主要有三种缓存类:

> 纹理缓存:TextureCache

> 精灵帧缓存:SpriteFrameCache

> 动画缓存:AnimationCache

缓存的目的就是:先将所需资源(如纹理图片)加载到内存中,之后再次使用该资源的时候,
就可以直接从内存中取出,而不需要重新加载。从而减少了CPU和GPU的内存占用。

*/
Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(photo);
if (texture==nullptr)
{
return false;
}
//初始化二维数组
for (int w=0;w<_row;++w)
{
std::vector<Sprite*>row;
for (int h=0;h<_column;++h)
{
row.push_back(nullptr);
}
_slice
4000
s.push_back(row);
}

//计算图片的宽与高

auto size = texture->getContentSize();
_sliceWidth = size.width / _row;
_sliceHeight = size.height / _column;

this->setContentSize(size);//设置Layer的大小,this是类中的隐藏指针,指向这个类所定义的对象本身。用来区分临时变量和类成员

//挖出最后一块,右下角
auto endSlice = Sprite::createWithTexture(texture,
Rect(_sliceWidth*(_row - 1), _sliceHeight*(_column - 1), _sliceWidth, _sliceHeight));
endSlice->setAnchorPoint(Vec2(0, 0));
endSlice->setPosition(_sliceWidth*(_row - 1), 0);
endSlice->setOpacity(100);
this->addChild(endSlice);

_slices[_row - 1][_column - 1] = nullptr;//表明是起点

//创建切片
for (int w=0;w<_row;++w)
{
for (int h=0;h<_column;++h)
{

if (w==_row-1&&h==_column-1)
{
break;//最后一片没有压进数组

}
Sprite*slices = Sprite::createWithTexture(texture,
Rect(_sliceWidth*w,_sliceHeight*h,_sliceWidth,_sliceHeight));
slices->setAnchorPoint(Vec2(0, 0));
slices->setPosition(w*_sliceWidth, size.height - (h + 1)*_sliceHeight);
this->addChild(slices);
_slices[w][h] = slices;
}
}

return true;

}
3.HelloWorldScene.cpp里的代码

auto visibleSize = Director::getInstance()->getVisibleSize();
auto *photo = CuttingPhotos::create("123.png",3,3);
auto size = photo->getContentSize();

/*

Layer默认锚点为(0,0),且要修改前必须加上
photo->ignoreAnchorPointForPosition(false);才可修改
photo->setAnchorPoint(Vec2(0.5, 0.5));

Sprite锚点默认(0.5,0.5),修改前无需加上

*/

photo->setPosition(visibleSize.width/2-size.width/2,visibleSize.height/2-size.height/2);
addChild(photo);
4.最后的效果图



出现的问题:频繁交叉点击图片和外界会出现数组越界的问题,暂未解决。

注:例程来自极客学院视频网站
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: