In my app I have a Sprite circling around another sprite. I want this sprite to speed up when the screen is held down. I have achieved the speed up function, but I cannot seem to figure out why the sprite resets back to a certain position when the screen is clicked and after it is let go. I have a feeling I know why, but I have tried to fix it and I cannot seem to get any closer to a solution. Code below.
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())
}
Side Note: you shouldnt use uppercase on variables. Uppercase is reserved for class Names..
for example:
let slider = Slider()
it's a naming convention thats used across pretty much all programming languages :)
Anyway.. try doing
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())
}
}