You know, to tell you the truth when I’m writing this I’m actually rediscovering cocos2d-x myself :D, how to do some things easier, but then again I only did board games on cocos2d-x … anyways back to tutorial.
Camera in cocos2d-x follows a certain target, normally this would be the player, but you need to define some boundaries to center the camera. This is actually a problem, because if I define boundaries I need to set the size of the playfield. I don’t like boundaries and the tmx map can have more than one map in it so screw that.
I defined a boundless camera which is going to follow an invisible target, this target’s y value will be always centered and x value will change according to player’s x value.
New stuff
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 |
#pragma once #include "cocos2d.h" #include "Level.h" #include "Player.h" #include <algorithm> #include <vector> #include <string> using namespace std; USING_NS_CC; class GameScene : public cocos2d::Layer { public: //FIELDS Level * level; Player *player; Sprite *player_sprite; Sprite *cameraTarget; Animate *walkRight; Animate *jumping; Animate *falling; Follow *camera; vector<EventKeyboard::KeyCode> heldKeys; //CONSTRUCTOR & METHODS virtual void onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event); virtual void onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event); void updateScene(float interval); void updatePlayer(float interval); void updatePlayerSprite(float interval); int signum(float x); static cocos2d::Scene* createScene(); virtual bool init(); void menuCloseCallback(cocos2d::Ref* pSender); void setupAnimations(); 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
#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"); AnimationCache::getInstance()->addAnimationsWithFile("solbrain-animations.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(); player->state = Player::State::Standing; Point origin = Director::getInstance()->getVisibleOrigin(); Size wsize = Director::getInstance()->getVisibleSize(); //default screen size (or design resolution size, if you are using design resolution) Point *center = new Point(wsize.width/2 + origin.x, wsize.height/2 + origin.y); cameraTarget = Sprite::create(); cameraTarget->setPositionX(player_sprite->getPosition().x); // set to players x cameraTarget->setPositionY(wsize.height/2 + origin.y); // center of height cameraTarget->retain(); this->setupAnimations(); this->addChild(player_sprite); this->schedule(schedule_selector(GameScene::updateScene)); this->addChild(cameraTarget); camera = Follow::create(cameraTarget, Rect::ZERO); camera->retain(); this->runAction(camera); return true; } void GameScene::setupAnimations(){ AnimationCache *cache = AnimationCache::getInstance(); Animation *animation = cache->getAnimation("walk"); Animate* animate = Animate::create(animation); animate->getAnimation()->setRestoreOriginalFrame(true); animate->setDuration(0.80f); animate->setTarget(player_sprite); this->walkRight = animate; this->walkRight->retain(); } void GameScene::updateScene(float delta){ cameraTarget->setPositionX( player_sprite->getPosition().x ); this->updatePlayer(delta); } void GameScene::updatePlayer(float delta){ if(std::find(heldKeys.begin(), heldKeys.end(), RIGHT_ARROW) != heldKeys.end()){ player->velocity.x = PLAYER_MAX_VELOCITY; if(player->grounded){ player->state = Player::State::Walking; } player->facingRight = true; } if(std::find(heldKeys.begin(), heldKeys.end(), LEFT_ARROW) != heldKeys.end()){ player->velocity.x = -PLAYER_MAX_VELOCITY; if(player->grounded){ player->state = Player::State::Walking; } player->facingRight = false; } // clamp the velocity to the maximum, x-axis only if (std::abs(player->velocity.x) > PLAYER_MAX_VELOCITY) { player->velocity.x = signum(player->velocity.x) * PLAYER_MAX_VELOCITY; } // clamp the velocity to 0 if it's < 1, and set the state to standing if (std::abs(player->velocity.x) < 1) { player->velocity.x = 0; if (player->grounded) { player->state = Player::State::Standing; } } // unscale the velocity by the inverse delta time and set // the latest position player->velocity = player->velocity * delta; player->position = player->position + player->velocity; player->velocity = player->velocity * 1/delta; player->velocity.x *= DAMPING; this->updatePlayerSprite(delta); } void GameScene::updatePlayerSprite(float delta){ if(player->state == Player::State::Walking){ if(walkRight->isDone()){ walkRight->startWithTarget(player_sprite); } walkRight->step(delta); if(player->facingRight){ player_sprite->setFlippedX(true); }else { player_sprite->setFlippedX(false); } }else if(player->state == Player::State::Jumping){ }else{ player_sprite->setSpriteFrame(Sprite::createWithSpriteFrameName("idle")->getSpriteFrame()); } player_sprite->setPositionX(player_sprite->getPositionX() + player->velocity.x); } void GameScene::onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event) { if(std::find(heldKeys.begin(), heldKeys.end(), keyCode) == heldKeys.end()){ heldKeys.push_back(keyCode); } } void GameScene::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event) { heldKeys.erase(std::remove(heldKeys.begin(), heldKeys.end(), keyCode), heldKeys.end()); } int GameScene::signum(float x) { if (x > 0.0L) return 1.0L; else if (x < 0.0L) return -1.0L; else return 0.0L; } void GameScene::menuCloseCallback(Ref* pSender) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) return; #endif Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif } GameScene::GameScene(void) { setKeyboardEnabled(true); } GameScene::~GameScene(void) { } |