带变量的 NSTimer
NSTimer with variable
在我的程序中,我希望定时器的长度在每次使用时变小。为此,我在 NSTimer 中放置了一个变量,并且在计时器为 运行 后将该变量乘以 0.9。 当"count=count0.9"被注释掉时,程序运行正常但定时器当然变小了。当它没有被注释掉时,定时器会在定时器第四次启动后立即触发(或启动)GameOver 函数。很奇怪。
NSTimer*Timer;
int count=3;
-(void)InGame{
Timer = [NSTimer scheduledTimerWithTimeInterval:count target: self selector:@selector(GameOver)userInfo:(nil) repeats:NO];
count=count*.9; }
快把我逼疯了,我为此创建了一个堆栈溢出帐户。
谢谢您的帮助!我希望我知道如何为你攻击我的整个代码。
这可能是因为您使用 int
来存储结果,所以您的十进制值被截断了。
Time 1: 3
Time 2: 3 * .9 = 2.7 => int = 2
Time 3: 2 * .9 = 1.8 => int = 1
Time 4: 1 * .9 = 0.9 => int = 0
在我的程序中,我希望定时器的长度在每次使用时变小。为此,我在 NSTimer 中放置了一个变量,并且在计时器为 运行 后将该变量乘以 0.9。 当"count=count0.9"被注释掉时,程序运行正常但定时器当然变小了。当它没有被注释掉时,定时器会在定时器第四次启动后立即触发(或启动)GameOver 函数。很奇怪。
NSTimer*Timer;
int count=3;
-(void)InGame{
Timer = [NSTimer scheduledTimerWithTimeInterval:count target: self selector:@selector(GameOver)userInfo:(nil) repeats:NO];
count=count*.9; }
快把我逼疯了,我为此创建了一个堆栈溢出帐户。 谢谢您的帮助!我希望我知道如何为你攻击我的整个代码。
这可能是因为您使用 int
来存储结果,所以您的十进制值被截断了。
Time 1: 3
Time 2: 3 * .9 = 2.7 => int = 2
Time 3: 2 * .9 = 1.8 => int = 1
Time 4: 1 * .9 = 0.9 => int = 0