我正在创建一系列精灵,它们应该随机出现,但它发生了
I am creating array of sprites and they should appare randomly but its happening?
我正在尝试创建包含5个精灵的数组精灵,如0.png
, 1.png
, 2.png
, 3.png
, 4.png
。
我希望它们随机出现在屏幕上。
下面是我的代码,但它不工作,任何帮助?
std::vector <CCSprite*> _sprites;
_sprites.reserve(10);
int spritearray[5] = { 0.png,1.png,2.png,3.png,4.png }; // I AM GETTING ERROR HERE?
int i;
for(i=0;i<5;i++)
{
CCSprite* foo = new cocos2d::CCSprite();
int index = rand() % 5;
// foo->initWithFile(index);
foo->setPosition(ccp(60,50*i));
_sprites.push_back(foo); //store our sprites to do other stuffs later
this->addChild(foo,1);
}
你的"逻辑"目前是好的,这是你有问题的实现。
如果您检查initWithFile
函数,您会看到它接受文件名作为字符串。
所以你需要创建一个字符串数组(文件名),而不是整数数组。然后使用随机索引作为该文件名数组的索引,并将其作为参数传递给initWithFile
函数。
好的,您使用的是我之前提供给您的相同代码:
std::vector <CCSprite*> _sprites;
_sprites.reserve(10);
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));
int random = rand() % 5;
foo->setPosition(CCPoint((60 * random), (50 * random)));
_sprites.push_back(foo); // <- store your sprites to do stuff to them later.
addChild(foo, 1); //<-- this is adding the child.
}
相关文章:
- 此测试()中发生了什么意外过程?为什么总是覆盖 ch[0 1 2..]?
- 这C++代码中发生了什么C++(指数函数)
- 哪种方式更快?究竟发生了什么,我们没有看到什么?
- 我正在将一个 std::string 传递给一个 boost 函数,该函数对该类型进行常量引用,但该值发生了变化
- c++问题:给一个变量赋值后,另一个变量发生了变化
- 从"LLONG_MAX 秒"构造 std::chrono::毫秒变量时发生了什么?
- 这个片段中关于 n 在 pc[i] 中的表示发生了什么
- istringstream,num1 和 num2 在这里发生了什么?
- 究竟发生了什么,我们需要在 c++ 中双重调度/访客
- libstdc++的make_shared布局在gcc 4.x和gcc 6.x之间是否发生了变化?
- 我是否能够确定在部署一个程序后发生了什么,我在数组末尾写入?
- 为什么我的 int 在 C++ 程序中间发生了变化?
- 这个阶乘程序内部发生了什么?
- C++ - *(int**) 地址?这里发生了什么?
- C++用用户输入在循环中填充 char 数组:输入整个字符串时到底发生了什么?
- 在使用VS2010构建ACE和TAO(CORBA)时发生了许多错误
- 为什么在波纹管程序中发生了从字符串常量到'char*'的警告已弃用的转换
- 让对象知道它创建的 show 函数中发生了什么
- 这里的矢量数组发生了什么?
- 我正在创建一系列精灵,它们应该随机出现,但它发生了