2014-03-05 19 views
0

我想創建一個新類,該子類是CCSprite的子類。但它不起作用。無法在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; 
} 
+0

你說的 「它不工作」 是什麼意思?我想你必須實例化一個CCBullet而不是CCSprite。請將'new CCSprite()'換成'new CCBullet()'。 –

+0

@LaurentZubiaur:它得到了錯誤CCBullet沒有命名我的問題中更新的類型。你對這個錯誤有什麼想法嗎? – lolyoshi

+0

請仔細檢查CCBullet.cpp是否包含CCBullet.h。 –

回答

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

讓我再試一次。另外,我應該使用pobSprite-> mAngle = 0;或this-> mAngle = 0? – lolyoshi

+0

在CCBullet :: create中,你必須使用pobSprite-> mAngle(這是一個靜態方法,所以你不能使用'this')。 –

+0

我知道錯誤的原因。我必須在其他調用CCBullet的類之前聲明CCBullet。但是,我得到這個錯誤未定義引用'CCBullet :: create(int,char const *)' – lolyoshi