Codesnipp.it Social Code Sharing

Amber Weinberg

How To Build A Custom Slot Machine iPhone App w/ Sound

by Amber Weinberg on Jul 05, 2010

// From Beginning iPhone 3 Development" book by Dave Mark and Jeff LaMarche // in your ViewController.h file // // CustomPickerViewController.h // 07 Pickers // // Created by Amber Weinberg on 6/27/10. // Copyright 2010 __MyCompanyName__. All rights reserved. // #import <UIKit/UIKit.h> #import <AudioToolbox/AudioToolbox.h> // needed to play sounds @interface CustomPickerViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> { // declare your outlets, one for the picker and one for the "Win" label UIPickerView *picker; UILabel *winLabel; // these will hold the image views in the slot machine NSArray *column1; NSArray *column2; NSArray *column3; NSArray *column4; NSArray *column5; UIButton *button; } @property(nonatomic, retain) IBOutlet UIPickerView *picker; @property(nonatomic, retain) IBOutlet UILabel *winLabel; @property(nonatomic, retain) NSArray *column1; @property(nonatomic, retain) NSArray *column2; @property(nonatomic, retain) NSArray *column3; @property(nonatomic, retain) NSArray *column4; @property(nonatomic, retain) NSArray *column5; @property(nonatomic, retain) IBOutlet UIButton *button; // the action method - (IBAction)spin; @end // In Your ViewController.m file // // CustomPickerViewController.m // 07 Pickers // // Created by Amber Weinberg on 6/27/10. // Copyright 2010 __MyCompanyName__. All rights reserved. // #import "CustomPickerViewController.h" @implementation CustomPickerViewController // synethesize outlets @synthesize picker; @synthesize winLabel; @synthesize column1; @synthesize column2; @synthesize column3; @synthesize column4; @synthesize column5; @synthesize button; - (void)showButton { button.hidden = NO; } - (void)playWinSound { NSString *path = [[NSBundle mainBundle] pathForResource:@"win" ofType:@"wav"]; SystemSoundID soundID; AudioServicesCreateSystemSoundID((CFURLRef) [NSURL fileURLWithPath:path], &soundID); AudioServicesPlaySystemSound(soundID); winLabel.text = @"WIN!"; [self performSelector:@selector(showButton) withObject:nil afterDelay:1.5]; } - (IBAction)spin { BOOL win = NO; int numInRow = 1; // how many of the same value in each row int lastVal = -1; // previous component's value for (int i = 0; i < 5; i++) { // loops through all values int newValue = random() % [self.column1 count]; // gets a random value if (newValue == lastVal) // compare new value to last value numInRow++; // increments if matches else numInRow = 1; // resets if doesn't lastVal = newValue; [picker selectRow:newValue inComponent:i animated:YES]; // set component to new value and reload [picker reloadComponent:i]; if (numInRow >= 3) // if there's three in a row, set win to YES win = YES; } button.hidden = YES; NSString *path = [[NSBundle mainBundle] pathForResource:@"crunch" ofType:@"wav"]; SystemSoundID soundID; AudioServicesCreateSystemSoundID((CFURLRef) [NSURL fileURLWithPath:path], &soundID); AudioServicesPlaySystemSound(soundID); if (win) [self performSelector:@selector(playWinSound) withObject:nil afterDelay:.5]; else [self performSelector:@selector(showButton) withObject:nil afterDelay:.5]; winLabel.text = @""; } - (void)viewDidLoad { // load all slot images from resource folder UIImage *seven = [UIImage imageNamed:@"seven.png"]; UIImage *bar = [UIImage imageNamed:@"bar.png"]; UIImage *crown = [UIImage imageNamed:@"crown.png"]; UIImage *cherry = [UIImage imageNamed:@"cherry.png"]; UIImage *lemon = [UIImage imageNamed:@"lemon.png"]; UIImage *apple = [UIImage imageNamed:@"apple.png"]; // create an instance for each image for (int i = 1; i <= 5; i++) { UIImageView *sevenView = [[UIImageView alloc] initWithImage:seven]; UIImageView *barView = [[UIImageView alloc] initWithImage:bar]; UIImageView *crownView = [[UIImageView alloc] initWithImage:crown]; UIImageView *cherryView = [[UIImageView alloc] initWithImage:cherry]; UIImageView *lemonView = [[UIImageView alloc] initWithImage:lemon]; UIImageView *appleView = [[UIImageView alloc] initWithImage:apple]; // arrray them NSArray *imageViewArray = [[NSArray alloc] initWithObjects: sevenView, barView, crownView, cherryView, lemonView, appleView, nil]; // assign the above array to each of 5 slot arrays NSString *fieldName = [[NSString alloc] initWithFormat:@"column%d", i]; [self setValue:imageViewArray forKey:fieldName]; // assign array to property [fieldName release]; // clean memory [imageViewArray release]; [sevenView release]; [barView release]; [crownView release]; [cherryView release]; [lemonView release]; [appleView release]; } srandom(time(NULL)); // random numb generator } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } - (void)viewDidUnload { self.picker = nil; self.winLabel = nil; self.column1 = nil; self.column2 = nil; self.column3 = nil; self.column4 = nil; self.column5 = nil; self.button = nil; [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (void)dealloc { [picker release]; [winLabel release]; [column1 release]; [column2 release]; [column3 release]; [column4 release]; [column5 release]; [button release]; [super dealloc]; } // Creates a horizontal line and title to discern from dropdown #pragma mark - #pragma mark Picker Data Source Methods - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 5; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return [self.column1 count]; } #pragma mark Picker Delegate Methods // return one of the image views from an array - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { NSString *arrayName = [[NSString alloc] initWithFormat:@"column%d", component+1]; NSArray *array = [self valueForKey:arrayName]; [arrayName release]; return [array objectAtIndex:row]; } @end

Can't see the comments? Please login first :)