Skip to content

Instantly share code, notes, and snippets.

@christopherkarani
Last active September 28, 2017 20:43
Show Gist options
  • Save christopherkarani/a97c42ed5305b8283438bd57709983a1 to your computer and use it in GitHub Desktop.
Save christopherkarani/a97c42ed5305b8283438bd57709983a1 to your computer and use it in GitHub Desktop.
Make A SpriteNode Move along a certain Path
//: A SpriteKit based Playground
import PlaygroundSupport
import SpriteKit
extension CGPoint {
static func CenterOfScene(_ scene: GameScene) -> CGPoint {
return CGPoint(x: scene.size.width / 2, y: scene.size.height / 2)
}
}
class GameScene: SKScene {
private var player : SKShapeNode?
var squarePath : SKAction = {
let action = SKAction()
let square = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 200, height: 200))
let path : CGPath = square.cgPath
return SKAction.follow(path, duration: 3)
}()
var circularPath : SKAction = {
let action = SKAction()
let rect = CGRect(x: 0, y: 0, width: 200, height: 200)
let circle = UIBezierPath(roundedRect: rect, cornerRadius: 100)
let path = circle.cgPath
return SKAction.follow(path, duration: 3)
}()
override func didMove(to view: SKView) {
player = SKShapeNode(rectOf: CGSize(width: 70, height: 70))
player?.position = CGPoint.CenterOfScene(self)
addChild(player!)
}
func touchDown(atPoint pos : CGPoint) {
player?.run(SKAction.repeatForever(SKAction.sequence([squarePath, circularPath, squarePath.reversed(), circularPath.reversed()])))
}
}
// Load the SKScene from 'GameScene.sks'
let sceneView = SKView(frame: CGRect(x:0 , y:0, width: 640, height: 480))
let scene = GameScene(size: sceneView.frame.size)
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
sceneView.presentScene(scene)
PlaygroundSupport.PlaygroundPage.current.liveView = sceneView
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment