gpt4 book ai didi

swift - SpriteKit swift : How to make a sprite jump in place when tapped?

转载 作者:行者123 更新时间:2023-11-28 09:32:43 24 4
gpt4 key购买 nike

所以我一直在开发一款游戏,并且有一个正在运行的小 Sprite 。每当用户点击屏幕躲避障碍物时,我都需要让他跳起来。我还需要他跳完后回到地面上的起点。

最佳答案

你可以尝试这样的事情!

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

}

在播放器的属性检查器窗口中,确保只选择“动态”和“受重力影响”选项,否则它不会在跳跃后落回地面。:)

我不认为物理学对于一个简单的游戏来说会像以前的回答中有人说的那样过分...对于 iPhone 或 iPad 硬件来说这不是什么大问题,它们具有疯狂的计算能力。

enter image description here

关于swift - SpriteKit swift : How to make a sprite jump in place when tapped?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32334473/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com