Naveed Abbas
Naveed Abbas

Reputation: 1187

Adding and accessing CCSprites

I'm having trouble inserting multiple children of same sprite and accessing it (or setting positions for them on runtime). Kindly advise any suitable method preferably point out my mistake. Here is my approach.

//In the Init Method...

//int i is defined in the start.

    for (i = 1; i < 4; i++)

    {

        hurdle = [CCSprite spriteWithFile:@"hurdle1.png"];

        [self addChild:hurdle z:i tag:i];

        hurdle.position = CGPointMake(150 * i, 0);

    }

It spreads all the sprites on the canvas. then in some "UPDATE Function" I'm calling this.

hurdle.position = CGPointMake(hurdle.position.x - 5, 10);

if (hurdle.position.x <= -5) {
    hurdle.position = ccp(480, 10);
}

It works but as expected only one instance moves horizontally. I want all the instances to be moved so I am trying to use this....

for (i = 1; i < 4; i++){

   [hurdle getChildByTag:i].position = CGPointMake(hurdle.position.x - 5, 10);

//OR
   [hurdle getChildByTag:i].position = CGPointMake([hurdle getChildByTag:i].position.x - 5, 10);

}

I've tried getting LOGs on various places and realized that getChildByTag doesn't work the way I'm trying to use it.

Upvotes: 1

Views: 1173

Answers (2)

Tone
Tone

Reputation: 322

Something that I do often is group objects of like kind that I want to act on in a similar way by adding them to a CCNode and add that CCNode to the layer.

I would create a class that derives from CCNode

Then I can put all my logic in that node and access then via [self children]

for(CCSprite *hurdle in [self children]) {
    // Do what you need to do
}

Upvotes: 0

Jesse Black
Jesse Black

Reputation: 7986

The problem is in the last block of code. You should make a local reference to each CCSprite within your for loop.

Since you added the sprites to self, you will retrieve them as children of self

for (i = 1; i < 4; i++){
   CCSprite * enumHurdle = [self getChildByTag:i];
   enumHurdle.position = CGPointMake(enumHurdle.position.x - 5, 10);
}

Be careful if you create any other sprites this way in the same scene. It is bad design to give any two sprites the same tag.

EDIT about avoiding duplicate tags.

If you know how many sprites you will have. Use an enum of tags and refer to the sprites by name.

If not, knowing how many groups and putting a limit on the size of groups could make it managable.

ie say you have 3 parts of code where you are generating sprites like this. You can include an enum in your .m (under @implementation line) and put the limits there

// Choose names that describe the groups of sprites
enum { kGroupOne = 0, // limiting the size of each group to 100 
    kGroupTwo = 100, // (besides the last group, but that is not important)
    kGroupThree = 200, 
};

Then when you create each group

// group 1
for (i = kGroupOne; i < 4; i++){
   // set up code here
}

// group 2 
// g2_size is made up, insert whatever you want
for (i = kGroupTwo; i < g2_size; i++) {
   // set up code here
}
.
.
.

Then to retrieve in groups

for (i = kGroupOne; i < 4; i++){
   CCSprite * enumHurdle = [self getChildByTag:i];
   enumHurdle.position = CGPointMake(enumHurdle.position.x - 5, 10);
}
.
.
.

Hopefully that sparks your creativity. Now have some fun.

Upvotes: 2

Related Questions