如何在iOS应用中编辑“弹丸”的速度

时间:2019-01-25 15:25:46

标签: ios swift sprite-kit

我试图在高分上升30时编辑弹丸的速度。但是,代码运行正常,游戏正常运行,但是弹丸的移动速度仍然相同。 / p>

 let touchLocation = touch.location(in: self)

    // Setting up the initial location of projectile
    let particle2 = SKEmitterNode(fileNamed: "smoke.sks")
    let projectile = SKSpriteNode(imageNamed: "inky")
    projectile.position = player.position
    particle2!.targetNode = self
    projectile.addChild(particle2!)

    projectile.physicsBody = SKPhysicsBody(circleOfRadius: projectile.size.width/2)
    projectile.physicsBody?.isDynamic = true
    projectile.physicsBody?.categoryBitMask = PhysicsCategory.projectile
    projectile.physicsBody?.contactTestBitMask = PhysicsCategory.monster
    projectile.physicsBody?.collisionBitMask = PhysicsCategory.none
    projectile.physicsBody?.usesPreciseCollisionDetection = true

    // Determine offset of location to projectile
    let offset = touchLocation - projectile.position

    // Bail out if you are shooting down or backwards
    if offset.x < 0 { return }

    // OK to add now - you've double checked position
    addChild(projectile)

    // Get the direction of where to shoot
    let direction = offset.normalized()

    // Make it shoot far enough to be guaranteed off screen
    let shootAmount = direction * 1000

    // Add the shoot amount to the current position
    let realDest = shootAmount + projectile.position

    // Create the actions
    let actionMove = SKAction.move(to: realDest, duration: 2.0)
    let actionMoveDone = SKAction.removeFromParent()
    projectile.run(SKAction.sequence([actionMove, actionMoveDone]))

     if UserDefaults().integer(forKey: "highscore") >= 30 {
        let actionMove = SKAction.move(to: realDest, duration: 1.0)

}
}

1 个答案:

答案 0 :(得分:0)

免责声明:我根本不是SpriteKit或iOS游戏的专家。很有可能会有更好的解决方案。

除了在检查分数之前致电SKAction.move(to: realDest, duration: 2.0)以外,您尝试过的尝试似乎是明智的。

我认为您最好这样做:

let highScore = UserDefaults().integer(forKey: "highscore")
let duration: CGFloat = highScore >= 30 ? 1.0 : 2.0
let actionMove = SKAction.move(to: realDest, duration: duration)
// add move end, run the animation

唯一的逻辑更改是模拟速度的运动持续时间,因此请根据您的游戏逻辑事先确定此值,然后将其传入