0

When the user taps a button, I want to move a sprite to the next nearest ball (sprite), so they can move from place to place.

As there will only be a limited amount of balls on the screen at a time, I was thinking I should store them in an array, and then when they move offscreen (are destroyed by a function I have in place), they are removed from the array.

I have the current setup (not in a loop atm) :

ball = [[CCSprite spriteWithFile:@"ball.png"] retain];
ball.position = ccp(randX, randY);

NSMutableArray *myArray = [[NSMutableArray alloc] init];  
[myArray addObject: ball];
[myArray addObject: ball];
[myArray addObject: ball];

for (int i = 0; i < 2; i++) {
    CCSprite *sprite = [myArray objectAtIndex:i];
    NSLog(@"%@", sprite.position.x);
}

Why is NSLog printing "(null)" to the console?

4

2 回答 2

0

Haha woops! Was a simple case of changing @"%@" to @"%f". Lol!

于 2012-08-07T03:25:48.943 回答
0

It looks like you actually want to find the sprite with the lowest X position:

CCSprite *sprite = [myArray objectAtIndex:0];

for (CCSrpite *s in myArray)
{
    if (s.position.x < sprite.position.x)
        sprite = s;
}

// now 'sprite' will contain the sprite with the lowest X position
于 2012-08-07T03:43:52.093 回答