扩展CCSprite

时间:2014-05-21 13:59:52

标签: c++ xcode opengl-es-2.0 cocos2d-x

想象一下一个旋转,它会使纹理调色板从屏幕的一侧滚动到另一侧。我们将随机选择给定矩形内的纹理来模拟图像旋转木马的功能。

        //Header file of the Texture Embedded. This is Fabric.h

         class Fabric: public WhirligigNetwork {
         .....
         ........
          void initFabric(void);

          public: 

         static Fabric * create();

我静态初始化主对象:

     //In Fabric.cpp

     //Fabric create function.

    Fabric * Fabric::create() {

   Fabric * fabric = new Fabric();
    if (fabric && fabric-> initWithSpriteFrameName("fabric.png")) {
    fabric->autorelease();
    fabric->initObstacle();
    return fabric;
      }
     CC_SAFE_DELETE(fabric);
     return NULL;

       }

不幸的是,当我尝试扩展'Fabric'(这是CCSprite类的掩码)并进行编译时,Xcode很难搞清楚Fabric真正的含义。 :混淆

  /*So let's say that we're implementing a randomized selection of fabric elements that are           
      assigned to a whirligig of Sprite 'containers'.*/

class WhirligigNetwork : public Sprite {

                  .................
           .......................
       //Xcode does not know type name (Fabric) - the override is useless.

           //An Array of Fabrics!
          cocos2d::Vector<cocos2d::Sprite *> _fabrics;

            void initFabric(Fabric * fabrics); /* doesn't run */

      /* If I play around with inline helper methods to query for a countable set of widths*/

         inline float getWideness() {

         //then I order and count the elements of my Vector<T>
         int count = _fabrics.size();

           //Default
             int wideness = 0;

    //Deal with the heap.
    class Fabric * fabrics;
    for (int i = 0; i < count; i++) {
        fabric = (class Fabric *) _fabics.at(i);

        // set-increment wideness
        wideness += fabric->getWideness();
    }
    return wideness;
}

会员访问不完整类型'class Fabric'...有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您有循环依赖

在定义Fabric之前,您需要定义班级WhirligigNetwork,但由于Fabric需要首先定义WhirligigNetwork,因此您无法这样做。< / p>

简单的解决方案是在Fabric定义之前声明WhirligigNetwork,然后将成员函数实现放在一个单独的源文件中,您可以安全地包含它们头文件的顺序正确。

所以在WhirligigNetwork的标题文件中,你有例如。

#ifndef WHIRLIGIGNETWORK_H
#define WHIRLIGIGNETWORK_H

// Declare the class Fabric
class Fabric;

// Define the class WhirligigNetwork
class WhirligigNetwork : public Sprite
{
private:
    cocos2d::Vector<cocos2d::Sprite *> _fabrics;

    ...

public:
    ...

    float getWideness();

    ...
};

#endif

WhirligigNetwork的源文件中:

#include "whirligignetwork.h"
#include "fabric.h"

// Can use `Fabric` freely in here

...

float WhirligigNetwork::getWideness()
{
    ...
}