Question

I want to put together a ping pong game in Xcode with Sprite Kit. But I want to do it for Mac OS X, there are no tutorials on fixing my code, and I want to make a keydown event to move the paddle. Here's my code.

-(void)keyDown:(NSEvent *)theEvent {
    /* Called when a keyPress occurs */
    // inside code
}

EDIT: So I downloaded a pong game that is an Xcode project then I looked at it and saw this:

- (void)handleKeyEvent:(NSEvent*)keyEvent keyDown:(BOOL)isKeyDown {

if ([keyEvent keyCode] == LED_PONG_MOVE_UP || [keyEvent keyCode] == LED_PONG_MOVE_UP_ALT) {
    self.moveUp = isKeyDown;
} else if ([keyEvent keyCode] == LED_PONG_MOVE_DOWN || [keyEvent keyCode] == LED_PONG_MOVE_DOWN_ALT) {
    self.moveDown = isKeyDown;
}

}

and this:

#define LED_PONG_MOVE_UP        13  // W
#define LED_PONG_MOVE_UP_ALT    126 // Arrow Up
#define LED_PONG_MOVE_DOWN      1   // S
#define LED_PONG_MOVE_DOWN_ALT  125 // Arrow Down

So that about solves it for anyone in my place, fellow iOS haters, and Mac OS X lovers.

Notify me for more info if you need help.

Était-ce utile?

La solution

It sounds like you simply need to make certain that your SKScene is designated as the first responder, which means that all events come in through your SKScene first.

So when your game (or scene) starts up, make certain that your NSWindow calls "makeFirstResponder:" with your SKScene object as the parameter.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top