我正在使用由CCSprite子类定义的类,并且结合CCTouchTargetedDelegate来使用。在定义委托方法时,我无法在函数内部使用"this"。
正如之前提出的问题所回答的那样,我不能使用带有作用域分辨符的函数名称,因为这会导致错误:“ccTouchBegan的非行内定义与'mygames :: DragSprite'中的任何声明不匹配”。
我还尝试在.h文件中声明该函数,但似乎没有什么作用。
我的代码如下:
那么,它还会调用委托函数吗?还是只调用 DragSprite 类中的函数?我的意思是,函数是否仍然被覆盖? 嗯...... 这是在 CCTargetedTouchDelegate 中声明的方法。我想它是一个抽象函数。
正如之前提出的问题所回答的那样,我不能使用带有作用域分辨符的函数名称,因为这会导致错误:“ccTouchBegan的非行内定义与'mygames :: DragSprite'中的任何声明不匹配”。
我还尝试在.h文件中声明该函数,但似乎没有什么作用。
我的代码如下:
#pragma once
#include "cocos2d.h"
namespace mygames
{
class DragSprite: public cocos2d::CCSprite, public cocos2d::CCTargetedTouchDelegate
{
public:
DragSprite* createWithFile(const char *pszFileName);
bool isTouchingOnSprite(cocos2d::CCPoint touch);
virtual bool init();
bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
static inline cocos2d::CCPoint ccpSub(const cocos2d::CCPoint v1, const cocos2d::CCPoint v2);
private:
bool isDrag;
cocos2d::CCPoint whereTouch;
};
}
.cpp File
#include "DragSprite.h"
using namespace mygames;
bool DragSprite::init()
{
if (!CCSprite::init()) {
return false;
}
whereTouch = cocos2d::CCPointZero;
isDrag = false;
return true;
}
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;
}
}
static inline cocos2d::CCPoint ccpSub(const cocos2d::CCPoint v1, const cocos2d::CCPoint v2)
{
return ccp(v1.x-v2.x, v1.y-v2.y);
}
//CCTargetedTouchDelegate
bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
cocos2d::CCPoint touchPoint = pTouch->getLocation();
if (this->isTouchingOnSprite(touchPoint)) {
this->whereTouch = ccpSub(this->position, touchPoint);
return true;
}
return false;
}
错误截图:
我在这里缺少了什么?
出于好奇
根据答案建议,如果我使用
bool DragSprite::ccTouchBegan
那么,它还会调用委托函数吗?还是只调用 DragSprite 类中的函数?我的意思是,函数是否仍然被覆盖? 嗯...... 这是在 CCTargetedTouchDelegate 中声明的方法。我想它是一个抽象函数。
DragSprite::
。 - Bryan Chenbool ccTouchBegan
的定义中漏掉了类作用域。 - juanchopanzathis
的地方,在大多数其他地方(包括这里)使用它被认为是不好的风格。 - Karthik T