我可以按如下方式创建自定义SKAction:
extension SKAction {
class func move(from: CGPoint, halfWayTo: CGPoint, duration: TimeInterval) -> SKAction {
let midPoint = CGPoint(x: (from.x + halfWayTo.x)/2, y: (from.y + halfWayTo.y)/2 )
return SKAction.move(to: midPoint, duration: duration)
}
}
然后我这样使用:
//Setup
let node = SKNode()
node.position = CGPoint(x: 50, y: 50)
let destination = CGPoint(x: 100, y: 100)
//Move from node's current position half way towards the destination
let action = SKAction.move(from: node.position, halfWayTo: destination, duration: 1)
node.run(action)
但符合标准:
SKAction.move(to: destination, duration: 1)
我宁愿能够说:
let action = SKAction.move(halfWayTo: destination, duration: 1)
node.run(action)
但我不知道如何参考节点'从自定义SKAction内部,以便我可以获得它的位置并计算中途点?
答案 0 :(得分:0)
您使用self
。
extension SKAction {
class func move(halfWayTo: CGPoint, duration: TimeInterval) -> SKAction {
let from = self.position
let midPoint = CGPoint(x: (from.x + halfWayTo.x)/2, y: (from.y + halfWayTo.y)/2 )
return SKAction.move(to: midPoint, duration: duration)
}
}
然后打电话(如你所说)
node.move(halfWayTo: B, duration: 1)
答案 1 :(得分:0)
我仍然不确定SKAction是否会引用它所应用的节点 - 即使它正在运行 - 或者即使它确实存在 - 我也不确定是否存在&#39 ;访问它的方式。如果有人知道确实如此,请告诉我。
我想到了一种方法如下:
不是扩展SKAction,而是按如下方式扩展SKNode:
extension SKNode {
func move(halfWayTo: CGPoint, duration: TimeInterval) -> SKAction {
let from = self.position
let midPoint = CGPoint(x: (from.x + halfWayTo.x)/2, y: (from.y + halfWayTo.y)/2 )
return SKAction.move(to: midPoint, duration: duration)
}
}
然后使用如下:
//Setup
let node = SKNode()
node.position = CGPoint(x: 50, y: 50)
let destination = CGPoint(x: 100, y: 100)
//Move from node's current position half way towards the destination
let action2 = node.move(halfWayTo: destination, duration: 1)
node.run(action2)