Need help on developing snake game projecct

Hi guys, i am having a trouble.
Ok, let me show my code then i will explain.
scene.cpp file:

bool GameScene::init()
{
         ....
	/////
	
	pLayer = new PlayLayer();
	this->addChild(pLayer);

	this->scheduleUpdate();

	return true;
}


void GameScene::update(float dt)
{
	pLayer->update();
}

layer.cpp

PlayLayer::PlayLayer()
{
	visibleSize = cocos2d::Director::getInstance()->getVisibleSize();
	score = 0;

	snake = Snake::create();
	snake->setPosition(Vec2(Globals::getVisibleSize().width / 2 - 300, Globals::getVisibleSize().height / 2 + 50));
	this->addChild(snake, 5);
	
	food = Food::create();

	this->addChild(food, 2);
	snake->currentFood = food;

	gameOver = false;
}
void PlayLayer::update()
{
	snake->update();
}

and the last one is update function in snake.cpp file

void Snake::update(){

	{
		auto _mp = snakeHead->getPosition();
		auto _Mp = _mp.operator+(Vec2(BODY_INTERVAL * sind(snakeHead->getRotation()), BODY_INTERVAL * cosd(snakeHead->getRotation())));
		snakeHead->setPosition(_Mp);
	}

	isMoving = true;

	for (int i = 1; i < size; i++){
		auto _mp = snakeBodyParts.at(i)->getPosition();
		Vec2 vec(snakeBodyParts.at(i - 1)->getPosition().x, snakeBodyParts.at(i - 1)->getPosition().y);
		auto _Mp = _mp.operator+(vec);
		snakeBodyParts.at(i)->setPosition(_Mp);
	}
}

My problem is when i run project snake not appeared. I don’t know how to fix.
I have a Snake class inherited Node class, this Snake node groups many sprites, those sprites are each part of a snake.I have a layer which is child of scene, and that layer is parent of Snake node, and yeah snake parts are child of Snake. I call function update of layer in function update of scene.cpp, then update function of layer calls update of snake. My function update of snake is to move snake from left to right.Everything is find until i run project , the snake does not appear. Can you guys help me out of this? Really appreciate

Can you explain why you are reducing the scope above? Once the scope is over, gone is anything you did while in that scope.

1 Like

oh that’s my mistake when i add that scope, i get rid of it, and snake still does not appear. I added CCLOG() to check whether update function of snake is called, and the result is yes,that update function is called But i don’t know why the snake does not appear. Here the clue: when i do like this

auto _mp = snakeHead->getPosition();
	//auto _Mp = _mp.operator+(Vec2(BODY_INTERVAL * sind(snakeHead->getRotation()), BODY_INTERVAL * cosd(snakeHead->getRotation())));
	auto _Mp = _mp.operator+(Vec2(100,0));
	snakeHead->setPosition(_Mp);

	isMoving = true;

	/*for (int i = 1; i < size; i++){
		auto _mp = snakeBodyParts.at(i)->getPosition();
		Vec2 vec(snakeBodyParts.at(i - 1)->getPosition().x, snakeBodyParts.at(i - 1)->getPosition().y);
		auto _Mp = _mp.operator+(vec);
		snakeBodyParts.at(i)->setPosition(_Mp);
	}*/ // i comment to all of this
	CCLOG("UPDATE");

and run project, snake body appears at the initial position but they don’t move . And when i do like this

/*auto _mp = snakeHead->getPosition();
	auto _Mp = _mp.operator+(Vec2(BODY_INTERVAL * sind(snakeHead->getRotation()), BODY_INTERVAL * cosd(snakeHead->getRotation())));
	auto _Mp = _mp.operator+(Vec2(100,0));
	snakeHead->setPosition(_Mp);*/ // i comment all of this

	isMoving = true;

	for (int i = 1; i < size; i++){
		auto _mp = snakeBodyParts.at(i)->getPosition();
		Vec2 vec(snakeBodyParts.at(i - 1)->getPosition().x, snakeBodyParts.at(i - 1)->getPosition().y);
		auto _Mp = _mp.operator+(vec);
		snakeBodyParts.at(i)->setPosition(_Mp);
	}
	CCLOG("UPDATE");

and run project snake head appears at initial position and does not move . And snake body does not appear.
Any clue. Thanks for spending your time

Use Actions to move Objects.

https://docs.cocos2d-x.org/cocos2d-x/v4/en/actions/

why not put log statements in your for loop and output position and other necessary debugging info.
Better would be to add breakpoints and just debug it while it is running and watch the console and the stack.

I dont have time to step through each step of your code distributed among multiple posts.

1 Like