Okay time to add the player to the scene we created before in the first part of this tutorial.
Stuff that is new and things to look out for.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#pragma once #include "cocos2d.h" using namespace cocos2d; class Player : public Object { public: //FIELDS enum State { Standing, Walking, Jumping }; State state; bool facingRight; bool grounded; float stateTime; Point position; Point velocity; Player(void); virtual ~Player(void); }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include "Player.h" Player::Player(void) { state = Player::State::Standing; facingRight = true; grounded = true; stateTime = 0; velocity = Point(0,0); position = Point(); } Player::~Player(void) { } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#pragma once #include "cocos2d.h" using namespace cocos2d; class Level : public Object { public: TMXTiledMap *map; void loadMap(const char* name); TMXTiledMap * getMap(); Point positionForTileCoordinate(Size s, Point point); Level(void); virtual ~Level(void); }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include "Level.h" #include "globals.h" void Level::loadMap(const char* mapname){ map = TMXTiledMap::create(mapname); map->retain(); } TMXTiledMap * Level::getMap(){ return map; } 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::Level(void) { } Level::~Level(void) { map->release(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#pragma once #include "cocos2d.h" #include "Level.h" #include "Player.h" USING_NS_CC; class GameScene : public cocos2d::Layer { public: //FIELDS Level * level; Player *player; Sprite *player_sprite; //CONSTRUCTOR & METHODS static cocos2d::Scene* createScene(); virtual bool init(); void menuCloseCallback(cocos2d::Ref* pSender); CREATE_FUNC(GameScene); GameScene(void); virtual ~GameScene(void); }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
#include "GameScene.h" #include "globals.h" Scene* GameScene::createScene() { auto scene = Scene::create(); auto layer = GameScene::create(); scene->addChild(layer); return scene; } bool GameScene::init() { if ( !Layer::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"); player_sprite = 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::GameScene(){ } GameScene::~GameScene(void) { } |
1 2 3 4 5 6 7 8 |
#pragma once const float SCALE_FACTOR = 2.0f; const float PLAYER_MAX_VELOCITY = 10.0f; const float PLAYER_JUMP_VELOCITY = 40.0f; const float DAMPING = 0.87f; |