Reputation: 1033
Hi I would like to know what's is wrong with this codes :
IBOutlet UIImageView sam1;
IBOutlet UIImageView sam1;
NSTimer *timer1;
NSTimer *timer2;
-(void)start {
[self performSelector:@selector(myTimer1) withObj....
}
-(void)startAlso {
[self performSelector:@selector(myTimer2) withObj....
}
-(void)myTimer1 {
timer1 = [NSTimer scheduledTimeWithInterval .....
}
-(void)myTimer2 {
timer2 = [NSTimer scheduledTimeWithInterval .....
}
-(void)specialFuct {
[timer1 invalidate];
[timer2 invalidate];
}
Desc of my program is a simple game using imageview. each imageview has their own NSTimer since they are both moving.
when 2 imageview collides, their timer is stop using invalidate.
my problem is, 2 imageview is coming from center.y -40 which is outside the screen layout.
when the 2 imageview is inside the screen, it stops. but when it is still outside the screen, -40 let say.
their NSTimer is still moving which is causing me a bug.
is their any explanation of why is this happening?
thank you in advance! first time xcoder here.
UPDATE!
I already found the problem,
it is on the
[self performSelector:@selector(myTimer1) withObj....
part.
What's happening is, the NSTimer is already shutdown but the performSelector delay is still runnning. My alternative way is to have a boolean and check if the game is already over or not.
if(gameOverIsNotOver) {
[self performSelector:@selector(myTimer1) withObject:nil afterDelay:3];
}
is there any best way to do this? thanks in advance!
Upvotes: 0
Views: 108
Reputation: 2494
You have awful description with dots in code. But hope my answer will help and solve your problem.
If you create scheduled NSTimer
you can call invalidate
to cancel its action.
For performSelector
you can call:
[NSObject cancelPreviousPerformRequestsWithTarget:self];
And it will cancel all future selectors with delay for this (self
) class.
Better don't use selectors and timers together. Global variables - is a bad pattern too.
Upvotes: 2