Skip to content

Instantly share code, notes, and snippets.

@ylem
Last active August 29, 2015 14:24
Show Gist options
  • Save ylem/fff766782bd20c4189d4 to your computer and use it in GitHub Desktop.
Save ylem/fff766782bd20c4189d4 to your computer and use it in GitHub Desktop.
Move SKSpriteNode by Bezier Curve Path
func BezierCurveMove(screenSize: CGSize, node: SKSpriteNode) {
let size = node.size
var cgpath = CGPathCreateMutable()
//random values
let xStart = Utility.randInRange(Int(size.width)...Int(screenSize.width-size.width))
let xEnd = Utility.randInRange(Int(size.width)...Int(screenSize.width-size.width))
//ControlPoint1
let cp1X = Utility.randInRange(Int(size.width)...Int(screenSize.width-size.width))
let cp1Y = Utility.randInRange(Int(size.width)...Int(screenSize.width-size.height))
//ControlPoint2
let cp2X = Utility.randInRange(Int(size.width)...Int(screenSize.width-size.width))
let cp2Y = Utility.randInRange(0...cp1Y)
let s = CGPointMake(CGFloat(xStart), screenSize.height + size.height)
let e = CGPointMake(CGFloat(xEnd), -node.size.height)
let cp1 = CGPointMake(CGFloat(cp1X), CGFloat(cp1Y))
let cp2 = CGPointMake(CGFloat(cp2X), CGFloat(cp2Y))
CGPathMoveToPoint(cgpath, nil, s.x, s.y)
CGPathAddCurveToPoint(cgpath, nil, cp1.x, cp1.y, cp2.x, cp2.y, e.x, e.y)
let anim = SKAction.followPath(cgpath, asOffset: false, orientToPath: true, duration: 10)
let remove = SKAction.removeFromParent()
node.runAction(SKAction.sequence([anim, remove]))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment