Search code examples
cocos2d-x

Can't create the child class of CCSprite in Cocos2d-x


I want to create a new class which is a child class of CCSprite. But it doesn't work.

It got the error CCBullet does not name a type

Please take a look and tell me some your ideas to solve my problems. Thanks.

CCBullet.h

#ifndef __GameplayScene_H__
#define __GameplayScene_H__

#include "cocos2d.h"
#include "common/Define.h"

#if ENABLE_PHYSICS_BOX2D_DETECT
#include "../../Box2DTestBed/GLES-Render.h"
#include "Box2D/Box2D.h"
#elif ENABLE_PHYSICS_CHIPMUNK_DETECT
#include "chipmunk.h"
#endif

USING_NS_CC;

class CCBullet : public cocos2d::CCSprite
{
public:
    static CCBullet* create(int bulletID, const char *filePath);
};

#endif

CCBullet.cpp

#include "common/Define.h"
USING_NS_CC;
using namespace cocos2d;
using namespace cocos2d::extension;

CCBullet* CCBullet::create(int bulletID, const char *filePath){
    CCBullet *pobSprite = new CCSprite();
    if (pobSprite && pobSprite->initWithFile(filePath))
    {       
        pobSprite->mBulletID = bulletID;    
        pobSprite->mAngle = 0;
        pobSprite->mSpeed = 0;
        pobSprite->mStrength = 0;
        pobSprite->mPushBack = 0;
        pobSprite->mCritical = 0;
        pobSprite->mFanShoot = 0;
        pobSprite->mSpread = 0;
        pobSprite->autorelease();
        return pobSprite;
    }
    CC_SAFE_DELETE(pobSprite);
    return NULL;
}

Solution

  • I've tested with the code below and it compile without problem. There must be something else in your code that cause the issue.

    CCBullet.h

    #ifndef __GameplayScene_H__
    #define __GameplayScene_H__
    
    #include "cocos2d.h"
    /// #include "common/Define.h"
    
    #if ENABLE_PHYSICS_BOX2D_DETECT
    #include "../../Box2DTestBed/GLES-Render.h"
    #include "Box2D/Box2D.h"
    #elif ENABLE_PHYSICS_CHIPMUNK_DETECT
    #include "chipmunk.h"
    #endif
    
    USING_NS_CC;
    
    class CCBullet : public cocos2d::CCSprite
    {
    public:
        static CCBullet* create(int bulletID, const char *filePath);
    protected:
        float mBulletID;
        float mAngle;
        float mSpeed;
        float mStrength;
        float mPushBack;
        float mCritical;
        float mFanShoot;
        float mSpread;
    };
    
    #endif
    

    CCBullet.cpp

    /// #include "common/Define.h"
    #include "CCBullet.h"
    USING_NS_CC;
    using namespace cocos2d;
    /// using namespace cocos2d::extension;
    
    CCBullet* CCBullet::create(int bulletID, const char *filePath){
        CCBullet *pobSprite = new CCBullet();
        if (pobSprite && pobSprite->initWithFile(filePath))
        {
            pobSprite->mBulletID = bulletID;
            pobSprite->mAngle = 0;
            pobSprite->mSpeed = 0;
            pobSprite->mStrength = 0;
            pobSprite->mPushBack = 0;
            pobSprite->mCritical = 0;
            pobSprite->mFanShoot = 0;
            pobSprite->mSpread = 0;
            pobSprite->autorelease();
            return pobSprite;
        }
        CC_SAFE_DELETE(pobSprite);
        return NULL;
    }