我想子類CCSprite
爲了添加拖動功能在精靈在COCOS2D-X,
這就是爲什麼我想用它CCTouchDelegate
。如何在cocos2dx中使用CCTargetedTouchDelegate?
但我得到的錯誤是關於重新定義委託方法。
好......代表方法是要重新定義的,不是嗎?
我想我一定在某處做了某些事情,但是找不到它。
我的代碼如下。
.h文件中
namespace mygames
{
class DragSprite: public cocos2d::CCSprite, cocos2d::CCTargetedTouchDelegate
{
public:
DragSprite* createWithFile(const char *pszFileName);
bool isTouchingOnSprite(cocos2d::CCPoint touch);
private :
bool isDrag;
cocos2d::CCPoint whereTouch;
};
}
.cpp文件
#include "DragSprite.h"
using namespace mygames;
DragSprite* DragSprite::createWithFile(const char *pszFileName)
{
DragSprite *pSprite = new DragSprite();
if (pSprite&&pSprite->initWithFile(pszFileName))
{
cocos2d::CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(pSprite, 0, true);
pSprite->autorelease();
return pSprite;
}
CC_SAFE_DELETE(pSprite);
return NULL;
}
bool DragSprite::isTouchingOnSprite(cocos2d::CCPoint touch)
{
if (this->boundingBox().containsPoint(touch)) {
return true;
}
else
{
return false;
}
}
bool cocos2d::CCTargetedTouchDelegate::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
}
void cocos2d::CCTargetedTouchDelegate::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
}
void cocos2d::CCTargetedTouchDelegate::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
}
void cocos2d::CCTargetedTouchDelegate::ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
}
CCTargetedTouchDelegate類
class CC_DLL CCTargetedTouchDelegate : public CCTouchDelegate
{
public:
/** Return YES to claim the touch.
@since v0
*/
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) { CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);return false;};
// optional
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}
virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}
};
CCTouchDelegate類
class CC_DLL CCTouchDelegate
{
public:
CCTouchDelegate() {}
virtual ~CCTouchDelegate()
{
}
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent); return false;};
// optional
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}
virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}
// optional
virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);}
virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);}
virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);}
virtual void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);}
};
只是出於好奇: -
,因爲我已經從CCTargetedTouchDelegate 繼承即我的課的語法是 類DragSprite:公衆的cocos2d :: CCSprite,公衆的cocos2d :: CCTargetedTouchDelegate {}
我仍然需要利用範圍分辨率運營商在錯誤發生代碼 即
bool cocos2d::CCTargetedTouchDelegate::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
}
void cocos2d::CCTargetedTouchDelegate::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
}
void cocos2d::CCTargetedTouchDelegate::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
}
void cocos2d::CCTargetedTouchDelegate::ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
}
我不能只是寫像下面?它會有所作爲嗎?
bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
}
void ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
}
void ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
}
void ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
}
所需的代表是否仍會被調用?
因爲我可以看到錯誤消失了,如果我這樣做。
而不是screenshotting你的代碼,請在這裏複製並粘貼相關部分。這會讓我們更容易幫助你。 – computerfreaker
我在Cocos2d-x論壇上回復了您的帖子。 – GameDeveloper