Frævik
Frævik

Reputation: 19

Smoother Sprite-Kit objects movement

I'm currently programming a little pong-game for Mac, but I can't find a way to make the paddles move smoothly. I use the keyDown-function to detect when a key (for example W) is pushed. This executes a run-command:

override func keyDown(with event: NSEvent) {
        if event.keyCode == 126 {
            pR.run(SKAction.move(by: CGVector(dx: 0, dy: 15), duration: 0))
        }
}

The thing is, when I push the arrow up-button (keyCode 126), my sprite/paddle (pR) moves one time, and if I keep holding the button down, it starts moving continuously.

Also, if I have two if-instructions (for example one for both pong players), it seems like there can't be made two inputs at the same time, that is, both players can't push a button and expect a response.

How can I solve these issues? All help is appreciated.

Upvotes: 0

Views: 310

Answers (1)

0-1
0-1

Reputation: 773

Question 1: Why isn't my paddle moving smoothly?
Why are you running a movement SKAction with a duration of 0?
This is the exact same as changing it's y position
You could making the duration longer like 0.1

If you want to do this, you should remove p1's SKAction every time you click the W key. Why? You don't want it to run 2 SKActions at the same time. (I'm assuming pR is your player 1 paddle. Stands for Paddle Right I guess.)

pR.removeAllActions()
pR.run(SKAction.move(by: CGVector(dx: 0, dy: 15), duration: 0.1))

Or, if you want it to be immediate, you could do this instead
(Although your paddle would move quite quickly depending on your frame rate)

pR.position.y += 15

I recommend the first option.
Or, if you like the second option because it's cleaner, you replace the 15 to 10 or 5.


Question 2: There can't be made two inputs at the same time
• 1: It's impossible for you to click 2 buttons at the 'exact' same time.
• 2: What does this mean?
• 3: Swift runs keyDown with the first key you pressed.
• 4: Swift then runs keyDown with the second key you pressed.

(Even if you think you pressed two keys at the same time, you didn't. One was probably pressed a microsecond before the second. It's all about timing. This is the same for SpriteKit on iOS, you cannot have two 'touchesBegan' inputs at the same time. It's quite rare.)

How to fix it:
You should be able to add a second 'if' statement for another key for player 2.
Example code (Except I used a switch to save space)

override func keyDown(with event: NSEvent) {
        switch Int(event.keyCode) {

        // W Key
        case 13: // (Player 1 moves up)
        // S Key
        case 1: // (Player 1 moves down)

        // I Key
        case 34: // (Player 2 moves up)
        // K Key
        case 40: // (Player 2 moves down)

        // Someone clicked the wrong key
        default: return
        }
}

Guess what! This code ^^^ almost works. I haven't answered your third question.


Question 3: Paddle Moves Once, Stops, Then Starts Moving Continuously
This is how the keyDown function works apparently. Here is what I recommend:

• Keep track in a variable which keys have been pressed
• Use the 'keyUp' function to remove those stored values.

How does this work?

var keysPressed: Set<Int> = []

override func keyDown(with event: NSEvent) {
        keysPressed.insert(event.keyCode)
}
override func keyUp(with event: NSEvent) {
        keysPressed.remove(event.keyCode)
}
override func update(_ currentTime: TimeInterval) {
        for i in keysPressed {
           // Put my Movement Switch Statement In Here
        }
}

I hope this helps! If you have any questions, please feel free to reply.


Snap, I just realized that this question was answered in the tiny comment section above. Oh well.

Upvotes: 1

Related Questions