-4

Hello I am fairly new to objective c and I am trying to make my own iPhone game however I get stuck when trying to do these things would you be able to help me out (I am fairly new to the site) Thanks!

I want to make an array of images, that will later be randomised and only 1 will be shown as the output. The output would be in the form of a ImageView.

My code for the array:

'NSArray *images = [NSArray arrayWithObjects:
                   [UIImage imageNamed:@"1.png"],
                   [UIImage imageNamed:@"2.png"],
                   [UIImage imageNamed:@"3.png"],
                   [UIImage imageNamed:@"4.png"],
                   [UIImage imageNamed:@"5.png"],
                   nil];'

This works however I am unsure how to randomise the output and get only 1 images.

username tbd
  • 9,152
  • 1
  • 20
  • 35
  • Also how would i make a UIImage move smoothly at a quite fast speed? – user3888729 Jul 29 '14 at 17:50
  • Do you have ur code? or you are going to write one? – Frans Raharja Kurniawan Jul 29 '14 at 17:51
  • 1
    One post, one question. Please narrow down your post. – duci9y Jul 29 '14 at 17:52
  • Sorry about that duci9y and – user3888729 Jul 29 '14 at 18:10
  • and Bejibun i have wrote some code which makes the UIImage move and i tried an array but it comes up with errors so i deleted it – user3888729 Jul 29 '14 at 18:11
  • First, please read StackOverlow's help page on how to ask questions. Secondly, always post the code that is not working, nobody can guess what you have tried and where the error might be. – Atomix Jul 29 '14 at 19:06
  • Ok will do and thanks for the advice although I'm not really fussed about the error code at the minute as i have deleted it but i need to understand the answers to the questions. – user3888729 Jul 29 '14 at 19:15
  • You need to edit this to contain only one question, however, none of these questions on their own are suitable for Stack Overflow. Questions here need to consist of more than a single sentence asking someone to explain an entire task to you. This is not a mentoring site. You need to do some work yourself so that you understand some portion of each problem you need to solve, and then put that into your post. – jscs Jul 29 '14 at 19:22
  • If you're just starting out, asking broad questions like this on Stack Overflow is not the place you need to be. You should find a good book or a series of online tutorials. Have a look at [Good resources for learning ObjC](http://stackoverflow.com/q/1374660). The Big Nerd Ranch books are excellent, and lots of people like the Stanford iOS course on iTunes U. Good luck! – jscs Jul 29 '14 at 19:23
  • possible duplicate of [How to pick a random image from NSArray and display randomly on view](http://stackoverflow.com/questions/19530918/how-to-pick-a-random-image-from-nsarray-and-display-randomly-on-view) – Paulw11 Jul 30 '14 at 08:14

1 Answers1

0

Well, to get the first image from the images array, you would do something like this:

UIImage *firstImage = [images objectAtIndex:0];

You can then use it in your UIImageView with [imageView setImage:firstImage], correct?

If you have all that down, all you need to do is find a random index between 0 and images.count. We can use arc4random_uniform(images.count) to generate that random index. Putting it all together:

UIImage *randomImage = [images objectAtIndex:arc4random_uniform(images.count)];
[imageView setImage:randomImage];
username tbd
  • 9,152
  • 1
  • 20
  • 35
  • Yes this seems to be what I need thank yo iso much for your help however as i am a beginner at this where would i add this code and the array code? – user3888729 Jul 29 '14 at 19:49
  • That depends on when you want your `UIImageView` to show the image (all you said in your question was "later"). If you have a method that's called when the image should be shown, you should put the code in there. – username tbd Jul 29 '14 at 19:51
  • Ahh i get you now, its all starting to come together. Thanks you very much for your help it has helped me greatly :) – user3888729 Jul 29 '14 at 19:54