gpt4 book ai didi

swift - SKAction 遵循 UIBezierPath 函数不起作用 SpriteKit - Swift

转载 作者:行者123 更新时间:2023-11-30 13:25:22 24 4
gpt4 key购买 nike

在我的应用程序中,我有一个 Sprite 围绕另一个 Sprite 旋转。我希望这个 Sprite 在按住屏幕时加速。我已经实现了加速功能,但我似乎无法弄清楚为什么当单击屏幕和松开屏幕时 Sprite 会重置回某个位置。我有一种感觉,我知道为什么,但我已经尝试修复它,但似乎无法更接近解决方案。代码如下。

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

let dx = Slider.accessibilityActivationPoint.x
let dy = Slider.accessibilityActivationPoint.y

let rad = atan2(dx, dy)

let Path = UIBezierPath(arcCenter: CGPoint(x: self.frame.width / 2, y: self.frame.height / 2), radius: 90, startAngle: rad, endAngle: rad + CGFloat(M_PI * 4), clockwise: true)

let follow = SKAction.followPath(Path.CGPath, asOffset: false, orientToPath: true, speed: 400)
//let rotate = SKAction.rotateByAngle(75, duration: 100)

Slider.runAction(SKAction.repeatActionForever(follow).reversedAction())
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {


let dx = Slider.accessibilityActivationPoint.x
let dy = Slider.accessibilityActivationPoint.y

let rad = atan2(dy, dx)

let Path = UIBezierPath(arcCenter: CGPoint(x: self.frame.width / 2, y: self.frame.height / 2), radius: 90, startAngle: 9, endAngle: rad + CGFloat(M_PI * 4), clockwise: true)

let follow = SKAction.followPath(Path.CGPath, asOffset: false, orientToPath: true, speed: 150)


Slider.runAction(SKAction.repeatActionForever(follow).reversedAction())
}

func moveClockWise(){

let dx = Slider.position.x / 2
let dy = Slider.position.y / 2

let rad = atan2(dy, dx)


let Path = UIBezierPath(arcCenter: CGPoint(x: self.frame.width / 2, y: self.frame.height / 2), radius: 90, startAngle: rad, endAngle: rad + CGFloat(M_PI * 4), clockwise: true)

let follow = SKAction.followPath(Path.CGPath, asOffset: false, orientToPath: true, speed: 150)
//let rotate = SKAction.rotateByAngle(75, duration: 100)

Slider.runAction(SKAction.repeatActionForever(follow).reversedAction())
//Slider.runAction(SKAction.repeatActionForever(rotate).reversedAction())
}

最佳答案

旁注:变量不应该使用大写。大写字母保留用于类名称..

例如:

let slider = Slider()

这是几乎所有编程语言都使用的命名约定:)

无论如何..尝试一下

import SpriteKit

class GameScene: SKScene {

let Slider = SKSpriteNode(color: SKColor.redColor(), size: CGSizeMake(10, 10))

override func didMoveToView(view: SKView) {
Slider.position = CGPointMake(size.width/2, size.height/2)
addChild(Slider)
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {


let dx = Slider.accessibilityActivationPoint.x
let dy = Slider.accessibilityActivationPoint.y

let rad = atan2(dx, dy)

let Path = UIBezierPath(arcCenter: CGPoint(x: self.frame.width / 2, y: self.frame.height / 2), radius: 90, startAngle: rad, endAngle: rad + CGFloat(M_PI * 4), clockwise: true)

let follow = SKAction.followPath(Path.CGPath, asOffset: false, orientToPath: true, speed: 400)
//let rotate = SKAction.rotateByAngle(75, duration: 100)

if !Slider.hasActions() {
Slider.runAction(SKAction.repeatActionForever(follow).reversedAction())
} else {
Slider.runAction(SKAction.speedTo(1, duration: 0))
}



}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
Slider.runAction(SKAction.speedTo(0.4, duration: 0))

}

func moveClockWise(){

let dx = Slider.position.x / 2
let dy = Slider.position.y / 2

let rad = atan2(dy, dx)


let Path = UIBezierPath(arcCenter: CGPoint(x: self.frame.width / 2, y: self.frame.height / 2), radius: 90, startAngle: rad, endAngle: rad + CGFloat(M_PI * 4), clockwise: true)

let follow = SKAction.followPath(Path.CGPath, asOffset: false, orientToPath: true, speed: 150)
//let rotate = SKAction.rotateByAngle(75, duration: 100)

Slider.runAction(SKAction.repeatActionForever(follow).reversedAction())
//Slider.runAction(SKAction.repeatActionForever(rotate).reversedAction())

}
}

关于swift - SKAction 遵循 UIBezierPath 函数不起作用 SpriteKit - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37281242/

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