C ++ Невозможно получить значение глобальной переменной в cocos2d-x

0

У меня проблема при работе с CCArray в cocos2d-x. Я объявляю CCARray * arrCs, сверху, в функции init() я устанавливал значение для arrCs, но после того, как я получил значение от него в событии touch, у него ничего нет. Пожалуйста, помогите мне, спасибо всем.

В 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++) {
      }
}
Теги:
cocos2d-x
cocos2d-x-3.0

1 ответ

2

CCArray - объект автоопределения (см. Метод создания) и уничтожается при выходе из метода init. Пожалуйста, зайдите в свой массив (arrCs-> сохранить()) в методе init, и вы получите ожидаемый результат в onTouchesEnded.

С уважением, Лоран

Ещё вопросы

Сообщество Overcoder
Наверх
Меню