SpriteKit Swift:点击时如何让精灵原地跳跃?
SpriteKit Swift: How to make a sprite jump in place when tapped?
所以我一直在开发一款游戏,并且有一个小精灵目前 运行 就位。每当用户点击屏幕躲避障碍物时,我都需要让他跳起来。我还需要他跳完后return回到地面上的起点
对于这样的游戏,使用物理学是矫枉过正的。甚至 2D 马里奥游戏也不使用物理学,所以我怀疑你是否需要它。
作为一个非常简单的实现,您可以使用一系列 SKAction
s。
// move up 20
let jumpUpAction = SKAction.moveBy(x: 0, y: 20, duration: 0.2)
// move down 20
let jumpDownAction = SKAction.moveBy(x: 0, y: -20, duration: 0.2)
// sequence of move yup then down
let jumpSequence = SKAction.sequence([jumpUpAction, jumpDownAction])
// make player run sequence
player.run(jumpSequence)
应该可以。不过,我可能在浏览器中输入了错误的内容。
您可以对此进行调整,使跳跃看起来更自然。也许在跳跃过程中再增加几步,以随着时间的推移改变 rise/fall 的速度,使其看起来像是在加速。等...
你可以试试这样的方法!
import SpriteKit
class GameScene: SKScene {
var player: SKSpriteNode?
override func didMove(to view: SKView) {
player = self.childNode(withName: "player") as? SKSpriteNode
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches { self.touchDown(atPoint: t.location(in: self)) }
}
func touchDown(atPoint pos: CGPoint) {
jump()
}
func jump() {
player?.texture = SKTexture(imageNamed: "player_jumping")
player?.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 500))
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches { self.touchUp(atPoint: t.location(in: self)) }
}
func touchUp(atPoint pos: CGPoint) {
player?.texture = SKTexture(imageNamed: "player_standing")
}
}
在玩家的属性检查器window上确保你只有select'Dynamic'和'Affected By Gravity'选项,否则跳跃后它不会掉回地面。 .:)
我不认为物理对于一个简单的游戏来说会像以前的回答中有人说的那样过分...对于 iPhone 或 iPad 硬件来说这不是什么大问题,他们有疯狂的计算能力。
所以我一直在开发一款游戏,并且有一个小精灵目前 运行 就位。每当用户点击屏幕躲避障碍物时,我都需要让他跳起来。我还需要他跳完后return回到地面上的起点
对于这样的游戏,使用物理学是矫枉过正的。甚至 2D 马里奥游戏也不使用物理学,所以我怀疑你是否需要它。
作为一个非常简单的实现,您可以使用一系列 SKAction
s。
// move up 20
let jumpUpAction = SKAction.moveBy(x: 0, y: 20, duration: 0.2)
// move down 20
let jumpDownAction = SKAction.moveBy(x: 0, y: -20, duration: 0.2)
// sequence of move yup then down
let jumpSequence = SKAction.sequence([jumpUpAction, jumpDownAction])
// make player run sequence
player.run(jumpSequence)
应该可以。不过,我可能在浏览器中输入了错误的内容。
您可以对此进行调整,使跳跃看起来更自然。也许在跳跃过程中再增加几步,以随着时间的推移改变 rise/fall 的速度,使其看起来像是在加速。等...
你可以试试这样的方法!
import SpriteKit
class GameScene: SKScene {
var player: SKSpriteNode?
override func didMove(to view: SKView) {
player = self.childNode(withName: "player") as? SKSpriteNode
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches { self.touchDown(atPoint: t.location(in: self)) }
}
func touchDown(atPoint pos: CGPoint) {
jump()
}
func jump() {
player?.texture = SKTexture(imageNamed: "player_jumping")
player?.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 500))
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches { self.touchUp(atPoint: t.location(in: self)) }
}
func touchUp(atPoint pos: CGPoint) {
player?.texture = SKTexture(imageNamed: "player_standing")
}
}
在玩家的属性检查器window上确保你只有select'Dynamic'和'Affected By Gravity'选项,否则跳跃后它不会掉回地面。 .:)
我不认为物理对于一个简单的游戏来说会像以前的回答中有人说的那样过分...对于 iPhone 或 iPad 硬件来说这不是什么大问题,他们有疯狂的计算能力。