了一段時間,但這裏的東西:
import SpriteKit
class GameScene: SKScene {
var sprite = SKSpriteNode()
override func didMove(to view: SKView) {
for _ in 1...15 {
spawnsprite()
}
}
func spawnsprite() {
sprite = SKSpriteNode(color: SKColor.yellow, size: CGSize(width: 50, height: 50))
sprite.position = CGPoint(x: (-size.width/2)+50, y: (-size.height/2)+50)
addChild(sprite)
let destination = CGPoint(x: (size.width/2)-50, y: (size.height/2)-50)
let path = createCurvedPath(from: sprite.position, to: destination, varyingBy: 500)
let squareSpeed = CGFloat(arc4random_uniform(600)) + 200
let moveAction = SKAction.follow(path, asOffset: false, orientToPath: false, speed: squareSpeed)
let rotateAction = SKAction.repeatForever(SKAction.rotate(byAngle: 2 * CGFloat.pi, duration: 2))
sprite.run(SKAction.group([moveAction, rotateAction]))
}
func createCurvedPath(from start: CGPoint, to destination: CGPoint, varyingBy offset: UInt32) -> CGMutablePath {
let pathToMove = CGMutablePath()
pathToMove.move(to: start)
let controlPoint = CGPoint(x: CGFloat(arc4random_uniform(offset)) - CGFloat(offset/2),
y: CGFloat(arc4random_uniform(offset)) - CGFloat(offset/2))
pathToMove.addQuadCurve(to: destination, control: controlPoint)
return pathToMove
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
}
}
有可以做,使這個更好的(如子SKSpriteNode,使道路的頌歌跟隨精靈本身的屬性)很多,但它說明了原理。
它可能也可以用SKEmitter來完成。
請發佈您迄今爲止的代碼,並說明您不希望它做什麼。 –
您好Steve,我一直在SKAction工作,但我不知道這是正確的解決方案?我不尋求幫助來調試任何代碼,只是一個方向指針:) – thar
然後,你可能想看看這個問題,它的答案 - http://stackoverflow.com/q/43960262/1430420它也是關於創建新的並將它們移動到屏幕的特定部分。儘管如果您沒有說出問題是增加一顆恆星,或者添加多顆恆星,或使它們移動,或使它們以某種方式移動,則很難提供幫助。你可能需要幫助創建一個新的精靈 –