Blocking for input during a loop?
Blocking for input during a loop?
- Subject: Blocking for input during a loop?
- From: Per Bull Holmen <email@hidden>
- Date: Sun, 17 Jan 2010 13:59:18 -0800 (PST)
Hi
Is it possible, in Cocoa, do program a loop which goes something like this:
for i=1 to 20
do something
block for GUI user input
do something with the input
repeat
It is for a simple game that plays tones etc, and lets the user guess what was played. Originally, I had implemented it without a loop, just using regular target/action mechanisms. The code got awfully complex with too many state variables, and it got difficult to implement new functionality etc. So, I tried implementing it as a loop running in a separate thread, but the loop is supposed to add/remove subviews on an NSView as the game progresses, and I found out that NSView doesn't want you to do that from a different thread than the main thread. I thought it would get cumbersome to arrange for messaging to the main thread to switch views, so I thought maybe running the loop in the main thread, and using NSRunLoop might be a solution?
I have no clue about NSRunLoop, but I tried that idea, and implement the method waiting for UI input this way:
-(int)waitForInput {
while( inputAvailable != CodeInput )
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
inputAvailable = NoInput;
return( inputCode );
}
The event handling methods should then update the instance variable inputAvailable to equal CodeInput (an enum value), and the inputCode would be updated to tell the game loop what choice the user made. But during the NSRunLoop statement no input is accepted at all. The handler methods are never run. I guess I'm completely lost and that NSRunLoop was never meant for this type of thing.
The game has several windows in which the user can click to respond to the game questions, therefore trying to run modally for one window is not ideal. If running modal for a window is the recomended way, I might look into collecting all the current input views into one window.
So, what do you recommend?
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden