Search code examples
cocos2d-x

A ERROR about Sprite::init() in cocos2dx


i want create a game like don't touch block. when i create a Block, i want let Sprite can init.code is here:

        #pragma once
        #include<iostream>
        #include "cocos2d.h"
        #include "Block.h"
        USING_NS_CC; 

Block* CreateWithArgs(Color3B color, Size size, std::string label, float fontsize, Color4B textcolor)
{
    auto b = new Block();
    b->initWithArgs(color, size, label, fontsize, textcolor);
    b->autorelease();
    return b;

}


bool initWithArgs(Color3B color, Size size, std::string label, float fontsize, Color4B textcolor)
{

    Sprite::init();
    return true;
}

but when i code this , i foud the error in Sprite::init(); VS2012 tell me "Don't have access to protected members (in cocos2d: : Sprite class declaration)"


Solution

  • You are writing C functions:

    Block* CreateWithArgs(Color3B color, Size size, std::string label, float fontsize, Color4B textcolor)
    bool initWithArgs(Color3B color, Size size, std::string label, float fontsize, Color4B textcolor)
    

    In C++ class methods need to be prefixed with the name of the class, for instance assuming class is MySprite:

    Block* MySprite::CreateWithArgs(Color3B color, Size size, std::string label, float fontsize, Color4B textcolor)
    bool MySprite::initWithArgs(Color3B color, Size size, std::string label, float fontsize, Color4B textcolor)