gpt4 book ai didi

swift - 使用 Swift 停止一个球 Sprite 套件

转载 作者:行者123 更新时间:2023-11-28 09:02:05 25 4
gpt4 key购买 nike

我需要知道如何让球在两三秒后减速以施加冲量

func setupPlayer(){
player = SKSpriteNode(imageNamed: "ball")
player.anchorPoint = CGPoint(x: 0.5, y: 0.5)
player.position = CGPoint(x: size.width/2, y: playableStart)
let scale: CGFloat = 0.07
player.xScale = scale
player.yScale = scale
player.physicsBody = SKPhysicsBody(circleOfRadius: player.size.width / 2)
worlNode.addChild(player)
}

func movePlayer(){
player.physicsBody?.applyImpulse((CGVectorMake( 50, 50)))
}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch in (touches as! Set<UITouch>) {
movePlayer()
}
}

最佳答案

使物理物体减速的一种方法是随时间降低其速度:

override func update(currentTime: CFTimeInterval) {
// This controls how fast/slow the body decelerates
let slowBy:CGFloat = 0.95
let dx = player.physicsBody!.velocity.dx
let dy = player.physicsBody!.velocity.dy
player.physicsBody?.velocity = CGVectorMake(dx * slowBy, dy * slowBy)
}

关于swift - 使用 Swift 停止一个球 Sprite 套件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31732880/

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