首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在iOS应用程序中编辑“射弹”的速度

如何在iOS应用程序中编辑“射弹”的速度
EN

Stack Overflow用户
提问于 2019-01-25 23:25:46
回答 1查看 59关注 0票数 0

当最高分上升30分时,我正在尝试编辑投射物的速度。然而,代码运行良好,游戏正常工作,但弹丸的移动速度仍然相同。

代码语言:javascript
复制
 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)

}
}
EN

回答 1

Stack Overflow用户

发布于 2019-01-25 23:45:51

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

除了在检查分数之前调用SKAction.move(to: realDest, duration: 2.0)之外,您已经尝试过的方法似乎是合理的。

我认为你最好这样做:

代码语言:javascript
复制
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

唯一的逻辑变化是模拟速度的移动持续时间,所以事先根据你的游戏逻辑计算出这个值,然后传入它

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54368241

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档