2

I have 4 images in 4 buttons: imag01.jpg, imag02.jpg, imag03.jpg, imag04.jpg and 4 sounds: sound01.aifc, sound02.aifc, sound03.aifc, sound04.aifc.

I am generating 4 buttons like this:

UIButton *oneButton = [UIButton buttonWithType:UIButtonTypeCustom];
[oneButton setTitle:@"1" forState:UIControlStateNormal];
[oneButton setTitle:@"1" forState:UIControlStateHighlighted];

[oneButton setImage:[UIImage imageNamed:@"imag01.jpg"] forState:UIControlStateNormal];
[oneButton setImage:[UIImage imageNamed:@"imag01.jpg"] forState:UIControlStateHighlighted];
oneButton.frame = CGRectMake(20, 100, 140, 100);

[scrMain addSubview:oneButton];

I want to generate a random number from 1 to 4, (for example 3), play the sound related to it (sound03.aifc) and ask the user to press the correct button for that sound (imag03.jpg).

How can I make a link between the buttons and the sounds?

Caleb
  • 124,013
  • 19
  • 183
  • 272
Mihai
  • 131
  • 1
  • 1
  • 10
  • Instead of asking for a pointer to sample code (a "request for resources" question), it's better on SO to explain your problem and ask for help with the solution. That way, when someone else has a similar problem, the answers here can help them, too. I've edited your question slightly to reflect that -- take a look and modify if you think it needs it. – Caleb Apr 19 '13 at 16:41

3 Answers3

1

See the answer for this question to generate a random number between 0 and 3: Generating random numbers in Objective-C.

If you aren't going to index your buttons from zero you can simply add one to the result.

Community
  • 1
  • 1
jszumski
  • 7,430
  • 11
  • 40
  • 53
1

Do this:

  • Assign different values from 0 to 3 to the tag property of each button.

  • Put the sounds in an array.

  • Generate a random integer between 0 and 3, inclusive. Use that as an index to retrieve a sound from the array. Play that sound.

  • Enable the 4 buttons and ask the user to tap the corresponding button.

  • If the tag property of the button matches the number you selected, HOORAY! Good job, user!

  • Else, TOO BAD! Try again.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • until step 5 i have pretty much what i need. how do i check the correspondance between the integer number and the tag property which is not integer i suppose? – Mihai Apr 19 '13 at 15:21
  • `tag` is indeed an `NSInteger` – jszumski Apr 19 '13 at 15:23
  • @Mihai [Tags are `NSInteger`s](http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instp/UIView/tag). If all four buttons send the same action to the same target, just say `if (sender.tag == theSoundIndex) { [self tellUser:@"HOORAY!"] }`. – Caleb Apr 19 '13 at 15:24
  • i have this after each button [scrMain addSubview:oneButton]; [oneButton addTarget:self action:@selector(onButtonPressCorrect) forControlEvents:UIControlEventTouchUpInside]; and -(IBAction)onButtonPressCorrect:(id)sender{ } what are you refering to as sender.tag? – Mihai Apr 19 '13 at 16:27
  • and originally it was like this -(void)onButtonPressCorrect { } – Mihai Apr 19 '13 at 16:31
  • [Actions in iOS can take up to two parameters](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html#//apple_ref/doc/uid/TP40002974-CH7-SW44) -- the first is a pointer to the object sending the action and is usually named `sender`. (The second is the event that triggered the action.) – Caleb Apr 19 '13 at 16:36
  • 1
    found it, it was an issue of semantics , i had to use "if ([sender tag] == theSoundIndex)". Thanks a lot Caleb, your help is much appreciated ! – Mihai Apr 19 '13 at 17:29
1

This is actually pretty easy to do with a NSArray. For easiest understanding lets create two arrays.

One for images and one for sound.

NSArray* buttonImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"imag01.jpg"],
                      [UIImage imageNamed:@"imag02.jpg"],[UIImage imageNamed:@"imag03.jpg"][UIImage imageNamed:@"imag04.jpg"],  nil];

NSArray* buttonSounds = [NSArray arrayWithObjects:[same idea as the uiimage, i don\'t know what you are using to load sound],  nil];

Then use arc4random to generate random numbers:

int randomNumber = arc4random()%4;

Then use the number you just generated to call out a image and sound and apply it the uibutton

UIImage* image = [buttonImages objectAtIndex:randomNumber];
[oneButton setImage:image forState:UIControlStateNormal];

Again do the same thing for sound and attach the property of the sound according to the same randomNumber we just generated. Just make sure the images array and sound array line up according to which image is the correct sound.

I couldn't write code for generating random sound for you because I don't understand how you are loading sound. So I am using UIImages as an example. So to point you in the right direction replace UIImage code with sound code, to apply the same idea.

To check if the sound match, i'd check if [buttonSound objectAtIndex:randomNumber]; is equal the one attached to the button that the user pressed.

John Riselvato
  • 12,854
  • 5
  • 62
  • 89