views:

1616

answers:

1

Hi I want to create multiple thread in my game. One thread is for timer and other for touch event. Because when i am running this game on iphone timer conflict the touch events so touch event will not be detected. Its working smooth on iphone simulator but on iphone or on itouch its became very slow. So i m using thread for touch and timer. When i give [NSThread sleepForTimeInterval:0.01] call to timer it sleep all threads means touch also get stop. I want to stop perticular thread only.

This is my code in touchesBegan -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

 BOOL Done = NO;

 while (!Done)
 {

  NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];
  [NSThread sleepForTimeInterval:0.01];
  [self performSelectorOnMainThread:@selector(callTapCode) withObject:tapView waitUntilDone:YES];

  //performSelectorOnMainThread:@selector(callTapCode) withObject:nil waitUntilDone:YES];

  [loopPool release];
  Done=YES;
 }

 [pool drain];

}

please its very urgent help me out :(

+1  A: 

If you are trying to create a timer, you should probably just use NSTimer, because that's simpler and more efficient than creating a thread just to send a "go" event every so often.

edit: As is, you aren't creating a second you are merely stalling the main thread by calling [NSThread sleepForTimeInterval:0.01] in a loop. This means your main thread is no longer running the event loop which can cause all kinds of things to no longer work.

rpetrich
Now my game is running smooth on itouch with thread concept but on iphone its still creating problem to detect touch with timer at time.I dont why this problem is coming on iphone only
Are you still using a timer of 0.01? You should leave more time between updates as the human eye cannot see more than 60Hz. The reason why the new iPod touch works but not the iPhone is that it has a faster CPU. On the iPhone, the timer events are being generated faster than they are being consumed.
rpetrich
My game is totally depend on touch events if it will not get detected because of timer then its of no use. So i use thead concept instead of timer but still some problem ob iphone. I tried everything in it. If u ve other solution then plz tell me
If you schedule a procedure to run every 1/100 of a second and it takes longer than that to run, the scheduled events will build up in the event queue leaving no room for other events including touch events.
rpetrich
Whether you schedule those events via NSTimer or performSelectorOnMainThread you will have the same issue.
rpetrich