我想首先从屏幕中心向左移动树节点,然后从右到左移动树节点。它应该看起来像无限量的树木。我对第一部分没有任何问题。这是代码:
let tree = SKSpriteNode (imageNamed: "oak")
tree.position = CGPointMake(self.size.width / 2, self.size.height / 2)
self.addChild(tree)
var move = SKAction.moveToX(150, duration: 1)
tree.runAction(move)
但如何做第二部分?我认为唯一可行的方法是制作另一个具有相同图像的SKSpriteNode并给它另一个起始位置。
但我觉得必须有更好的解决方案!
我明白了。也许,根本不优雅,但它有效!
func newPlace() {
tree.position = CGPointMake(self.size.width + 100, self.size.height / 2)
}
override func didMoveToView(view: SKView) {
tree.position = CGPointMake(self.size.width / 2, self.size.height / 2)
self.addChild(tree)
var move = SKAction.moveToX(150, duration: 1)
var place = SKAction.runBlock(newPlace)
var moveAlong = SKAction.moveToX(100, duration: 2)
var seq = SKAction.sequence([place, moveAlong])
var repeat = SKAction.repeatActionForever(seq)
var sequence = SKAction.sequence([move, place, moveAlong, seq, repeat])
tree.runAction(sequence)
}
也许,它会帮助别人。
答案 0 :(得分:1)
我根本不使用Swift,但是这个简单的例子应该解释如何将精灵从屏幕的一侧移动到另一侧,让所有看起来无穷无尽:
import SpriteKit
class GameScene: SKScene {
let tree = SKSpriteNode(color: SKColor .greenColor(), size: CGSizeMake(20,30))
override func didMoveToView(view: SKView) {
println(self.frame.size)
tree.position = CGPointMake(self.frame.size.width + self.tree.size.width, CGRectGetMidY(self.frame))
var move = SKAction.moveToX(-tree.size.width, duration: 3)
var reset = SKAction.runBlock(){
self.tree.position = CGPoint(x: self.frame.size.width+self.tree.size.width, y: self.tree.position.y)
}
var wait = SKAction.waitForDuration(0.5) // Here you can randomize. This will make animation to wait before starts
var sequence = SKAction.sequence([wait,move,reset])
self.tree.runAction(SKAction.repeatActionForever(sequence))
self.addChild(tree)
}
}
我认为您的场景尺寸设置正确,因此当您复制/粘贴并运行此代码时,您应该看到所需的结果(它适用于我)。关于两个数字之间的随机搜索搜索SO网站,有很多帖子描述该主题。