gpt4 book ai didi

objective-c - 随着时间间隔改变 NSImageView

转载 作者:行者123 更新时间:2023-12-03 17:51:39 25 4
gpt4 key购买 nike

我正在尝试制作一个简单的双骰子游戏。在过去的一年里,我完成了大量的 C 编程,并且正在尝试转换为 GUI 和 Obj C。我的窗口内有一个按钮和两个图像单元。图像被视为输出(染料、染料2),按钮保持 Action (滚轮)我的目标是让用户单击按钮,然后让图像单元显示一些图像,然后在迭代图像更改(比方说三次)后保留特定图像。这是我的 Dice 对象和我的应用程序委托(delegate)文件:

谢谢!

@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

最佳答案

你的问题是什么?

现在我可以看到,所有选择器都将在调用 Roller 方法后大约 0.5 秒被调用 - 您可能应该选择递增的值,例如 0.5、1、1.5...

此外,我建议使用 NSTimer 类:https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html

编辑:再次阅读您的问题后,我认为您误解了选择器是什么 - 您应该像这样在其中添加一个方法:

-(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];

另请注意,在 Objective-C 中,您可以调用对象的函数,例如:

[rollView dicecreator] 

关于objective-c - 随着时间间隔改变 NSImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24813142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com