作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想子类化 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)
{
}
是否仍会召集所需的代表?
因为我可以看到错误消失了,如果我这样做的话。
最佳答案
如果您想覆盖 DragSprite 类中的方法,您需要使用该类的前缀。
而不是重新实现 cocos2d 命名空间/类中的类:
void cocos2d::CCTargetedTouchDel....
你需要像这样在你自己的类中实现这些方法:
void DragSprite::CCTargetedTouchDel....
关于c++ - 如何在cocos2dx中使用CCTargetedTouchDelegate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23441824/
我是一名优秀的程序员,十分优秀!