加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 在非静态成员函数错误之外无效使用’this’?

发布时间:2020-12-16 10:34:34 所属栏目:百科 来源:网络整理
导读:我正在使用CCTouchTargetedDelegate和CCSprite子类. 在定义委托方法时,我无法在函数内部使用“this”. 如先前提出的问题所述 我无法使用具有使用范围分辨率的函数的类的名称,因为它然后给了我错误 “’ccTouchBegan’的外线定义与’mygames :: DragSprite’
我正在使用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;
    };
}

.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,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;
}

错误屏幕截图: –

我在这里失踪了什么?

只是出于好奇心

AS在答案中建议,如果我使用

bool DragSprite::ccTouchBegan

那么,它还会调用delegete函数吗?或者只是我的DragSprite类中的函数.我的意思是,该功能是否仍会被覆盖?
好…
它是在CCTargetedTouchDelegete中声明的方法.我猜它是一个抽象的函数.

解决方法

bool ccTouchBegan(

需要是

bool DragSprite::ccTouchBegan(

你不应该首先需要这个.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读