小白程序媛求助大佬!!一个简单的问题!!!

Scene * game::createScene() {
auto scene = Scene::create();
auto layer = game::create();
scene->addChild(layer);
return scene;
}
SnakeNode* SnakeNode::create(int type)
{
SnakeNode *pRet = new SnakeNode;
if (pRet && pRet->init(type))
{
pRet->autorelease();
return pRet;
}
else
{
delete pRet;
pRet = nullptr;
return nullptr;
}
}

bool SnakeNode::init(int type){
if (!Sprite::init())
{
return false;
}
//根据类型不同初始化不同的纹理
switch (type)
{
case 1://蛇头
{auto sprite = Sprite::create(“head.png”);
sprite->setAnchorPoint(Point::ZERO);
this->addChild(sprite);
m_dir = ENUM_DIR::DIR_RIGHT;//向右移动
}
break;
case 2://身体
{auto sprite = Sprite::create(“body.png”);
sprite->setAnchorPoint(Point::ZERO);
this->addChild(sprite);
m_dir = ENUM_DIR::DIR_RIGHT;//向右移动
}
m_dir = ENUM_DIR::DIR_STOP;
break;
case 3://食物
{
auto sprite = Sprite::create(“food.png”);
sprite->setAnchorPoint(Point::ZERO);
this->addChild(sprite);
}
m_dir = ENUM_DIR::DIR_STOP;//停止
break;
default:
break;
}
return true;
}
//设置节点坐标
void SnakeNode::setPositionRC(int row, int col)
{
this->m_row = row;
this->m_col = col;
setPosition(Point(row * 32, col * 32));
}
bool game::init(){
if (!Layer::init())
{
return false;
}
//添加地图
auto draw = DrawNode::create();
draw->setAnchorPoint(Point::ZERO);
draw->setPosition(Point::ZERO);
this->addChild(draw);
for (int i = 0; i < 11; i++)
{
draw->drawSegment(Point(0, 32i), Point(320,32i), 1, Color4F(1, 1, 1, 1));
draw->drawSegment(Point(32i, 0), Point(32i, 320), 1, Color4F(1, 1, 1, 1));
}
//添加蛇头
spHead = SnakeNode::create(1);
this->addChild(spHead);
//添加身体
//添加食物
spFood = SnakeNode::create(3);
int row = rand() 10; int col = rand() 10;
spFood->setPositionRC(row, col);
this->addChild(spFood);
auto size = Director::getInstance()->getWinSize();
//添加背景
auto spriteBK = Sprite::create(“bg.png”);
spriteBK->setPosition(Point(size.width / 2, size.height / 2));
spriteBK->setOpacity(10);//设置透明度
this->addChild(spriteBK);
//分数显示
m_score = 0;
auto labelScore = Label::create(“SCORE:0”, “宋体”, 25);
labelScore->setTag(110);
labelScore->setPosition(Point(size.width - 80, size.height - 50));
this->addChild(labelScore);
//返回按钮
auto menuItemBack = MenuItemFont::create(“Back”, CC_CALLBACK_1(game::menuCallBack, this));
auto menu = Menu::create(menuItemBack,NULL);
menu->setPosition(Point::ZERO);
menuItemBack->setPosition(Point(size.width -
menuItemBack->getContentSize().width - 30,
menuItemBack->getContentSize().height + 30));
this->addChild(menu);
//计划任务
this->schedule(schedule_selector(game::gameLogic),0.5);
//加入用户触摸事件侦听
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan=[&](Touch *t,Event *e){
//改变贪吃蛇移动的方向
int col = t->getLocation().x / 32;
int row = t->getLocation().y / 32;
int spHeadCol = spHead->getPositionX() / 32;
int spHeadRow = spHead->getPositionY() / 32;
if (abs(spHeadCol - col) > abs(spHeadRow - row))
{
if (spHeadCol < col)
{
spHead->m_dir = ENUM_DIR::DIR_RIGHT;
}
else
{
spHead->m_dir = ENUM_DIR::DIR_LEFT;
}
}
else
{
if (spHeadRow < row)
{
spHead->m_dir = ENUM_DIR::DIR_UP;
}
else
{
spHead->m_dir = ENUM_DIR::DIR_DOWN;
}
}
return true;
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,
this);
return true;
}
void game::menuCallBack(Ref * object){
auto scene = MainMenu::createScene();
Director::getInstance()->replaceScene(scene);
}

void game::gameLogic(float t)
{
//蛇头移动
switch (spHead->m_dir){
case ENUM_DIR::DIR_RIGHT:
spHead->runAction(MoveBy::create(0.3, Point(32, 0)));
spHead->m_col++;
break;
case ENUM_DIR::DIR_LEFT:
spHead->runAction(MoveBy::create(0.3, Point(-32, 0)));
spHead->m_col–;
break;
case ENUM_DIR::DIR_UP:
spHead->runAction(MoveBy::create(0.3, Point(0, 32)));
spHead->m_row++;
break;
case ENUM_DIR::DIR_DOWN:
spHead->runAction(MoveBy::create(0.3, Point(0, -32)));
spHead->m_row–;
break;
}
//碰撞检测 当蛇头坐标和食物坐标相等时候
if (spHead->m_row == spFood->m_row&&spHead->m_col == spFood->m_col)
{
//分数增加
this->m_score += 100;
Label *label = (Label *)this->getChildByTag(110);
char strscore[20];
//sprintf(strscore, “SCORE:%d”, m_score);
cout << strscore << “:” << m_score;
label->setString(strscore);
//食物产生新的位置
int row = rand() 10; int col = rand() 10;
spFood->setPositionRC(row, col);
}
}

最后这个碰撞检测 为啥蛇头碰到食物的时候还是没反应 我实在找不到错误了 来求助大佬们 望大佬们帮帮忙:sob:

你开启碰撞检测了吗?

碰撞检测写在gamelogic里面 然后在计划任务那里已经添加啦:sob:

一个贪吃蛇小游戏:sob:

gamelogic里 加个蛇头坐标,跟食物坐标的打印
实时跟踪位置信息

啊好了!!!我给蛇头和食物加了getPosition之后就好了!!谢谢大佬!