【问题标题】:Swift - Increase speed of a moving Node using SpriteKitSwift - 使用 SpriteKit 提高移动节点的速度
【发布时间】:2017-07-08 23:05:38
【问题描述】:

我正在开发一个类似于 pong 的游戏,在开始游戏时,我以随机方向向球施加脉冲,例如位于屏幕中心的球

func impulse(){
    let randomNum:UInt32 = arc4random_uniform(200)
    let someInt:Int = Int(randomNum)

    //Ball Impulse
    if someInt<49{
    ball.physicsBody?.applyImpulse(CGVector(dx: ballSpeed+3, dy: ballSpeed-5))
    }else if someInt<99{
        ball.physicsBody?.applyImpulse(CGVector(dx: ballSpeed+5, dy: -ballSpeed+5))
    }else if someInt<149{
        ball.physicsBody?.applyImpulse(CGVector(dx: -ballSpeed-5, dy: -ballSpeed+5))
    }else if someInt<200{
        ball.physicsBody?.applyImpulse(CGVector(dx: -ballSpeed-3, dy: ballSpeed-5))
    }

}

其中ballSpeed是球的预设速度。 那么有什么办法可以在球运动时逐渐增加球的速度吗?由于球会继续在屏幕上弹跳,因此很难使用 dx 和 dy 对其施加力。

编辑-

我使用上述方法只是为了在游戏开始时为球分配一个随机象限/方向。

开始,当实施脉冲方法时,例如 Game start image

并且我想在游戏过程中随着时间的推移将球的速度增加一个预定义的单位,例如 Gameplay

【问题讨论】:

  • 如果精灵通过物理移动,那么它的velocity 属性将有一个值。这是一个向量,因此您可以使用速度作为该脉冲的向量来应用另一个脉冲,以使其更快。
  • 你知道你的间隔是不均匀的吗?您的第一个区间从 0 到 48,下一个从 49 到 98,下一个从 99 到 148,最后一个从 149 到 200,这意味着前 3 个范围只有 49 个值宽,最后一个是 51。
  • @user1118321 - 实际上,我更喜欢在比赛开始时将球从第 3 象限开始,所以这就像我在这里给出的额外 2 分的奖励,执行时通常可以忽略不计跨度>
  • @SteveIves - 在这种情况下,我试图每 5 秒将球速提高 2 个单位,那么我该如何使用 ball.physicsBody?.applyImpulse((ball.physicsBody?.velocity)!) 来实现它

标签: ios swift sprite-kit game-physics skphysicsbody


【解决方案1】:

好的,给你!无论如何,球总是变得更快!!!调整amount 以确定每帧球的速度增加多少。

class GameScene: SKScene, SKPhysicsContactDelegate {

  let ball = SKShapeNode(circleOfRadius: 20)
  var initialDY = CGFloat(0)
  var initialDX = CGFloat(0)



  override func didMove(to view: SKView) {
    self.physicsBody = SKPhysicsBody(edgeLoopFrom: frame)
    self.physicsWorld.gravity = CGVector.zero

    // Configure ball pb:
    let pb = SKPhysicsBody(circleOfRadius: 20)
    ball.physicsBody = pb
    addChild(ball)

    pb.applyImpulse(CGVector(dx: 10, dy: 10))
  }

  override func update(_ currentTime: TimeInterval) {
    initialDX = abs(ball.physicsBody!.velocity.dx)
    initialDY = abs(ball.physicsBody!.velocity.dy)
  }

  override func didSimulatePhysics() {
    guard let pb = ball.physicsBody else { fatalError() }

    // decrease this to adjust the amount of speed gained each frame :)
    let amount = CGFloat(5)

    // When bouncing off a wall, speed decreases... this corrects that to _increase speed_ off bounces.
    if abs(pb.velocity.dx) < abs(initialDX) {
      if      pb.velocity.dx < 0 { pb.velocity.dx = -initialDX - amount }
      else if pb.velocity.dx > 0 { pb.velocity.dx =  initialDX + amount }
    }

    if abs(pb.velocity.dy) < abs(initialDY) {
      if      pb.velocity.dy < 0 { pb.velocity.dy = -initialDY - amount }
      else if pb.velocity.dy > 0 { pb.velocity.dy =  initialDY + amount }
    }
  }
}

您可以使用SKAction.repeatForever(.sequence([.wait(forDuration: TimeInterval, .run( { code } ))) 将其修改为仅每 5 秒提高一次速度,但 IMO 让它不断提高速度会更棒,更容易实现。

【讨论】:

  • 再次感谢,能不能也分享一下不断加速的代码
  • 这是@AmolKumar
猜你喜欢
  • 2018-02-22
  • 1970-01-01
  • 1970-01-01
  • 2015-06-16
  • 1970-01-01
  • 1970-01-01
  • 2017-08-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多