Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t在Cocos2d-x中创建CCSprite的子类_Cocos2d X - Fatal编程技术网

Can';t在Cocos2d-x中创建CCSprite的子类

Can';t在Cocos2d-x中创建CCSprite的子类,cocos2d-x,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 "../../Box2DTest

我想创建一个新类,它是
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;
}

我已经用下面的代码进行了测试,它可以顺利编译。您的代码中一定有其他原因导致此问题

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

你说“它不起作用”是什么意思?我想你必须实例化一个CCBullet而不是CCSprite。请将
new CCSprite()
更改为
new CCBullet()
@LaurentZubiaur:出现错误,CCBullet没有命名我在问题中更新的类型。你对此错误有什么想法吗?请仔细检查CCBuller.h是否包含在CCBuller.cpp中。@LaurentZubiaur:我没有测试过你的代码,并且include“CCBuller.h”丢失了(尽管我不知道“common/Define.h”中有什么内容)。加上代码“CCBullet*pobSprite=new CCSprite();”将无法编译。让我再试一次。另外,我应该使用pobSprite->mAngle=0;或者这个->mAngle=0?在CCBullet::create中,您必须使用pobSprite->mAngle(这是一个静态方法,因此您不能使用
这个
)。我知道错误的原因。我必须在其他调用CCBullet的类之前声明CCBullet。但是,我在未定义对“CCBullet::create(int,char const*)”的引用时遇到了这个错误。我解决了这个问题。我必须将CCBullet.h放在我使用CCBullet的类的标题上方。谢谢