-1

I'm brand new to SpriteKit, and I'm creating a game that involves randomizing the CGPoint positions of objects on my screen when the user reaches certain score increments. I created a plist of the 8 set positions I want my objects to choose from when this method is called, but I'm having a problem with my objects selecting duplicate plist positions and overlapping each other. Is there a method I can call that will prevent my objects from picking the same position from the plist as another object when that method is called?

FYI, I plan on calling this randomization method many times throughout the course of the game.

I wrote the following code but my game crashes.

- (NSMutableArray *)pickAndRemoveFromList:(NSMutableArray *)list {

    list = [_objectList copy];

    NSInteger randomIndex = arc4random()%list.count;
    Objects *object = [list objectAtIndex:randomIndex];

    [list addObject:object];
    [list removeObject:object];

    return list;
}
0x141E
  • 12,613
  • 2
  • 41
  • 54
Newbie
  • 164
  • 7
  • I saw this answer in your suggested post "Create a local mutablearray copy of main array, and after getting random value, remove object available at random index from local array, process it till array count is 1.".....would you happen to know how I can create this programmatically? – Newbie Oct 06 '14 at 01:44
  • I wrote the above code using your suggested method. – Newbie Oct 06 '14 at 02:11

1 Answers1

1

Your method has several problems. First, you're passing in a mutable array called list, but then in the first line of the method you redefine what list is. You either shouldn't pass in the list to start with, or you shouldn't redefine it. Second, if _objectList is an immutable array, you need to use mutableCopy to make it a mutable array, not copy. Third, there's no need to add an object to the list, and then turn around and remove it in the next line. Also a better random number function to use is arc4random_uniform(). So, if you're passing in a mutable array, your method should be,

- (NSMutableArray *)pickAndRemoveFromList:(NSMutableArray *)list {

    NSInteger randomIndex = arc4random_uniform((int)list.count);
    Objects *object = [list objectAtIndex:randomIndex];
    [list removeObject:object];
    return list;
}
rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • I wrote your suggested code and called it in my update method but nothing happened. I took "list NSMutableArray" out of my initial definition of the - (NSMutableArray *)pickAndRemoveFromList method, and redefined it locally in the method as a copy of my global _objectlist. I have another object that moves across the screen throughout the game, so I wrote this if statement in my update method, "if (moving object.position.x > 50){ [self - (NSMutableArray *)pickAndRemoveFromList]" but nothing happened. – Newbie Oct 06 '14 at 04:52
  • Here's my code that I wrote `- (NSMutableArray *)pickAndRemoveFromList{ NSMutableArray *list = [_objectList copy]; NSInteger randomIndex = arc4random_uniform((int)list.count); Objects *object = [list objectAtIndex:randomIndex]; [list removeObject:object]; return list;` – Newbie Oct 06 '14 at 04:53