如何制作一个在大小变化时遵循圆形路径的精灵?
How to make a sprite that follow a circular path while its size changes?
我已经让球沿着圆形路径运动,但唯一的问题是我希望当接触开始时路径变长并且球继续沿着路径运动。我尝试添加一个 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")
})
}
我认为您可以进行一些更正,以帮助您完成项目的其余部分。
下面这个在 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 的最后一个大括号之后,例如:
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"))
})
}
我已经让球沿着圆形路径运动,但唯一的问题是我希望当接触开始时路径变长并且球继续沿着路径运动。我尝试添加一个 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")
})
}
我认为您可以进行一些更正,以帮助您完成项目的其余部分。
下面这个在 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 的最后一个大括号之后,例如:
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"))
})
}