0
votes

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.

1
Are you asking how to put a variable into an array? or are you asking how to get the position of the sprites?Just a coder
How to get the position of the sprites inside the array.stenger96
Do you already know how to get the position of the sprites? and do you know how to store an object into an array?Just a coder
Yeah? sprite.position; and [array insertObject:object atIndex:i];stenger96

1 Answers

2
votes

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;