I am new to Cocos2d-x and am creating a simple space shooter (similar to Galaga) as a first game using Cocos. I have a pretty basic question.
I have my main class called GameScene and I want to create a Player class to hold things like the player sprite, player speed, etc, and getters and setters. With something like SFML, you’re working directly in main() but I believe it is a bit different with Cocos. Where should I instantiate my Player object? I thought it would make sense to put it in the GameScene::init() function but then it is not within the scope of other GameScene methods. I tried making it a global object in the GameScene.cpp file but I think there must be a better way of doing this.
Is there a better method of doing this or a best practice? I have posted a bit of my code as it is now:
Player.cpp
#include "Player.h"
Player::Player()
{
playerSprite = Sprite::create("Player.png");
}
Sprite* Player::getPlayerSprite()
{
return playerSprite;
}
GameScene.cpp
bool GameScene::init()
{
if (!Scene::init())
{
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
Player player;
player.getPlayerSprite()->setPosition(500, 500);
this->addChild(player.getPlayerSprite(), 0);
return true;
}