Populate NSMutableArray with CCSprites via for loop - objective-c

Hey guys, using obj-c and the cocos2d framework;
How would I populate an NSMutableArray with CCSprite's with a for-loop to reduce code.
I have +100 sprites/images that need to go into this array, so a for-loop is necessary.
This is my current code:
_backgrounds = [[NSMutableArray alloc]initWithCapacity:31];
for (int i = 31; i > 1; i--){
[_backgrounds addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:#"background_%d.png", i]]];
}
Im looking for something along the lines of this: (Although this code doesnt work),
for (int i = 31; i > 1; i--){
[backgrounds addObject:[CCSprite spriteWithFile:#"background%d.png", i]];
}
Thanks in advanced,
Oliver.

Chances are your shared CCSpriteFrameCache does not contain any values. You must populate it prior to accessing anything from it.

Related

Objective-C/Cocos2D - Display different Sprites with Animation on the same positon on the Screen

Hello i have a big question and i don't find a way to solve my problem till now.
I work on simple game with objective-c and cocos2D.
I have 3 different objects (sprites with animation) and four fixed position on the screen.
Alternately with a interval i want to display the different objects on the positions.
I wanted to do it with a double for() to position the objects. And in the for for i want to create a multidimensional array with all the objects.
And then i want to create a Method where i have access to the time-interval and the frequency of the different objects.
Do you think i can solve my problem with this solution or do you know a better way…??
It would be great if anybody could help me.
Thanky you
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"myPlist.plist"];
myArray = [[CCArray alloc] init];
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= 2; j++) {
Figure *figure = [Figure spriteWithSpriteFrameName:#"a0001.png"];
figure.position = ccp(j * figure.contentSize.width + 50, i * figure.contentSize.height + 50);
[myArray addObject: figure];
[self addChild:figure z:1];
}
}
I do not know how you structured your plist, but I imagine that you have renamed the images in this way:
a0001.png
a0002.png
a0003.png
why you don't try to make a more simple for loop instead of a "for inside a for" for place only 3 images at your view and manage your file like this:
for (int i = 1; i < 3; i++) {
Figure *figure = [Figure spriteWithSpriteFrameName:[NSString stringWithFormat:#"a000%i.png", i]]
figure.position = //add something that fit with your layout
[myArray addObject: figure];
[self addChild:figure z:1];
}

How to clear a NSTableView's data source

I am trying to make a method in which the data source for my NATableView is cleared, but I cannot figure out how to do this anywhere.
Here is the code I am using to send an array called final to my table view.
// I want to clear it here before filling it again.
for (int i = 0; i < [final count]; i++) {
[myArrayController addObject: [NSMutableDictionary dictionaryWithObjectsAndKeys:final[i], #"File Name", nil]];
}
Any help would be great!
If you are using an NSMutableArray and myArrayController is your mutable array you want to call
[myArrayController removeAllObjects];
before you enter the for-loop.

adding a string followed by an int to an array

I am very new to Objective-C with Cocoa, and I need help.
I have a for statement in which I loop i from 1 to 18, and I would like to add an object to an NSMutableArray in this loop. Right now I have:
chapterList = [[NSMutableArray alloc] initWithCapacity:18];
for (int i = 1; i<19; i++)
{
[chapterList addObject:#"Chapter"+ i];
}
I would like it to add the objects, chapter 1, chapter 2, chapter 3... , chapter 18. I have no idea how to do this, or even if it is possible. Is there a better way? Please Help
Thanks in advance,
chapterList = [[NSMutableArray alloc] initWithCapacity:18];
for (int i = 1; i<19; i++)
{
[chapterList addObject:[NSString stringWithFormat:#"Chapter %d",i]];
}
good luck
Try:
[chapterList addObject:[NSString stringWithFormat:#"Chapter %d", i]];
In Objective-C/Cocoa you can't append to a string using the + operator. You either have to use things like stringWithFormat: to build the complete string that you want, or things like stringByAppendingString: to append data to an existing string. The NSString reference might be a useful place to start.
If you're wanting strings that merely say Chapter 1, Chapter 2, you can just do this:
chapterList = [[NSMutableArray alloc] initWithCapacity:18];
for (int i = 1; i<19; i++) {
[chapterList addObject:[NSString stringWithFormat:#"Chapter %d",i]];
}
And don't forget to release the array when you're done, as you're calling alloc on it.

make CAlayers using for loop

sorry for the simple question but how do you make a CAlayers using a for loop?
I have this now but what is the proper way of doing this?
for (int n = 1; n <= 6; n++) {
NSString *theCloud = [NSString stringWithFormat:#"cloudImage%d",n];
NSString *theCloudLayer = [NSString stringWithFormat:#"cloudLayer%d",n];
CALayer *theCloudLayer = theCloud.layer;
}
any help is appreciated.
Use an NSArray or NSMutableArray, not a bunch of variables with numbers at the end of their names (also known as a Poor Man's Array).
So that would be something like:
NSArray *cloudImages; // Use this to store your objects currently in the cloudLayerN variables
NSMutableArray *cloudLayers = [NSMutableArray array];
for (id cloud in cloudImages) {
[cloudLayers addObject:[cloud layer]];
}

change speed of an array loop? Objective-C

I have this code
NSArray *food = [NSArray arrayWithObjects:#"Apples:",#"bacon",#"corn",#"donuts",#"elfs",#"fidge",nil];
for(int i = 0; i<6; i++){
NSLog(#"item at index %i is %#",i,[food objectAtIndex:i]);
}
and right now they are all printed to the console instantly. How can I make a variable to decrease or increase the speed they are logged?
I'm new at objective-C so thanks a lot for your help! :)
NSArray *food = [NSArray arrayWithObjects:#"Apples:",#"bacon",#"corn",#"donuts",#"elfs",#"fidge",nil];
// the number of seconds to wait between printing each item
double secondsToSleep = 1.0;
for(int i = 0; i<6; i++){
[NSThread sleepForTimeInterval:secondsToSleep];
NSLog(#"item at index %i is %#",i,[food objectAtIndex:i]);
}
There's a sleepForTimeInterval: method on NSThread that might do what you're looking for. The documentation is here.
Edit: Sorry, for Objective-C newbies, you would just type something like this:
[NSThread sleepForTimeInterval:0.01];
See the sleep() function.