如何创建精灵数组?

时间:2014-05-07 11:50:32

标签: c++ cocos2d-x

我是最近3天的cocos2dx的新手我正在尝试创建精灵数组假设让我有1.png,2.png,3.png,4.png精灵,我需要存储在一个数组中以这种方式,虽然addchild()精灵应该随机出现在屏幕上,请让我知道代码?

std::vector<std::string> _spriteNames = {"0.png", "1.png", "2.png", "3.png", "4.png"};
for (int i=0;i < _spriteNames.size(); i++) {
  CCSprite* foo = cocos2d::CCSprite::create(_spriteNames.at(i));  //here i am getting error as no matching of the function??
  int random = rand() % 5;
  foo->setPosition(CCPoint((60 * random), (50 * random)));
  _sprites.push_back(foo); 
  addChild(foo, 1); 
}

1 个答案:

答案 0 :(得分:0)

这对我有用。另外,我回答了这个问题并给了你这个代码。为什么您没有回复此处的原始帖子:https://stackoverflow.com/questions/23491659/how-to-create-random-sprites/23499031#23499031

std::vector<CCSprite*> _sprites;
std::vector<std::string> _spriteNames = {"0.png", "1.png", "2.png", "3.png", "4.png"};

for (int i=0;i < _spriteNames.size(); i++)
{
    CCSprite* foo = cocos2d::CCSprite::create(_spriteNames.at(i));//here i am getting error as no matching of the function??

    int random = rand() % 5;

    foo->setPosition(CCPoint((60 * random), (50 * random)));

    _sprites.push_back(foo);

    addChild(foo, 1);
}

以下是截图:http://imgur.com/TcPDows