Paw*_*shi 3 c++ ios cocos2d-x cocos2d-x-3.0
我正在使用CCTouchTargetedDelegate和CCSprite子类.在定义委托方法时,我无法在函数内部使用"this".
正如先前提到的问题所回答的那样,我无法使用具有使用范围分辨率的函数的类的名称,因为它然后给了我"'ccTouchBegan'的外线定义与'mygames :: DragSprite中的任何声明都不匹配的错误""
我也尝试在.h文件中声明该函数,但似乎没有任何效果.
我的代码如下: -
.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;
};
}
Run Code Online (Sandbox Code Playgroud)
.cpp文件
#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;
}
Run Code Online (Sandbox Code Playgroud)
错误屏幕截图: -

我在这里失踪了什么?
只是出于好奇心
AS在答案中建议,如果我使用
bool DragSprite::ccTouchBegan
Run Code Online (Sandbox Code Playgroud)
那么,它还会调用delegete函数吗?或者只是我的DragSprite类中的函数.我的意思是,该功能是否仍会被覆盖?嗯...它是在CCTargetedTouchDelegete中声明的方法.我猜它是一个抽象的函数.
Kar*_*k T 13
bool ccTouchBegan(
Run Code Online (Sandbox Code Playgroud)
需要是
bool DragSprite::ccTouchBegan(
Run Code Online (Sandbox Code Playgroud)
你不应该this首先需要.
| 归档时间: |
|
| 查看次数: |
17797 次 |
| 最近记录: |