Search code examples
c++cocos2d-iphone

CC_SYNTHESIZE(int, beadColor, _BeadColor); Invalid use of 'this' outside of a non-static member function


I have some issues in my cocos2d-x code. I post my code at here.

BeadSprite.h

#include "cocos2d.h"
#include "iSet.h"
#include "LHSprite.h"

enum{
    BlueBead = 1,
    RedBead = 2,
    GreenBead = 3,
    WhiteBead = 4,
    BlackBead = 5,
    HeartBead = 6,
    StrongBlueBead = 7,
    StrongRedBead = 8,
    StrongGreenBead = 9,
    StrongWhiteBead = 10,
    StrongBlackBead = 11,
    StrongHeartBead = 12,
    ClearBead = 13,
};

USING_NS_CC;

class BeadSprite : public LHSprite
{
private:

    void changeBeadColorAction(int ToColor);
    void changeBeadColor(int ToColor);
    void boombBeads(int ToColor);
    void boombStrongBeads(int ToColor);
    void boombStrongFX();
    void runShatterEffectWithCan(CCDelayTime* time);
public:
    /*static LHSprite* spriteWithName(const std::string& spriteName,
                                    const std::string& sheetName,
                                    const std::string& spriteHelperFile);*/
    CC_SYNTHESIZE(int, beadColor, _BeadColor);
};

#endif

BeadSprite.cpp

#include "BeadSprite.h"
using namespace cocos2d;

void FsetBeadColor(const std::string& color){
    if(color == "BlueBead") this->beadColor = BlueBead; <-Invalid use of 'this' outside of a non-static member function
    if(color == "RedBead") this->beadColor = RedBead; <-Invalid use of 'this' outside of a non-static member function
    if(color == "GreenBead") this->beadColor = GreenBead; <-Invalid use of 'this' outside of a non-static member function
    if(color == "WhiteBead") this->beadColor = WhiteBead; <-Invalid use of 'this' outside of a non-static member function
    if(color == "BlackBead") this->beadColor = BlackBead; <-Invalid use of 'this' outside of a non-static member function
    if(color == "HeartBead") this->beadColor = HeartBead; <-Invalid use of 'this' outside of a non-static member function
}

void changeBeadColorAction(int ToColor){

}

error: BeadSprite.cpp:31:29: Invalid use of 'this' outside of a non-static member function

How to fix this bug? please :( this use cocos2d-x-2.2.1


Solution

  • Your use of CC_SYNTHESIZE will create a private member beadColor and public getter get_BeadColor and setter set_BeadColor. So your function implementation has the wrong name and needs to be qualified as belonging to the class BeadSprite:

    void BeadSprite::set_BeadColor(const std::string& color) {
      // ...
    }