我想继承 CCSprite 以便在 COCOS2D-X 中的 Sprite 上添加拖动功能, 这就是为什么我想使用 CCTouchDelegate 它。
但我得到的错误是关于委托(delegate)方法的重新定义。
嗯....委托(delegate)方法应该被重新定义,不是吗?
我想我一定是在某个地方做错了什么,但找不到。
我的代码如下。
.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:公共(public) cocos2d::CCSprite,公共(public) 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)
{
}
还需要调用所需的代表吗?
因为我可以看到错误消失了,如果我这样做的话。
Best Answer-推荐答案 strong>
如果要覆盖 DragSprite 类中的方法,则需要使用该类的前缀。
而不是重新实现 cocos2d 命名空间/类中的类:
void cocos2d::CCTargetedTouchDel....
您需要像这样在自己的类中实现方法:
void DragSprite::CCTargetedTouchDel....
关于c++ - cocos2dx中如何使用CCTargetedTouchDelegate?,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/23441824/
|