.applyimpulse不能在Xcode 8.3.3中使用SKSpriteNode()

时间:2017-07-26 13:21:50

标签: ios xcode sprite-kit xcode8

我一直在尝试在YouTube上关注Pong指南(我是SpriteKit的新手),当我在Xcode 8.3.3中运行我的代码时,球不会移动。该指南使用Xcode 8,所以必须改变一些东西。这是我的GameScene.swift代码:

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
}
}

1 个答案:

答案 0 :(得分:1)

正如Simone Pistecchia指出的那样,我没有创建一个物理机构。我使用以下命令执行此操作:

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