I wanted to add player to my tiled map, but I got segmentation fault.
Here are files:
GameScene.cpp:
#include "GameScene.h"
#include "globals.h"
USING_NS_CC;
Scene* GameScene::createScene()
{
return GameScene::create();
}
// Print useful error message instead of segfaulting when files are not there.
static void problemLoading(const char* filename)
{
printf("Error while loading: %s\n", filename);
printf("Depending on how you compiled you might have to add 'Resources/' in front of filenames in HelloWorldScene.cpp\n");
}
// on "init" you need to initialize your instance
bool GameScene::init()
{
//////////////////////////////
// 1. super init first
if ( !Scene::init() )
{
return false;
}
level = new Level;
level->loadMap("level1.tmx");
level->retain();
auto director = Director::getInstance();
level->getMap()->setScale(SCALE_FACTOR);
this->addChild(level->getMap());
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("solbrain.plist");
cocos2d::Sprite::createWithSpriteFrameName("idle");
player_sprite->setScale(SCALE_FACTOR);
player_sprite->setFlippedX(true);
Point point = Point(10, 2);
Size size = player_sprite->getContentSize();
player_sprite->setPosition(level->positionForTileCoordinate(size, point));
player = new Player();
player->retain();
this->addChild(player_sprite);
return true;
}
GameScene.h:
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include "Level.h"
#include "Player.h"
USING_NS_CC;
class GameScene : public cocos2d::Scene
{
public:
Level* level;
Player* player;
Sprite* player_sprite;
static cocos2d::Scene* createScene();
virtual bool init();
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);
// implement the "static create()" method manually
CREATE_FUNC(GameScene);
};
#endif
Level.cpp:
#include "Level.h"
#include "globals.h"
void Level::loadMap(const char *mapname) {
map = TMXTiledMap::create(mapname);
map->retain();
}
TMXTiledMap *Level::getMap() {
return map;
}
Level::Level() {
}
Level::~Level() {
map->release();
}
Point Level::positionForTileCoordinate(Size s, Point point) {
float x = floor(s.width / 2 * SCALE_FACTOR + point.x * map->getTileSize().width * SCALE_FACTOR);
float y = floor(s.height / 2 * SCALE_FACTOR + point.y * map->getTileSize().height * SCALE_FACTOR);
return Point(x, y);
}
Level.h:
#pragma once
#include "cocos2d.h"
using namespace cocos2d;
class Level : public Ref{
public:
TMXTiledMap* map;
void loadMap(const char* mapname);
TMXTiledMap* getMap();
Point positionForTileCoordinate(Size s, Point point);
Level();
virtual ~Level();
};
Player.cpp:
#include "Player.h"
Player::Player() {
state = Player::State::Standing;
facingRight = true;
grounded = true;
stateTime = 0;
velocity = Point(0, 0);
position = Point();
}
Player::~Player() {
}
Player.h:
#pragma once
#include "cocos2d.h"
USING_NS_CC;
class Player : public Ref{
public:
enum State {
Standing, Walking, Jumping
};
State state;
bool facingRight;
bool grounded;
float stateTime;
Point position;
Point velocity;
Player();
virtual ~Player();
};
Am I stupid or it’s not my fault?