将冲动应用于SKShapeNode

时间:2017-05-15 20:22:31

标签: ios swift sprite-kit

import SpriteKit
import GameplayKit

class GameScene: SKScene {
    var player = SKShapeNode()
    let ballRadius: CGFloat = 20

    override func didMove(to view: SKView) {
        view.scene?.anchorPoint = CGPoint(x: 0.5,y : 0.5)
        player = SKShapeNode(circleOfRadius: ballRadius)
        player.name = "player"
        player.fillColor = .green
        player.position = CGPoint(x: 0, y: 0)
        player.physicsBody?.isDynamic = true

        self.addChild(player)

        player.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 10))
    }

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

我试图给玩家增添一种冲动,但我无法弄明白,请特别注意我是初学者。我认为这与skshapenode不是基于体积的对象有关,但我不确定

1 个答案:

答案 0 :(得分:0)

你走在正确的轨道上!您需要做的就是为您的播放器添加一个物理主体以使其移动。

player.physicsBody = SKPhysicsBody(circleOfRadius: 30) 
    // the 30 in this case is an example. You can just change it to whatever you want