0

在 cocos2d-x 中使用 CCArray 时遇到问题。我在顶部声明 CCArray *arrCs,在 init() 函数中我为 arrCs 设置了值,但是在我在触摸事件中从它获取值之后,它什么都没有。请帮助我,谢谢大家。

在 MainGame.cpp

int radius= 32;
float scaleImage= 0.5;
int nItem= 60;
float pMargin= 5;

int limitTime= 70;
float timeWaiting= 8;
bool opTouch= true;
int lastIndex= -1;
Size visibleSize;

CCArray *arrCs;

bool MainGame::init(){
    arrCs= CCArray::create();

    int xx= pMargin+ radius;
    int yy= pMargin+ radius;

    int inLabel= 0;
    for (int i=0; i<nItem; i++) {
        inLabel= i;
        if (i>((nItem/2)-1)) {
            inLabel= i-(nItem/2);
        }

        CCString *iconName= CCString::createWithFormat("ricon_%i.png", inLabel);
        Sprite *cs= CCSprite::create(iconName->getCString());
        cs->setPosition(Point(xx, yy));
        cs->setTag(-1);
        cs->setScale(scaleImage);    

/*****SET VALUE FOR arrCs ******************/
        arrCs->addObject(cs);

        this->addChild(cs, (1+ rand()%3));

        xx= xx+ (radius*2)+ pMargin;
        if (xx+ (radius/2)> visibleSize.width) {
            xx= radius + pMargin;
            yy= yy+(radius*2)+ pMargin;
        }
    }
}




void MainGame::onTouchesEnded(const std::vector<Touch*>& touches, Event* event){
 /******** arrCs has nothing ************/
      for (int i=0; i< arrCs->count(); i++) {
      }
}
4

1 回答 1

2

CCArray 是一个自动释放对象(参见 create 方法),在离开 init 方法时被销毁。请在您的 init 方法中调用您的数组 (arrCs->retain()) 上的保留,您应该在 onTouchesEnded 中获得预期的结果。

问候,劳伦特

于 2014-02-19T23:10:23.083 回答