好的。這段代碼讓我瘋狂。它只是不起作用。我收到的唯一消息是「試圖添加一個已經有父母的SKNode」。是的,我知道這裏已經有過一些討論,但是他們都沒有提供我需要的解決方案。試圖添加一個已經有父母的SKNode - Swift
這是代碼。我非常感謝任何幫助。
import SpriteKit
class MyScene: SKScene {
let intervalShapeCreation:NSTimeInterval = 2.0 // Interval for creating the next Shape
let gravitationalAcceleration:CGFloat = -0.5 // The gravitational Y acceleration
let shapeSequenceAction = SKAction.sequence([
SKAction.scaleTo(1.0, duration: 0.5),
SKAction.waitForDuration(2.0),
SKAction.scaleTo(0, duration: 0.5),
SKAction.removeFromParent()
])
override init(size: CGSize) {
super.init(size: size)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func didMoveToView(view: SKView) {
super.didMoveToView(view)
addBackground()
initializeScene()
}
// MARK: Level Building
func initializeScene() {
self.physicsWorld.gravity = CGVectorMake(0.0, gravitationalAcceleration)
runAction(SKAction.repeatActionForever(
SKAction.sequence([SKAction.runBlock(self.createShape),
SKAction.waitForDuration(intervalShapeCreation)])))
}
func addBackground() {
let backgroundAtlas = SKTextureAtlas(named: "background")
let background = SKSpriteNode(texture: backgroundAtlas.textureNamed("background"))
background.position = CGPoint(x: size.width/2, y: size.height/2)
background.anchorPoint = CGPointMake(0.5, 0.5)
background.zPosition = -1
background.name = "background"
self.addChild(background)
}
func createShape() {
let newShape = sSharedAllPossibleShapes[0]
print("\n shape creada: \(newShape.name)")
newShape.position = CGPointMake(size.width/2, CGFloat(Int.random(fromZeroToMax: 500)))
self.addChild(newShape)
newShape.runAction(shapeSequenceAction)
}
}
你能解釋一下「不行」嗎?請嘗試闡明預期的行爲。 – tjameson 2014-10-04 00:36:11
好的。它必須做的是在屏幕上以相同的x位置和隨機的y位置顯示一些圖形。這種形狀必須持續2秒,然後消失並移除。它可以創建第一個沒有問題,但其餘停止與「試圖添加一個已經有父母的SKNode」相同的不方便。 – 2014-10-04 00:49:57