Skip to content

Instantly share code, notes, and snippets.

@rc-rogers
Created July 29, 2014 01:18
Show Gist options
  • Save rc-rogers/8c0f62e294284a05a6b7 to your computer and use it in GitHub Desktop.
Save rc-rogers/8c0f62e294284a05a6b7 to your computer and use it in GitHub Desktop.
Swift倉庫・SKActionでCJumpTo ref: http://qiita.com/RichQiitaJp/items/353dcaf62d136cc848a4
import SpriteKit
class CJumpTo: SKNode {
init(sprite: SKSpriteNode, targetPoint: CGPoint, height: CGFloat, duration: NSTimeInterval) {
super.init()
let startPoint = sprite.position
var finalPath: CGPath!
var controlPoint = CGPoint()
controlPoint.x = startPoint.x + (targetPoint.x - startPoint.x)/2
controlPoint.y = startPoint.y + height
#if os(iOS)
// iOS Support
let bezierPath: UIBezierPath = UIBezierPath()
bezierPath.moveToPoint(startPoint)
bezierPath.addQuadCurveToPoint(targetPoint, controlPoint: controlPoint)
finalPath = bezierPath.CGPath
#else
// OSX Support
var path = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, startPoint.x, startPoint.y)
CGPathAddQuadCurveToPoint(path, nil, controlPoint.x, controlPoint.y, targetPoint.x, targetPoint.y)
finalPath = path
#endif
let jumpAction = SKAction.followPath(finalPath, asOffset:false, orientToPath:false, duration: duration)
jumpAction.timingMode = .EaseIn
let scaleA = SKAction.scaleTo(1.3, duration: (duration/4)*3)
let scaleB = SKAction.scaleTo(1.0, duration: (duration/4)*1)
let scaleSequence = SKAction.sequence([scaleA,scaleB])
let sequence = SKAction.group([jumpAction, scaleSequence])
sprite.runAction(sequence)
}
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
let previousLocation = touch.previousLocationInNode(self)
CJumpTo(sprite: theSprite, targetPoint: location, height: 50, duration: 1.0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment