Nested for loop array initialization - objective-c

I want to initialize a 2-d array gameBoard and display the result on screen. Will the following nested for loops work? I'm having trouble displaying it on the screen so I can't tell if this is working correctly or not.
for (NSInteger x = 0; x <= 2; x++)
{
for (NSInteger y = 0; y <=2; y++)
{
gameBoard [x][y] = 0;
NSLog(#"%ld"), gameBoard [x][y];
}
}

Your NSLog line is wrong, but other than that you're ok (assuming your array is appropriately sized, that is). Change the log line to:
NSLog(#"%ld", gameBoard[x][y]);
to get some actual output. Now that I look again, I think your example won't even compile cleanly the way it is.

Related

How to use a double array in objective c?

I'm trying to use a double array to make a ccp. So I'm looping through my array called grid1x for the X axis and grid1y for the Y axis like so:
for (int i = 0; i < [grid1x count]; i++)
{
p = ccp(x*sw/768,sh-y*sh/1024);
grab = [[Grab alloc] initWithPosition:p];
[grabs addObject:grab];
[batch1 addChild:grab z:5];
[grab release];
}
The line
p = ccp(x*sw/768,sh-y*sh/1024);
Works perfect when I provide x and y individually, but I cannot figure out how to use them at the index position of the array. I do know to use [grid1x objectAtIndex:i]; but cannot get it to work either.

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];
}

Checking to see if two positions are equal

Right now I have a NSMutableArray that holds 3 sprite objects. I need to be able to see if another sprite not in the Array shares the same position as any of the sprites in the array.
I tried doing this:
CCSprite *sect;
if (i > maxHealth) {
for (int j = 0; j < i; j++) {
sect = [tail objectAtIndex:j];
}
if (CGRectContainsPoint(sect.boundingBox, playerPos)) {
NSLog(#"On top");
return;
}
But it does't work. I think it's trying to see if it intersects all of them at once.
Your if is outside the for loop. It is only going to test one object; the last one accessed in the loop.

Reusing a variable name as for loop index doesn't produce the error I expect

Am I allowed to re-use the same variable name in different for loops in Objective-C? For example:
// This doesn't give me an error but I feel like it should:
for(int i = 0; i < 10; i++){
//do something
}
for (int i = 0; i < 5; i++){ // I'm using "i" again. Is this allowed?
//do something else
}
This compiles and seems to run fine, but I just want to make sure that this is legal and allowed without causing some sort of complication in my program. I'm newish to ObjC, but in Java I normally would get errors from this.
That should be fine. The scope of i in the snippet you show is limited to each of the for loops, so there's no conflict. If you instead do it like this:
int i;
for (i = 0; i < 2; i++) {
//...
}
int i;
for (i = 5; i < 10; i++) {
//...
}
then you'll have a problem because you're declaring i twice in the same scope.
This is perfectly fine. Because you doesn't initialize the integer outside of the for loop. If you had two times this:
int i;
for (i = 0; ...)
then you would of course get a compile error, because you can't define two variables with the same name in the same block.

Shuffling an array of NSNumbers & converting back to ints

I have an NSMutableArray urlArray of size n, I want to randomly choose 4 of these URLs from the total number of elements in the array.
However I don't want to shuffle urlArray directly, I'd prefer to make an "indexArray" [0 ... (n-1)] & shuffle these, & then use the first 4 elements of the shuffled indexArray to decide which elements I choose from urlArray.
First off I created the indexArray as follows:
for (int i = 0; i < numberOfStems; i++) {
[indexArray addObject:[NSNumber numberWithInteger:i]];
}
This allowed me to shuffle my indexArray, so far so good. Because I used the
[NSNumber numberWithInteger:i] method, the elements in the shuffled indexArray are NSNumbers.
Is there a way to convert the NSNumber objects in indexArray into ints?
I attempted to use the intValue function but this didn't appear to be what I needed.
I also tried creating a c style array but that wasn't so successful either - I'd like to stick with objective-c syntax if possible.
Any ideas? any hints appreciated :)
Why don't you just create a normal c array, shuffle that and then use the first four integers in the array as the for random index?
something like
int* index = malloc(numberOfStems*sizeof(int));
for (int i = 0; i < numberOfStems; ++i)
{
index[i] = i;
}
for (int i = numberOfStems - 1; i > 0; --i)
{
int randomIndex = arc4random() % i;
int tmp = index[i];
index[i] = index[randomIndex];
index[randomIndex] = tmp;
}
now use index to access the URL's
EDITED: updated algorithm (although not really related to OP question)
For a temporary array that stores only integers and gets thrown away after a relatively short task I would definitely prefer a C-style array: this would avoid a great deal of overhead, and is also simple to read.
int *array = (int*)malloc(sizeof(int)*numberOfStems);
for (int i = 0 ; i != numberOfStems ; i++) {
array[i] = i;
}
// Do the shuffle
// Pick first four, and do whatever you need to do
// ...
// Now that you are done with the array, do not forget to free it:
free(array);