当最高分上升30分时,我正在尝试编辑投射物的速度。然而,代码运行良好,游戏正常工作,但弹丸的移动速度仍然相同。
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)
}
}发布于 2019-01-25 23:45:51
免责声明:我根本不是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唯一的逻辑变化是模拟速度的移动持续时间,所以事先根据你的游戏逻辑计算出这个值,然后传入它
https://stackoverflow.com/questions/54368241
复制相似问题