Swift PhysicsBody不受Impulse的影响

时间:2017-02-27 09:58:01

标签: ios swift3 xcode8 game-physics

这是我在这里的第一篇文章,我是iOS编程的新手。我正在使用Xcode 8和Swift 3来构建我的练习游戏。我想要的只是我想在点击屏幕时跳球,但冲动不起作用。这是我写的代码。

class GameScene: SKScene {
var footBall = SKSpriteNode(imageNamed: "sfb")
var Score = 0
var GameStarted = false


override func didMove(to view: SKView) {
    self.removeAllChildren()
    self.anchorPoint = CGPoint(x: 0.0, y: 0.0)

    footBall.size = CGSize(width: 100, height: 100)
    footBall.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
    footBall.physicsBody = SKPhysicsBody(circleOfRadius: footBall.frame.height / 2)
    footBall.physicsBody?.affectedByGravity = false
    footBall.physicsBody?.allowsRotation = true
    footBall.physicsBody?.isDynamic = true
    self.addChild(footBall)
}


override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if(GameStarted == false){
        GameStarted = true
        footBall.physicsBody?.affectedByGravity = true
        footBall.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
        footBall.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 90))
    }
    else{
        footBall.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
        footBall.physicsBody?.applyForce(CGVector(dx: 0, dy: 90))
    }
}

override func update(_ currentTime: TimeInterval) {

}

当点击时,球的速度暂时变为零,但是冲动应该将其驱动,这是不会发生的。请帮助我完成这个并忽略任何无知,因为这是我的第一个问题,我是新手。

1 个答案:

答案 0 :(得分:1)

请注意,在真正的分支中,你正在施加一种力量,而不是一种冲动。

变化:

footBall.physicsBody?.applyForce(CGVector(dx: 0, dy: 90))

footBall.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 90))

不要理解为什么你总是把速度归零。