Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top