I am trying to learn the Swift language and I have followed a lot of tutorials on Youtube. Since most of them are for iOS, I wanted to make an OSX version of this: https://www.youtube.com/watch?v=8PrVHrs10to (SideScroller game)
I watched him closely, but I was stuck for about 22 minutes of the video when I need to update the playerβs position. My first problem was getting the keys pressed. I came from C #, so I wanted to find something like
If(Keyboard.GetState().IsKeyDown(Keys.Right)) man.Position.x += 5
but this does not exist, so I figured it out:
override func keyDown(theEvent: NSEvent!) { updateManPosition(theEvent) } func updateManPosition(theEvent:NSEvent) { if theEvent.keyCode == 123 { man.position.x -= 2 } else if theEvent.keyCode == 124 { man.position.x += 2 } else if theEvent.keyCode == 126 { println("jump") } }
I found the corresponding value (123/124/126) using println(theEvent.keyCode) , but this is not very useful if I need to recognize many keys. In any case, he works for this game, the position of the player changes. But I have one more problem, which is that the keyDown function does not seem to be called on every update (60 times per second), which prevents the player from moving smoothly.
SO, here is my question: How can I call keyDown on every update, and does anyone have a cleaner way to get the keys pressed?
thanks
swift keyboard keyboard-events sprite-kit macos
P1kachu
source share