无法在Cocos2d-x中创建CCSprite的子类

时间:2014-03-05 18:47:19

标签: cocos2d-x

我想创建一个新类,它是CCSprite的子类。但它不起作用。

出现错误 CCBullet未命名类型

请看一下,告诉我一些解决问题的想法。感谢。

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;
}

1 个答案:

答案 0 :(得分:1)

我已经使用下面的代码进行了测试,并且编译没有问题。您的代码中必须存在导致问题的其他内容。

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;
}