I'm a beginner in SpriteKit programming, and have been trying to figure out how to handle input from the keyboard.
What I've found so far is that you should subclass NSResponder and implement it like this:
@interface AppDelegate : NSResponder <NSApplicationDelegate>
-(void)keyUp:(NSEvent *)theEvent;
-(void)keyDown:(NSEvent *)theEvent;
@end
@implementation AppDelegate
-(void)keyUp:(NSEvent *)theEvent
{
NSLog(@"Key Released");
}
-(void)keyDown:(NSEvent *)theEvent
{
NSLog(@"Key Pressed");
}
@end
Obviously, there are a few more methods/properties in the interface and implementation of AppDelegate
but I didn't put them there to keep the question relevant.
Next, I would start using key codes to detect which keys are being pressed, but the keyUp
and keyDown
methods don't even get called. I'm not sure why.
Any Help?
Update:
Thanks for your answers! I discovered that you have to implement keyUp
and keyDown
directly in your scene class, because they won't get called from the AppDelegate. Thanks again for the help!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…