Created
July 29, 2014 01:18
-
-
Save rc-rogers/8c0f62e294284a05a6b7 to your computer and use it in GitHub Desktop.
Swift倉庫・SKActionでCJumpTo ref: http://qiita.com/RichQiitaJp/items/353dcaf62d136cc848a4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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