##【CJumpToアニメーション】
簡単なJumpToを書いてみました。これにジャンプ目的地までの間に何回ジャンプするのかなどCocos2DのCCJumpTo、CCJumpBy的な機能をかければと思っています。
CJumpTo.swift
import SpriteKit
class CJumpTo: SKNode {
init(sprite: SKSpriteNode, targetPoint: CGPoint, height: CGFloat, duration: NSTimeInterval) {
super.init()
let startPoint = sprite.position
var bezierPath: UIBezierPath = UIBezierPath()
bezierPath.moveToPoint(startPoint)
var controlPoint: CGPoint = CGPoint()
controlPoint.x = startPoint.x + (targetPoint.x - startPoint.x)/2
controlPoint.y = startPoint.y + height
bezierPath.addQuadCurveToPoint(targetPoint, controlPoint: controlPoint)
let jumpAction = SKAction.followPath(bezierPath.CGPath, asOffset:false, orientToPath:false, duration: 0.2)
jumpAction.timingMode = .EaseIn
let scaleA = SKAction.scaleTo(1.2, duration: 0.1)
let scaleB = SKAction.scaleTo(1.0, duration: 0.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)
}
}
##【CJumpTo・iOS/OSX対応】
OSXとiOSの両側で使えるバージョンも作ってみました。
CJumpTo.swift
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)
}
}