user7321171
user7321171

Reputation:

.applyimpulse not working on SKSpriteNode() in Xcode 8.3.3

I have been trying to follow a Pong guide on YouTube (I am new to SpriteKit) and when I run my code in Xcode 8.3.3 the ball doesn't move. The guide uses Xcode 8 so something must have changed. Here is my GameScene.swift code:

import SpriteKit
import GameplayKit

class GameScene: SKScene {

var Ball = SKSpriteNode()
var comPaddle = SKSpriteNode()
var mainPaddle = SKSpriteNode()

override func didMove(to view: SKView) {

    Ball = self.childNode(withName: "Ball") as! SKSpriteNode
    mainPaddle = self.childNode(withName: "mainPaddle") as! SKSpriteNode
    comPaddle = self.childNode(withName: "comPaddle") as! SKSpriteNode

    Ball.physicsBody?.isDynamic = true;
    Ball.physicsBody?.applyImpulse(CGVector(dx: 20, dy: 20))


    let border = SKPhysicsBody(edgeLoopFrom: self.frame)

    border.friction = 0
    border.restitution = 1

    self.physicsBody = border
}


override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered
}
}

Upvotes: 1

Views: 114

Answers (1)

user7321171
user7321171

Reputation:

As Simone Pistecchia pointed out, I hadn't created a physics body. I did this with the following command:

Ball.physicsBody = SKPhysicsBody(circleOfRadius: Ball.size.width)

Upvotes: 1

Related Questions