请大神们来指导下我,到底哪里有问题(真心求助,在线等)

代码如下:
启动项的H文件:
#ifndef NEW_SCENE_H
#define NEW_SCENE_H

#include “cocos2d.h”
using namespace cocos2d;

class New : 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();
/*Sprite* ball;*/
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);

//Sprite* pingtu;

// Sprite* background;

/* void onAcceleration(cocos2d::Acceleration* acc, cocos2d::Event* event); */
// implement the “static create()” method manually
CREATE_FUNC(New);

/*void upbtnCallback();
void downbtnCallback();
void leftbtnCallback();
void rightbtnCallback();  */  

};

启动项的CPP
#include “NewScene.h”
#include “CuttingPhotos.h”

//#define FIX_POS(_pos,_min,_max)
// if(_pos<_min)
// _pos=_min;
// else if(_pos>_max)
// _pos=_max;
//
USING_NS_CC;

Scene* New::createScene()
{
// ‘scene’ is an autorelease object
auto scene = Scene::create();

// 'layer' is an autorelease object
auto layer = New::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 New::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}

Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();

CuttingPhotos* photo = CuttingPhotos::create("pingtu.jpg",3,3);
//auto photo = CuttingPhotos::create("pingtu.jpg",3,3);
//photo->setAnchorPoint(Vec2(0,0));
auto photoSize = photo->getContentSize();
photo->setPosition(origin.x+visibleSize.width/2-photoSize.width/2,origin.y+visibleSize.height/2-photoSize.height/2);
addChild(photo);

//Sprite* pingtu = Sprite::create("pingtu.jpg");
//pingtu->setPosition(visibleSize.width-100,visibleSize.height-100);
//pingtu->setScale(0.4f,0.4f);
//addChild(pingtu);

//auto text = TextFieldTTF::textFieldWithPlaceHolder("The original image","Marker Felt",20);
//text->setPosition(visibleSize.width-100,visibleSize.height-200);
//this->addChild(text);

//auto sprite = Sprite::create("bg.png");
//sprite->setPosition(origin.x,origin.y);
/*sprite->setAnchorPoint(Vec2(origin.x*0.5,origin.y*0.5));
this->addChild(sprite);
auto move = MoveBy::create(3.0,Vec2(visibleSize.width/2,0));
auto action = Sequence::create(move,FlipX::create(true),move->reverse(),CallFunc::create(](){MessageBox("move complete","action");}) ,NULL);
sprite->runAction(action);*/ 

/*auto ng = NodeGrid::create();
this->addChild(ng);
ng->addChild(sprite);
auto jump = JumpTiles3D::create(3.0,Size(20,20),2,60);
ng->runAction(jump);*/

/*auto label = Label::createWithCharMap("digit.png",20,20,'0');
label->setString("12358");
auto letterSprite = label->getLetter(2);
auto scale = RotateBy::create(3.0,360);
letterSprite->runAction(scale);
label->setPosition(visibleSize/2);
this->addChild(label);*/

/*auto player = Sprite::create("player.png");
player->setPosition(visibleSize/2);
this->addChild(player);
auto menuitem1 = MenuItemImage::create("element1.png","element2.png",(Ref* r){
    auto rotate = RotateBy::create(1.0,360);
    player->runAction(rotate);
    auto scale = ScaleBy::create(0.8,0.8,0.8,0.8);
    player->runAction(scale);
});
auto menuitem2 = MenuItemImage::create("element.png","element2.png",](Ref* r){
    Director::getInstance()->end();
});
auto menu = Menu::create(menuitem1,menuitem2,NULL);

menuitem1->setPosition(-300,10);
menuitem2->setPosition(300,10);
this->addChild(menu);*/

//auto text = TextFieldTTF::textFieldWithPlaceHolder("input here","Marker Felt",33);
//text->setPosition(200,200);
//this->addChild(text);
//auto player = Sprite::create("player.png");
//player->setPosition(Vec2(100,100));
//this->addChild(player,1);
//auto listener =EventListenerTouchOneByOne::create();
//listener->onTouchBegan = ](Touch *t,Event *e)
//{
//    auto target = static_cast<Sprite*>(e->getCurrentTarget());
//    if(target->getBoundingBox().containsPoint(t->getLocation()))
//    {
//        //target->attachWithIME();  

//    }
//    else
//    {
//        //target->detachWithIME();
//    }
//    return true;
//};
//listener->onTouchMoved = ](Touch *t,Event *e)
//{
//    auto target = static_cast<TextFieldTTF*>(e->getCurrentTarget());
//    if(target->getBoundingBox().containsPoint(t->getLocation()))
//    {
//        target->setPosition(target->getPosition()+t->getDelta());
//    }
//    log("moved");
//};
//listener->onTouchEnded = ](Touch *t,Event *e)
//{
//    log("ended");
//};
//auto dispatch =  Director::getInstance()->getEventDispatcher();
//dispatch->addEventListenerWithSceneGraphPriority(listener,player);

//要记得修改
//auto player = Sprite::create("player.png");
//player->setPosition(visibleSize/2);
//this->addChild(player,1);

//auto UpButton = MenuItemImage::create("up.png","up.png",CC_CALLBACK_0(New::upbtnCallback,this));
//auto menu = Menu::create(UpButton,NULL);
//menu->setPosition(visibleSize.width-200,visibleSize.height-502);
//this->addChild(menu);

//auto DownButton = MenuItemImage::create("down.png","down.png",CC_CALLBACK_0(New::downbtnCallback,this));
//auto menu2 = Menu::create(DownButton,NULL);
//menu2->setPosition(visibleSize.width-200,visibleSize.height-579);
//this->addChild(menu2);
//
//auto LeftButton = MenuItemImage::create("left.png","left.png",CC_CALLBACK_0(New::leftbtnCallback,this));
//auto menu3 = Menu::create(LeftButton,NULL);
//menu3->setPosition(visibleSize.width-277,visibleSize.height-579);
//this->addChild(menu3);

//auto RightButton = MenuItemImage::create("right.png","right.png",CC_CALLBACK_0(New::rightbtnCallback,this));
//auto menu4 = Menu::create(RightButton,NULL);
//menu4->setPosition(visibleSize.width-123,visibleSize.height-579);
//this->addChild(menu4);


//重力感应
//auto ball = Sprite::create("ball.png");
//ball->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2));
//ball->setTag(12);
//ball->addChild(ball);
//Device::setAccelerometerEnabled(true);
//auto listener = EventListenerAcceleration::create(CC_CALLBACK_2(New::didAccelerate,this));
//_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this);

//     ball = Sprite::create("ball.png");
//     ball->setPosition(visibleSize/2);
//     //ball->setScale(2f,2f);
//     addChild(ball);
//  

//Device::setAccelerometerEnabled(true);
//auto listener = EventListenerAcceleration::create(CC_CALLBACK_2(New::onAcceleration, this));
//_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);    





return true;

}

//void New::onAcceleration(cocos2d::Acceleration* acc, cocos2d::Event* event){
//
// if(ball==nullptr)
// return;
//
// Size visibleSize = Director::getInstance()->getVisibleSize();
//
// auto ballsize = ball->getContentSize();
// auto ptNow = ball->getPosition();
//
// ptNow.x += acc->x * 9.81f;
// ptNow.y += acc->y * 9.81f;
//
// FIX_POS(ptNow.x,ballsize.width/2,visibleSize.width);
// FIX_POS(ptNow.y,ballsize.height/2,visibleSize.height);
//
// ball->setPosition(ptNow);
//}

//void New::didAccelerate(Acceleration* acc, Event* event)
//{
// auto ball = this->getChildByTag(12);
// auto pos = ball->getPosition();
// pos.x += acc->x;
// pos.y += acc->y;
// ball->setPosition(pos);
//}

//void New::upbtnCallback()
// {
// Size visibleSize = Director::getInstance()->getVisibleSize();
// auto player = Sprite::create(“player.png”);
// player->setPosition(visibleSize/2);
// auto action = JumpBy::create(3,Vec2(visibleSize.width/2-500,visibleSize.height/2-80),30,5);
// player->runAction(action);
// this->addChild(player);
// }
//
//void New::downbtnCallback()
// {
// Size visibleSize = Director::getInstance()->getVisibleSize();
// auto player = Sprite::create(“player.png”);
// player->setPosition(visibleSize/2);
// auto action = JumpBy::create(3,Vec2(visibleSize.width/2-500,visibleSize.height/2-600),30,5);
// player->runAction(action);
// this->addChild(player);
// }
//
//void New::leftbtnCallback()
// {
// Size visibleSize = Director::getInstance()->getVisibleSize();
// auto player = Sprite::create(“player.png”);
// player->setPosition(visibleSize/2);
// auto action = JumpBy::create(3,Vec2(visibleSize.width/2-800,visibleSize.height/2-300),30,5);
// player->runAction(action);
// this->addChild(player);
// }
//
//void New::rightbtnCallback()
// {
// Size visibleSize = Director::getInstance()->getVisibleSize();
// auto player = Sprite::create(“player.png”);
// player->setPosition(visibleSize/2);
// auto action = JumpBy::create(3,Vec2(visibleSize.width/2-80,visibleSize.height/2-300),30,5);
// player->runAction(action);
// this->addChild(player);
// }

void New::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox(“You pressed the close button. Windows Store Apps do not implement a close button.”,“Alert”);
return;
#endif

Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}

新写的类的H文件:
#ifndef CuttingPhotos__CuttingPhotos
#define CuttingPhotos__CuttingPhotos

#include <stdio.h>
#include
#include “cocos2d.h”

USING_NS_CC;

class CuttingPhotos:public Layer
{
CuttingPhotos();
~CuttingPhotos();
public:

static CuttingPhotos* create(const char* photo,unsigned int rows=3,unsigned int columns=3);

protected:
unsigned int _rows;//行数
unsigned int _columns;//列数
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 initSlice(const char* photo);//切割图片

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

void move(int x,int y);

public:
void restart();//重新开始游戏
};
#endif

新写的类的CPP:
#include “CuttingPhotos.h”

CuttingPhotos::CuttingPhotos():
_rows(3),
_columns(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;//初始化是否成功

if(Layer::init())
{
    _rows=rows;
    _columns=columns;
    if(initSlice(photo)){
        initTouchEventListener();
        restart();
        result=true;
    }else
    {
        result=false;
    }
}else
{
    result=false;
}
return result;

}
bool CuttingPhotos::initSlice(const char photo)
{
Texture2D texture = Director::getInstance()->getTextureCache()->addImage(photo);
if(texture=nullptr){
return false;
}
//初始化存储行列的列表
for(int w=0; w < _rows; ++w)
{
std::vector<Sprite
> row;
for(int h=0; h < _columns;++h)
{
row.push_back(nullptr);
}
_slices.push_back(row);
}
//计算图片每列的宽度
auto photoSize=texture->getContentSize();
_sliceWidth=photoSize.width/_rows;
_sliceHeight=photoSize.height/_columns;
this->setContentSize(photoSize);//设置Layer的大小
//挖出最后一块切片
auto endSlice=Sprite::createWithTexture(texture,Rect(_sliceWidth
(_rows-1),_sliceHeight*(_columns-1),_sliceWidth,_sliceHeight));
endSlice->setAnchorPoint(Vec2(0,0));
endSlice->setPosition((_rows-1)_sliceWidth,0);
endSlice->setOpacity(100);//降低透明度
this->addChild(endSlice);
_slices=nullptr;//设为空,表示这格可以移入
//创建切片
for(int w=0; w < _rows;++w){
for(int h=0;h < _columns;++h){
if(w==_rows-1 && h==_columns-1){
//最后一块不需要
break;
}
Sprite slice=Sprite::createWithTexture(texture,Rect(_sliceWidthw,_sliceHeight
h,_sliceWidth,_sliceHeight));
slice->setAnchorPoint(Vec2(0,0));
slice->setPosition(w*_sliceWidth,photoSize.height-(h+1)*_sliceHeight);
this->addChild(slice);
_slices=slice;
}
}
return true;
}
void CuttingPhotos::initTouchEventListener()
{
auto listner=EventListenerTouchOneByOne::create();
listner->onTouchBegan=&](Touch touch,Event event){
Point locationInNode=this->convertToNodeSpace(touch->getLocation());
Size s=this->getContentSize();
Rect rect=Rect(0,0,s.width,s.height);
if(rect.containsPoint(locationInNode)){
return true;
}else
{
return false;
}
};
listner->onTouchEnded=&](Touch touch,Event event){
Point locationInNode=this->convertToNodeSpace(touch->getLocation());
int x=static_cast(floor(locationInNode.x/this->_sliceWidth));
int y=static_cast(floor(locationInNode.x/this->_sliceHeight));
this->move(x,y);
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listner,this);
}
void CuttingPhotos::move(int x,int y)
{
y=_columns-y-1;
auto slice=_slices;
if(slice==nullptr){
return;
}
//判断周围是否有空余的格子
bool isFind=false;
Point targetPos;
if(y<_columns-1&&_slices==nullptr){
targetPos.x=x;
targetPos.y=y+1;
isFind=true;
}else if(y>0&&_slices==nullptr)
{
targetPos.x=x;
targetPos.y=y-1;
isFind=true;
}else if(x<_rows-1&&_slices==nullptr)
{
targetPos.x=x+1;
targetPos.y=y;
isFind=true;
}else if(x>0&&_slices==nullptr)
{
targetPos.x=x-1;
targetPos.y=y;
isFind=true;
}
if(isFind==false)
{
return;
}
slice->runAction(MoveTo::create(0.25f,Vec2(targetPos.x
_sliceWidth,_sliceHeight
_columns-(targetPos.y+1)_sliceHeight)));
_slices=slice;
_slices=nullptr;
}
void CuttingPhotos::restart()
{
//取出所有的图块
cocos2d::Vector<Sprite
> list;
for(int w=0; w<_rows;++w)
{
for(int h=0;h<_columns;++h)
{
auto slice=_slices;
if(slice!=nullptr){
list.pushBack(slice);
}
}
}
//随机布置到每一个位置
for(int w=0; w<_rows;++w){
for(int h=0;h<_columns;++h)
{
if(w==_rows-1&&h==_columns-1)
{
//排除最后一块
_slices=nullptr;
break;
}
auto length=list.size();
auto value=static_cast(CCRANDOM_0_1()length);
auto slice=list.at(value);
_slices=slice;
slice->setPosition(w
_sliceWidth,_sliceHeight
_columns-(h+1)
_sliceHeight);
}
}
}

可是在运行调试的时候老是出现以下的错误:
0x1000FDCD (libcocos2d.dll) (HelloWorld.exe 中)处有未经处理的异常: 0xC0000005: 读取位置 0x0000002C 时发生访问冲突。

Size Texture2D::getContentSize() const
{
Size ret;
ret.width = _contentSize.width / CC_CONTENT_SCALE_FACTOR();//中断的时候箭头指向这一行
ret.height = _contentSize.height / CC_CONTENT_SCALE_FACTOR();

return ret;

}

看下调用了 getContentSize() 函数 的对象是不是正常值,这大概是传进来的_contentSize出了问题。慢慢调试

那个问题解决了,新的问题是,拼图没有办法移动,但是触摸的函数是没有问题的,在逻辑上我找不到错了