菜鸟教程小白 发表于 2022-12-12 22:10:34

c++ - cocos2dx中如何使用CCTargetedTouchDelegate?


                                            <p><p>我想继承 <code>CCSprite</code> 以便在 <code>COCOS2D-X 中的 Sprite 上添加拖动功能,</code> 这就是为什么我想使用 <code>CCTouchDelegate</code>它。</p>

<p>但我得到的错误是关于委托(delegate)方法的重新定义。</p>

<p>嗯....委托(delegate)方法应该被重新定义,不是吗?</p>

<p>我想我一定是在某个地方做错了什么,但找不到。</p>

<p>我的代码如下。</p>

<p><strong>.h 文件</strong></p>

<p> <img src="/image/6D0pi.png" alt=".h file"/> </p>

<pre><code>namespace mygames
{

    class DragSprite: public cocos2d::CCSprite, cocos2d::CCTargetedTouchDelegate
    {
      public:
      DragSprite* createWithFile(const char *pszFileName);
      bool isTouchingOnSprite(cocos2d::CCPointtouch);

      private :
      bool isDrag;
      cocos2d::CCPoint whereTouch;
    };
}
</code></pre>

<p><strong>.cpp 文件</strong></p>

<p> <img src="/image/1j3Ou.png" alt="enter image description here"/> </p>

<pre><code>#include &#34;DragSprite.h&#34;
using namespace mygames;

DragSprite* DragSprite::createWithFile(const char *pszFileName)
{
    DragSprite *pSprite = new DragSprite();
    if (pSprite&amp;&amp;pSprite-&gt;initWithFile(pszFileName))
    {
    cocos2d::CCDirector::sharedDirector()-&gt;getTouchDispatcher()-&gt;addTargetedDelegate(pSprite, 0, true);
      pSprite-&gt;autorelease();
      return pSprite;
    }
    CC_SAFE_DELETE(pSprite);
    return NULL;
    }

    bool DragSprite::isTouchingOnSprite(cocos2d::CCPointtouch)
    {

      if (this-&gt;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)
    {

    }
</code></pre>

<p><strong>CCTargetedTouchDelegate 类</strong></p>

<p> <img src="/image/uogyn.png" alt="CCTargetedTouchDelegate"/> </p>

<pre><code>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);}
};
</code></pre>

<p><strong>CCTouchDelegate 类</strong></p>

<p> <img src="/image/alkEj.png" alt="CCTouchDelegate"/> </p>

<pre><code>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);}

};
</code></pre>

<p><strong>只是出于好奇:-</strong></p>

<p>因为我已经从 CCTargetedTouchDelegate 继承
即我的类语法是
    类 DragSprite:公共(public) cocos2d::CCSprite,公共(public) cocos2d::CCTargetedTouchDelegate
    {
    }</p>

<p>我还需要在错误生成代码中使用范围解析运算符吗
即</p>

<pre><code>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)
{

}
</code></pre>

<p>我不能像下面这样写吗?会有什么不同吗?</p>

<pre><code>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)
{

}
</code></pre>

<p>还需要调用所需的代表吗?</p>

<p>因为我可以看到错误消失了,如果我这样做的话。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>如果要覆盖 DragSprite 类中的方法,则需要使用该类的前缀。</p>

<p>而不是重新实现 cocos2d 命名空间/类中的类:</p>

<pre><code>void cocos2d::CCTargetedTouchDel....
</code></pre>

<p>您需要像这样在自己的类中实现方法:</p>

<pre><code>void DragSprite::CCTargetedTouchDel....
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于c&#43;&#43; - cocos2dx中如何使用CCTargetedTouchDelegate?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/23441824/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/23441824/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: c&#43;&#43; - cocos2dx中如何使用CCTargetedTouchDelegate?