0

I'm currently making a game. I have an UIImageView that scrolls from right to left and once it leaves the ViewController the image resets on the right and scrolls left again, this currently set to displays "image1" I would like to change it to display a different image randomly from a set of 5 each time it resets to the right.

Here is my Method:

- (void)PlacePipe{

    RandomTopPipePosition = arc4random() %350;
    RandomTopPipePosition = RandomTopPipePosition - 228;
    RandomBottomPipePosition = RandomTopPipePosition + 660;

    PipeTop.center = CGPointMake(340-10, RandomTopPipePosition);
    randomImagebottom.center = CGPointMake(340-10, RandomBottomPipePosition);

}

The name of the images I want to randomly add into this UIImageView are -toppipestyleone.png, toppipestyletwo.png, toppipestylethree.png, toppipestylefour.png, toppipestylefive.png".

I'm not sure at the best route to doing this, I looked at doing it with an array but I'm not sure how to set up an array or even call images randomly.

Lyndsey Scott
  • 37,080
  • 10
  • 92
  • 128
C.Wetherell
  • 215
  • 2
  • 14
  • You can try creating a single array of `UIImage` objects and then retrieve a random object every time (see [this post](http://stackoverflow.com/questions/7580354/get-random-object-from-array) on how to do that). You'll need to do a little more work if you want to prevent repeated images per x number of image changes. – sooper May 31 '14 at 20:53
  • Most of the code you posted seems irrelevant to the question... May want to trim it for clarity's sake. – Lyndsey Scott May 31 '14 at 21:06
  • And you shouldn't edit your question to contain our answers because when people are looking for an answer to the same question, the whole thread will essentially become useless since it's confusing when the answers no longer answer the question. If you have other questions, please ask them separately. – Lyndsey Scott Jun 01 '14 at 08:23
  • @c.Wetherell change your profile picture.this is international website not looking good.... – karthikeyan Jun 01 '14 at 09:22

1 Answers1

4

You could put the image names in an array as you considered, ex.

NSArray *imageNameArray = [[NSArray alloc] initWithObjects:@"toppipestyleone.png", @"toppipestyletwo.png", @"toppipestylethree.png", @"toppipestylefour.png", @"toppipestylefive.png", nil];

then create and set an image using the image name at a random index of that array using:

imageView.image = [UIImage imageNamed:[imageNameArray objectAtIndex:arc4random_uniform((uint32_t)[imageNameArray count])];
Lyndsey Scott
  • 37,080
  • 10
  • 92
  • 128
  • 2
    Perhaps better to use `arc4random_uniform([imageArray count])` to avoid modulo bias. – sooper May 31 '14 at 21:05
  • do i have to delegate an array in my .h and should this code go in my `-(void)PlacePipe`? – C.Wetherell May 31 '14 at 21:07
  • @C.Wetherell You don't *have* to delegate it in your .h, but you should probably store it as a class variable either in your .h or at the head of your .m so that you don't have to initialize it multiple times. And I actually can't see a proper spot to place the code... Are you adding each "pipe" UIImageView as a subview anywhere? – Lyndsey Scott May 31 '14 at 21:24
  • to tell thie UIImageView what image to use i simply just put an `IBOutlet` in my .h and then in the interface builder i dragged the outlet to the `UIImageView` then i just selected the image name using the toolbar on the right of xcode 5 – C.Wetherell May 31 '14 at 21:27
  • @C.Wetherell Gotcha. I think PlacePipe seems like an appropriate place then if you want to set the image view to display a random image whenever the pipe is placed using that method. – Lyndsey Scott May 31 '14 at 21:29
  • And @sooper, yeah, after doing some research, I agree. Although it won't make a huge difference re:modulo bias since the array length is so small and the RAND_MAX is so large, I may as well use arc4random_uniform since the group has a known, finite number. It's more correct that way. I'll make the correction. – Lyndsey Scott May 31 '14 at 21:31
  • +1 good answer. @LyndseyScott, you're right that the different flavors of arc4random hardly matter here, but the _uniform variety does save you the modulo operation. While we're nit picking, the whole thing could be prettier if you use modern array literal (and indexing) syntax: @[@"imageA", @"imageB", ...] and imageNameArray[arc4random_uniform{...)] – danh May 31 '14 at 22:17
  • @LyndseyScott ive Edited my question as you suggested in the comments for the question and i've got a couple problems too. – C.Wetherell May 31 '14 at 22:51
  • @C.Wetherell The "UIImageView" portion of "UIImageView.image" should be the variable of your image view -- ex. imageView.image, not UIImageView.image – Lyndsey Scott Jun 01 '14 at 01:59
  • Thank you, my mistake sorry about that it was late last night haha, ive changed all to suit now, and i get a "yellow error" that says "Implicit conversion loses interger precision: ‘NSUInterger’ (aka usdigned long’) to ‘u_int32’ (aka ‘unsigned int’) " - i'be looked around and a post i saw said to declare the count as an integer i did that but no luck, any suggestions? – C.Wetherell Jun 01 '14 at 07:12
  • @C.Wetherell That's not an error, it's a warning. What line is it next to? – Lyndsey Scott Jun 01 '14 at 08:09
  • its next to `PipeTop.image = [UIImage imageNamed:[imageNameArray objectAtIndex:arc4random_uniform([imageNameArray count])]];` i also get this on the line above `Thread1: EXC_BAD_ACCESS (code=exc_I386_GPFLT)` but its a green field not an error – C.Wetherell Jun 01 '14 at 08:13
  • Try adding a (uint32_t) cast, ex: [imageNameArray objectAtIndex:arc4random_uniform((uint32_t)[imageArray count])] – Lyndsey Scott Jun 01 '14 at 08:15
  • that fixed the yellow warning but it still crashed because of the "EXC_BAD_ACCESS.." – C.Wetherell Jun 01 '14 at 08:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/54866/discussion-between-lyndsey-scott-and-c-wetherell). – Lyndsey Scott Jun 01 '14 at 08:29