gpt4 book ai didi

c++ - 扩展 CCSprite

转载 作者:行者123 更新时间:2023-11-28 06:52:02 27 4
gpt4 key购买 nike

想象一个变种人,它使纹理调色板从屏幕的一侧滚动到另一侧。我们将随机选择给定矩形内的纹理来模拟图像轮播的功能。

        //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”...有什么建议吗?

最佳答案

你有一个循环依赖

在定义 WhirligigNetwork 之前,您需要先定义类 Fabric,但您不能这样做,因为 Fabric 需要 WhirligigNetwork 首先定义。

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

所以在 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()
{
...
}

关于c++ - 扩展 CCSprite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23785451/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com