0

I'm new to SpriteKit, and I don't know why my code is not working at all. After I create all the nodes, the only one that I want affected by physics is the ball. I want to apply an impulse to the ball node, but I can' make it work.

Here's the code:

import SpriteKit
import GameplayKit

class GameScene: SKScene {

    var ramp = SKSpriteNode()
    var label = SKLabelNode()
    var ball = SKSpriteNode()


    override func didMove(to view: SKView) {
        ball = self.childNode(withName: "ball") as! SKSpriteNode
        ramp = self.childNode(withName: "ramp") as! SKSpriteNode
        label = self.childNode(withName: "label") as! SKLabelNode

        ball.physicsBody?.isDynamic = true
        ball.physicsBody?.affectedByGravity = true

        let border = SKPhysicsBody(edgeLoopFrom: self.frame)
        border.friction = 0
        border.restitution = 0
        self.physicsBody = border

        ball.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 50))
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    }
    override func update(_ currentTime: TimeInterval) {
        // Called before each frame is rendered
    }


}
Ron Myschuk
  • 6,011
  • 2
  • 20
  • 32

0 Answers0