Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 快速-使用SpriteKit提高移动节点的速度_Ios_Swift_Sprite Kit_Game Physics_Skphysicsbody - Fatal编程技术网

Ios 快速-使用SpriteKit提高移动节点的速度

Ios 快速-使用SpriteKit提高移动节点的速度,ios,swift,sprite-kit,game-physics,skphysicsbody,Ios,Swift,Sprite Kit,Game Physics,Skphysicsbody,我正在做一个类似于乒乓球的游戏,在开始游戏时,我在屏幕中央的随机方向上对球施加冲动,例如: func impulse(){ let randomNum:UInt32 = arc4random_uniform(200) let someInt:Int = Int(randomNum) //Ball Impulse if someInt<49{ ball.physicsBody?.applyImpulse(CGVector(dx: ballSpeed+

我正在做一个类似于乒乓球的游戏,在开始游戏时,我在屏幕中央的随机方向上对球施加冲动,例如:

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

}
func脉冲(){
让randomNum:UInt32=arc4random_均匀(200)
让someInt:Int=Int(randomNum)
//球冲量

如果someInt好,开始吧!无论发生什么情况,球都会不断变快!!!调整
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 }
    }
  }
}
类游戏场景:SKScene,skphysiccontactdelegate{
let ball=SKShapeNode(半径:20)
var initialDY=CGFloat(0)
var initialDX=CGFloat(0)
覆盖func didMove(到视图:SKView){
self.physicsBody=SKPhysicsBody(edgeLoopFrom:frame)
self.physicsWorld.gravity=CGVector.zero
//配置球pb:
设pb=SKPhysicsBody(圆形半径:20)
ball.physicsBody=pb
addChild(球)
pb.应用脉冲(CGVector(dx:10,dy:10))
}
覆盖函数更新(uCurrentTime:TimeInterval){
initialDX=abs(ball.physicsBody!.velocity.dx)
initialDY=abs(ball.physicsBody!.velocity.dy)
}
重写func didSimulatePhysics(){
guard let pb=ball.physicsBody else{fatalError()}
//减小此值可调整每帧获得的速度:)
let amount=CGFloat(5)
//当从墙上反弹时,速度会降低…这修正了“增加速度”的反弹。
如果abs(pb.velocity.dx)0{pb.velocity.dx=initialDX+amount}
}
如果abs(pb.velocity.dy)0{pb.velocity.dy=initialDY+amount}
}
}
}

您可以通过
SKAction.repeatForever(.sequence([.wait(forDuration:TimeInterval,.run({code}))修改此选项,使其仅每5秒增加一次速度
但是我认为让它不断地提高速度更令人敬畏,也更容易实现。

如果精灵是通过物理运动的,那么它的
速度
属性将有一个值。这是一个向量,所以你可以使用速度作为脉冲的向量来应用另一个脉冲来提高它的速度。你知道你的inteRVAL是不均匀的?你的第一个间隔从0到48,下一个从49到98,下一个从99到148,最后一个从149到200,这意味着前3个范围只有49个值宽,最后一个是51。@user1118321-事实上,我更喜欢比赛开始时球从第三象限开始,所以这就像我额外加2分一样在这里,当executed@SteveIves-在这里,在这种情况下,我试图每5秒增加2个单位的球速,那么如何使用
ball.physicsBody?.applyImpulse((ball.physicsBody?.velocity)!)
再次感谢,您能分享恒速齿的代码吗?是@AmolKumar吗