1
我遇到了麻烦,我想知道是否有人可以帮忙?图像未出现在detachNewThreadSelector
当用户在我的测验游戏中点击答案时,我在屏幕上加载了一个覆盖图像,该图像是勾号或十字形以指示答案是否正确。图像正在加载到新线程中。
问题是图像不会在用户每次点击答案时出现,它只会在一轮中出现大约20%的时间。
更令人感到奇怪的是,虽然我在几个月前已经准备好了iOS3游戏,并且这个问题没有发生,但它似乎只是在更新到iOS4时才开始。
下面是代码:
tick = [UIImageView alloc];
[tick initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
[tick setImage:[UIImage imageNamed:@"correct.png"]];
cross = [UIImageView alloc];
[cross initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
[cross setImage:[UIImage imageNamed:@"wrong.png"]];
的线程在这里称为:
的图像在的loadView方法创建
-(void)answerAttempt:(UIButton *)b{
ansQues++;
if([b.currentTitle isEqualToString:ans]){
[NSThread detachNewThreadSelector: @selector(showTick) toTarget:self withObject:nil];
[appDelegate playSound:1];
score++;
...
...
...
}else{
[NSThread detachNewThreadSelector: @selector(showCross) toTarget:self withObject:nil];
[appDelegate playSound:0];
}
这里是两个线程调用的函数:
-(void)showTick{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[tick setHidden:NO];
[levelPage addSubview:tick];
[levelPage bringSubviewToFront: tick];
usleep(500000);
[tick removeFromSuperview];
[pool release];
}
-(void)showCross{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[cross setHidden:NO];
[levelPage addSubview:cross];
[levelPage bringSubviewToFront: cross];
usleep(500000);
[cross removeFromSuperview];
[pool release];
}
正如你可以从代码中看出的那样,我尝试了各种方法来试图让它每次都显示出来,但是现在还没有找到任何解决方案。我已经检查过以确保每次都调用正确的线程,而且它的确如此 - 只是UIImage用于勾号和交叉不显示。
请随时提出任何进一步的问题。
谢谢。
+1对我来说很好 – 2010-10-13 15:21:27
感谢您的帮助和建议队友,但即时通讯尝试在我的showTick方法中使用该行代码,就在睡眠之前,我得到一个错误消息:'UIImageView'可能不会响应'-performSelectorOnMainThread:withObject:waitUntilFinished:'任何想法解决这个问题? – user474582 2010-10-14 12:54:31
我从内存中写道。你有没有看过文档?他们是一个很好的资源。我可能在某处出现错字。 – coneybeare 2010-10-14 14:37:48