String array error

Hey guys, sorry for this topic, but I’m trying to create a system to randomly select an image to generate a sprite, but I can’t figure out how to do that here is my code:

Color::Color( )
{
string spriteC[2] = {“a.png”,“b.png”};
visibleSize = Director::getInstance( )->getVisibleSize( );
origin = Director::getInstance( )->getVisibleOrigin( );
}
void Color::SpawnColor(cocos2d::Layer *layer)
{
int cor = rand()%(2-0)+0;
auto colorObstacle = Sprite::create(spriteC[cor]);
auto colorObstacleBody = PhysicsBody::createBox(colorObstacle->getContentSize());
colorObstacleBody->setDynamic(true);
colorObstacle->setPhysicsBody(colorObstacleBody);
colorObstacle->setPosition(origin.x+visibleSize.width,origin.y+visibleSize.height);
layer->addChild(colorObstacle,0);
}

But it’s not working, what would I do to make this work? Thanks in advance

The first thing I notice is spriteC is declared in the Color constructor as a local variable, so the SpawnColor function won’t be able to access it.

Oh yeah, I didn’t realize that thanks, but this isn’t the biggest trouble, cause I can’t figure out how to declare an string array into cocos :confused:

string spriteC[2] = {“a.png”,“b.png”};

Color::Color( )
{
visibleSize = Director::getInstance( )->getVisibleSize( );
origin = Director::getInstance( )->getVisibleOrigin( );
}
void Color::SpawnColor(cocos2d::Layer *layer)
{
int cor = rand()%(2-0)+0;
auto colorObstacle = Sprite::create(spriteC[cor]);
auto colorObstacleBody = PhysicsBody::createBox(colorObstacle->getContentSize());
colorObstacleBody->setDynamic(true);
colorObstacle->setPhysicsBody(colorObstacleBody);
colorObstacle->setPosition(origin.x+visibleSize.width,origin.y+visibleSize.height);
layer->addChild(colorObstacle,0);
}

what about now?

I found the way to create strings in cocos using cocos2d::__String *Example = cocos2s::__String::create()

But how to create an array and set it’s values(yeah I’m really new at c++)!!!

Thanks again

If all your Color objects will have the same list of strings, you probably want to use a static const String* variable. The way I would do it is declare in the Color header file,

class Color {
private:
...
static const String* spriteC[2];
...
};

Then in the source file you would use,

const String* Color::spriteC = { String::create("a.png"), String::create("b.png") };

Hopefully that works. I’m a little rusty on arrays in C++ and I’m testing this code in cocos v2. C++ is a little daunting at the start, especially if you don’t learn the basics first, but I like it :smiley:

Why don’t you use the std string and vector?

Yeah, std string is what I use. Suppose I should have said that, but he mentioned cocos2d::String so I went with it.

I think a vector would be overkill, because I imagine he won’t be modifying it at runtime. Perhaps a std list would be better for his purposes; definitely a vector if he wants to dynamically change the list.

Thanks for the suggestions guys, well first of all grimfate, you where almost right about what I was trying to do, but the declaration String* was deprecated, I had to use __String *ArrayName, it worked, but now, I’m still stuck cause I’m trying to call one of those array elements but I’m always getting an error, here is the code now:

Color::Color( )
{
const cocos2d::__String *stringTeste[2] = {__String::create(“a.png”),__String::create(“b.png”)};
cor = rand()%(2-0)+0;

}
void Color::SpawnColor(cocos2d::Layer *layer)
{
auto d = {“a.png”,“b.png”};

auto colorObstacle = Sprite::create(stringTeste[cor]);

}

Thanks again!!!

And gejza, how grimfate said, I won’t be modifying it at runtime, thanks too!!!

You’ve declared stringTeste as a local variable again.

I created a new topic, my bad

Overkill? Cocos2d-x is more MB. The difference is not really sensable. And you have direct access to any item, you don’t need to iterate through. :wink:
http://info.prelert.com/blog/stl-container-memory-usage

Sorry, grimmfate i thouht i answered for the questioner, not you. It was just a misunderstanding.

I just assumed an immutable list would be more efficient than a mutable vector, as the latter would have functions unnecessary for a static list, but I did look online and found that you can’t reference by index with a list, which makes it more annoying.

Surely there is a type of object that simply contains a immutable list of objects, a function for getting the size of the list and a function to access the objects by index.

And yeah, I am probably being a little too picky about efficiency when the array will be rather small in size haha