Basic Cpp Array Object Problem

Hi all,

I hope you guys can help me, I’ve got a basic problem with populating an array with objects.
I created an array in the header file like this:
CCArray * animations;

and then I tried to populate it with data:
@
AnimationClass * anim;
anim = new AnimationClass;
anim~~>numOfFrames = numFrames;
anim~~>numOfRounds = numOfRounds;
anim~~>tPerFrame = tPerFrame;
anim~~>filename = str;
this~~>animations~~>addObject(anim);@

The problem I have is that the object is overwritten. Each object references to the same anim-object.
How do I fix that?

I thought that you create an object by
ObjectClass * obj = ObjectClass::create();
or
ObjectClass * obj = new ObjectClass();
and then it should create a new instance which is unique, or am I missing something?

Thanks in advance!

You must create a new object with “ObjectClass * obj = ObjectClass::create()”. This method return a pointer to the object.
Are you using some kind of iteration? I think that you are adding the same pointer that you overwrites in each iteration and then you have an array of pointers to the last object that you create.
Please, paste the full code.

I don’t think that this is the actual problem, it’s more that the object is released after adding it to the array.

I had to create a new class which contains some int values and a const char * string, like this:
animationclass.h
@ #include “cocos2d.h”

USING_NS_CC;

class AnimationClass: public CCObject {
private:
public:
const char * filename;
int numOfFrames;
int tPerFrame;
int numOfRounds;
int actualFrame;
int actualRound;
static AnimationClass * create();
};
@

animationclass.c
@ #include “AnimationClass.h”

AnimationClass * AnimationClass::create()
{
AnimationClass * pAnim = new AnimationClass();
pAnim~~>filename = “”;
pAnim~~>numOfFrames = 0;
return pAnim;
} @

When I’m instantiating the object in the function addanimation:
@ AnimationClass * anim;
anim = AnimationClass::create();
anim~~>numOfFrames = numFrames;
anim~~>numOfRounds = numOfRounds;
anim~~>tPerFrame = tPerFrame;
anim~~>filename = str;
anim~~>autorelease;
this~~>animations->addObject(anim);@

The object is overwritten each time the function is called, but what I want is to store multiple objects of this type in the array.

Do you mean:

AnimationClass * anim;
anim = AnimationClass::create();
anim->numOfFrames = numFrames;
anim->numOfRounds = numOfRounds;
anim->tPerFrame = tPerFrame;
anim->filename = str;
anim->autorelease();
this->animations->addObject(anim);

then modify anim and add it to animations ?

for (int i = 0; i < 10; i++)
{
    anim = (AnimationClass*) anim->copy();
    // blaaaa
    animations->addObject(anim);

}

So copy is the magic? ^^

copy is not supported in Cocos2d-x.
Is there another possibility to fill an array with objects?

See no problem in how you fill your array. Maybe you access objects not correctly?

The access is completely fine:
@
AnimationClass * anim = static_cast<AnimationClass*>(this~~>animations~~>objectAtIndex(animationNumber));
printf(“anim: %s”, anim->filename);
@

I’m sure, that the created objects are not allocated correctly.
How can I implement copy() ?

You don’t need to implement copy(). I create my objects and populate arrays with them fine. There must be a problem in your code. I see no problem in what you posted so it’s somewhere else.

PS Okay another try: how did you create array you store in your class? if you created it as an autorelease object did you retain it afterwards?

Yea, with sprites it works fine, but since I created my own subclass of CCObject it’s not working.
Maybe the subclass is missing something?

I’m gonna answer your question in a few minutes, when I’m on my other PC.

Okay, I worked it out.

It was a problem with stringstream, which I used to create the tags for the object.
What I did was the following:
@ tempString.str(“”);
tempString << textureName << “t_”;
this~~>addAnimation.c_str, 9, 0.08, 1);
tempString.str;
tempString << textureName << “r_”;
this~~>addAnimation(tempString.str().c_str(), 9, 0.08, 1);@
So the string which I generated was still changeable and not hard stored, by changing tempString it changed the string also in the array objects.
Is there a way to copy the string before passing it to the function, so that it’s constant?

The function addAnimation is like this:
void AnimatedObject::addAnimation(const char * str, int numFrames, float tPerFrame, int numOfRounds) { AnimationClass * anim = new AnimationClass();//AnimationClass::create(); anim->numOfFrames = numFrames; anim->numOfRounds = numOfRounds; anim->tPerFrame = tPerFrame; anim->filename = str; this->animations->addObject(anim); printf("name: %s\r\n",anim->filename); }

Simpliest way I see is to store std::string in objects, not const char**. Passed const char** will be copied when assigned to string.

Yea that solved it. But I got another error with the array, I opened a new thread, so it’s easier to follow.