Mets Blog

Cocos2D-X Platformer tutorial Part 10 – Refactor Player

To fix the stuttering issue and making the code cleaner we are going to refactor the Player class. Instead of having the player sprite as a separate field we will extend the Sprite class.
After that we will make the necessary changes to GameScene.

  • Even we have a sprite for the player we will save its container size separately, because scaling or animating effects might change the size and break collision detection
  • There will be changes in initializing and updating the GameScene
  • Update player sprite method is removed from GameScene, it belongs to the Player class now
  • Updated to 3.9, next part will improve the scaling


  • mudboy

    also the AppDelegate.h file needs the declaration:
    virtual void initGLContextAttrs();

    • metin

      It should be there since it was auto generated, but thanks will post the AppDelegate.h too

  • nishanth

    Hi metin in Player class some members are not found in below i mentioned error details kindly review it.

    ||=== Build: all in MyGame (compiler: GNU GCC Compiler) ===|
    /home/nishanth/projects/Platfomer/Classes/GameLayer.cpp||In member function ‘void GameLayer::updateScene(float)’:|
    /home/nishanth/projects/Platfomer/Classes/GameLayer.cpp|134|error: ‘class Player’ has no member named ‘getPositionX’|
    /home/nishanth/projects/Platfomer/Classes/GameLayer.cpp||In member function ‘void GameLayer::updatePlayer(float)’:|
    /home/nishanth/projects/Platfomer/Classes/GameLayer.cpp|168|error: ‘class Player’ has no member named ‘getBoundingBox’|
    /home/nishanth/projects/Platfomer/Classes/GameLayer.cpp|175|error: ‘class Player’ has no member named ‘getPositionX’|
    /home/nishanth/projects/Platfomer/Classes/GameLayer.cpp|176|error: ‘class Player’ has no member named ‘getPositionY’|
    /home/nishanth/projects/Platfomer/Classes/GameLayer.cpp|185|error: ‘class Player’ has no member named ‘getBoundingBox’|
    /home/nishanth/projects/Platfomer/Classes/GameLayer.cpp|186|error: ‘class Player’ has no member named ‘getBoundingBox’|
    /home/nishanth/projects/Platfomer/Classes/GameLayer.cpp|207|error: ‘class Player’ has no member named ‘getBoundingBox’|
    /home/nishanth/projects/Platfomer/Classes/GameLayer.cpp|208|error: ‘class Player’ has no member named ‘getBoundingBox’|
    /home/nishanth/projects/Platfomer/Classes/GameLayer.cpp|218|error: ‘class Player’ has no member named ‘setPositionY’|
    /home/nishanth/projects/Platfomer/Classes/GameLayer.cpp|218|error: ‘class Player’ has no member named ‘getPositionY’|
    /home/nishanth/projects/Platfomer/Classes/GameLayer.cpp|222|error: ‘class Player’ has no member named ‘setPositionY’|
    /home/nishanth/projects/Platfomer/Classes/GameLayer.cpp|241|error: ‘class Player’ has no member named ‘setPosition’|
    /home/nishanth/projects/Platfomer/Classes/GameLayer.cpp||In constructor ‘GameLayer::GameLayer()’:|
    /home/nishanth/projects/Platfomer/Classes/GameLayer.cpp|290|warning: ‘virtual void cocos2d::Layer::setKeyboardEnabled(bool)’ is deprecated (declared at /home/nishanth/projects/Platfomer/cocos2d/cocos/2d/CCLayer.h:207) [-Wdeprecated-declarations]|
    ||=== Build failed: 12 error(s), 1 warning(s) (0 minute(s), 6 second(s)) ===|