I am trying to make a simple craps game. I have done a fair amount of C programming in the past year and am trying to convert over to GUI and Obj C. I have a button and two image cells inside my window. The images are seen as outputs (dye, dye2) and the button holds the action(roller) My goal is to have the user click the button and then have the image cells show a few images and then stay with a specific image after iterating the image change, lets say, three times. Here's my Dice object and my app delegate files:
THANKS!
@implementation Dice
-(NSImage *) dicecreator {
int x;
x = rand() % 6 + 1;
switch(x){
case 1:
_side = [NSImage imageNamed: @"firstside.jpg"];
break;
case 2:
_side = [NSImage imageNamed: @"secondside.jpg"];
break;
case 3:
_side = [NSImage imageNamed: @"thirdside.jpg"];
break;
case 4:
_side = [NSImage imageNamed: @"fourthside.jpg"];
break;
case 5:
_side = [NSImage imageNamed: @"fifthside.jpg"];
break;
case 6:
_side = [NSImage imageNamed: @"sixthside.jpg"];
break;
default:
return 0;
}
return _side;
}
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
}
- (IBAction)roller:(id)sender {
Dice *rollView = [[Dice alloc]init];
[self.dye setImage:rollView.dicecreator];
[self.dye2 setImage: rollView.dicecreator];
[self performSelector:@selector(dye) withObject:rollView.dicecreator afterDelay:0.5];
[self performSelector:@selector(dye2) withObject:rollView.dicecreator afterDelay:0.5];
[self performSelector:@selector(dye) withObject:rollView.dicecreator afterDelay:0.5];
[self performSelector:@selector(dye2) withObject:rollView.dicecreator afterDelay:0.5];
[self performSelector:@selector(dye) withObject:rollView.dicecreator afterDelay:0.5];
[self performSelector:@selector(dye2) withObject:rollView.dicecreator afterDelay:0.5];
}
@end
And what is Your question?
For now I can see that all the selectors are going to be called about 0.5 seconds after the roller method is invoked - You should probably choose increasing values like 0.5, 1, 1.5...
Also I would recommend using NSTimer class instead: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html
Edit: after reading Your question again I think You misunderstand what a selector is - You should put a method in it like that:
-(void) assignImage1:(NSImage*)image {
[self.dye setImage:image];
}
-(void) assignImage2:(NSImage*)image {
[self.dye2 setImage:image];
}
...
[self performSelector:@selector(assignImage1:) withObject:[rollView dicecreator] afterDelay:0.5];
Note also that in objective-C You call object's functions like:
[rollView dicecreator]