Checking to see if two positions are equal - objective-c

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.

Related

Creating a lot of buttons for levels in Cocos2d using a for loop

I'm trying to make a couple hundred levels for a game I'm developing, so obviously I want to use a for loop and not create each button individually.
I do all of this fine, but the code I'm currently using makes it so that the call block for each button is the same. Obviously if I'm going to have the different level buttons go to different levels, the call block must be different for each one.
In order to differentiate which level I'm on, I call [[LevelManager sharedInstance] nextLevel] the number of times corresponding to the level number. I attempted to change this number by getting the touch location of the user touching the button, getting a row/column number, then running the above code a certain number of times. However, it was not updating the touch position before running the whole call block after touching the button, which obviously makes my code not work.
Is there a way to update the touch position manually, before the call block finishes? Is there a way to somehow store every button in an array and establish a different call block for each one? I'm not sure what the best approach is to fixing my problem. Thanks and my code is below.
Called upon initialization
for (int l = 0; l < NUMBER_OF_WORLDS; l++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 5; k++) {
NSString *tempTitle = [NSString stringWithFormat:#"%i",(k+1)+(l*5)];
CCButton *tempButton;
tempButton = [CCButton buttonWithTitle:tempTitle]
tempButton.block = ^(id sender) {
int row = floorf(touchLocation.y/(screenBounds.size.height/6)) + 1;
int column = floorf(touchLocation.x/(screenBounds.size.width/4)) + 1;
int buttonIndex = row*column;
for (int m = 1; m < buttonIndex; m++) {
[[LevelManager sharedInstance] nextLevel];
}
CCScene *gameplayScene = [CCBReader loadAsScene:#"LevelScene"];
[[CCDirector sharedDirector] replaceScene:gameplayScene];
levelNumber = i;
};
tempButton.position = ccp((screenBounds.size.width/4)*j + levelImagePlaceholder.contentSize.width*tempButton.scale, screenBounds.size.height/6*k + 50 + l*screenBounds.size.height);
[self addChild:tempButton];
i++;
}
}
}
Method for getting touch position
-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
CCLOG(#"touch ran");
touchLocation = [touch locationInNode:self];
}
You already have the row/column available. They are j and k. Those variables can be referenced in your button's "on pressed" block.

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

Mutable Array and Sprites

I'm making a "snake game" like player and I have the body moving fine so that it adds a block in the previous position each time it moves up to 4 then it removes the last one... etc. I'm adding each block to a NSMutableArray (each block is a sprite and I add the sprite to the array) and I was wondering how to get the position of one of the sprites in the array. I need this so I can check to see if the "head" is trying to move onto itself.
P.S. I'm using cocos2d for iPhone.
When I say position I mean the coordinates, not the index position in the array.
[tail insertObject:block atIndex:i];
[self addChild:[tail objectAtIndex:i]];
i +=1;
CCSprite *sect;
for (int j = 0; j >= i; j++) {
sect = [tail objectAtIndex:j];
}
if (i > maxHealth) {
[self removeChild:[tail objectAtIndex:i-maxHealth-1] cleanup:YES];
id object = [tail objectAtIndex:i-maxHealth-1];
[tail removeObject:object];
}
When the scene is started i is set to 0 and the max health equals 3.
Get the position of a node in an array? Like
// Get the node from the array
CCNode *node = (CCNode*)[myArray objectAtIndex:0];
// Retrieve the position
CGPoint nodePosition = node.position;

Nested for loop array initialization

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.

How to cycle through an array of CGPoints

I have created an array of 16 CGpoints representing 16 positions on a game board. This is how i set up the array CGPoint cgpointarray[16]; I would like to create a for loop to cycle through each item in the array and check if the touch is within x distance of a position (i have the position as a CGPoint. I don't have much experiance with xcode or objective c. I know the python equivalent would be
for (i in cgpointarray){
//Stuff to do
}
How would i accomplish this? Thanks
for (int i = 0; i < 16; i++){
CGPoint p = cgpointarray[i];
//do something
}
Or if you want to use the NSArray Class:
NSMutableArray *points = [NSMutableArray array];
[points addObject:[ NSValue valueWithCGPoint:CGPointMake(1,2)]];
for(NSValue *v in points) {
CGPoint p = v.CGPointValue;
//do something
}
( not tested in XCode )
This should do it:
for (NSUInteger i=0; i < sizeof(cgpointarray)/sizeof(CGPoint); i++) {
CGPoint point = cgpointarray[i];
// Do stuff with point
}
I would normally go for the NSValue approach above but sometimes you are working with an API where you can't change the output. #Andrews approach is cool but I prefer the simplicity of .count:
NSArray* arrayOfStructyThings = [someAPI giveMeAnNSArrayOfStructs];
for (NSUInteger i = 0; i < arrayOfStructyThings.count; ++i) {
SomeOldStruct tr = arrayOfStructyThings[i];
.... do your worst here ...
}