我已经确定球跟随一条圆形路径,但唯一的问题是我想要在接触开始时路径增长并且球继续跟随路径。我尝试添加SKAction
,但球不遵循路径。希望有人可以帮助我。
class GameScene: SKScene {
var circuloPrincipal = SKSpriteNode(imageNamed: "circulo")
var circuloFondo = SKSpriteNode(imageNamed: "circuloFondo")
override func didMoveToView(view: SKView) {
circuloPrincipal.size = CGSize(width: 35, height: 35)
circuloPrincipal.position = CGPoint(x: frame.width / 2, y: frame.height / 2 - 105)
circuloPrincipal.color = UIColor(red: 0.2, green: 0.9, blue: 0.6, alpha: 1.0)
circuloPrincipal.colorBlendFactor = 1.0
circuloPrincipal.zPosition = 3.0
circuloFondo.size = CGSize(width: 300, height: 300)
circuloFondo.position = CGPoint(x: frame.width / 2, y: frame.height / 2)
self.addChild(circuloFondo)
circuloFondo.color = UIColor(red: 0.2, green: 0.9, blue: 0.6, alpha: 1.0)
circuloFondo.colorBlendFactor = 1.0
circuloFondo.zPosition = 1.0
circuloPrincipal.position = CGPointMake( CGRectGetMidX(frame) , (CGRectGetMidY(frame) + circuloFondo.size.width/2) )
addChild(circuloPrincipal)
let dx = circuloPrincipal.position.x - frame.width / 2
let dy = circuloPrincipal.position.y - frame.height / 2
let radius = (circuloFondo.frame.size.width / 2) - 20.0
let radian = atan2(dy, dx)
let playerPath = UIBezierPath(
arcCenter: CGPoint(x: CGRectGetMidX(frame), y: CGRectGetMidY(frame)),
radius: radius,
startAngle: radian,
endAngle: radian + CGFloat(M_PI * 4.0),
clockwise: true)
let follow = SKAction.followPath(playerPath.CGPath, asOffset: false, orientToPath: true, speed: 200)
circuloPrincipal.runAction(SKAction.repeatActionForever(follow))
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
circuloFondo.runAction(SKAction.scaleTo(0.5, duration: 5))
}
//新代码
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
//fondoTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("fondoEfecto"), userInfo: nil, repeats: true)
if (circuloFondo.actionForKey("scaleTo") == nil) {
let scale = SKAction.scaleTo(0.5, duration: 5)
circuloFondo.runAction(scale,withKey:"scaleTo",optionalCompletion: {
//here the error occours
// scaleTo is terminated
// stop followTrackPath
self.circuloPrincipal.removeActionForKey("followTrackForever")
// re-run in the new path
let follow = SKAction.followPath(playerPath.CGPath, asOffset: false, orientToPath: true, speed: 200)
self.circuloPrincipal.runAction(SKAction.repeatActionForever(follow),withKey: "followTrackForever")
})
}
答案 0 :(得分:2)
我认为你可以做一些更正,可以帮助你完成项目的其余部分。
以下适用于Swift 2.x,请勿使用Swift 3.x:
首先,我想介绍一下这个有用的扩展:
extension SKNode
{
func runAction( action: SKAction!, withKey: String!, optionalCompletion: dispatch_block_t? )
{
if let completion = optionalCompletion
{
let completionAction = SKAction.runBlock( completion )
let compositeAction = SKAction.sequence([ action, completionAction ])
runAction( compositeAction, withKey: withKey )
}
else
{
runAction( action, withKey: withKey )
}
}
func actionForKeyIsRunning(key: String) -> Bool {
if self.actionForKey(key) != nil {
return true
} else {
return false
}
}
}
<强>用法强>:
self.runAction(myAction,withKey: "myActionName",optionalCompletion: {
// do whatever you want when action is finished..
})
if self.actionForKeyIsRunning("myActionName") {
// this action is up
}
放置此扩展程序的位置:
您也可以将此扩展名放在班级的最后一个大括号之后,例如:
class MyClass: SKScene {
...
} // end of MyClass
extension SKNode
{
...
}
这一行可能成为:
circuloPrincipal.runAction(SKAction.repeatActionForever(follow),withKey: "followTrackForever"))
并且scaleTo
行可以是:
if !circuloFondo.actionForKeyIsRunning("scaleTo") {
let scale = SKAction.scaleTo(0.5, duration: 5)
circuloFondo.runAction(scale,withKey:"scaleTo",optionalCompletion: {
// scaleTo is terminated
// stop followTrackPath
self.circuloPrincipal.removeActionForKey("followTrackForever")
// re-run in the new path
let follow = SKAction.followPath(playerPath.CGPath, asOffset: false, orientToPath: true, speed: 200)
self.circuloPrincipal.runAction(SKAction.repeatActionForever(follow),withKey: "followTrackForever"))
})
}