0

Ok。このコードは私を夢中にさせています。それはうまくいきません。私が受け取った唯一のメッセージは、「既に親を持つ SKNode を追加しようとしました」です。はい、ここでいくつかの議論があったことは知っていますが、どれも私が必要とする解決策を提供していません.

これがコードです。どんな助けでも本当に感謝しています。

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)
  }

}
4

1 に答える 1

1

createShape は、実際には SKShapeNode を作成しません。sSharedAllPossibleShapes 配列から最初の形状を取得し、それを子として自分自身に追加します。このメソッドを 2 回目に呼び出すと、その形状には既に親があり、再度追加することはできません。

SKShapeNode の新しいインスタンスを作成する必要があります。ここでの配列には、意図したとおりにノードを再利用できないため、ノード自体ではなく、形状を定義する CGPath オブジェクトが実際に含まれている必要があります。

于 2014-10-04T07:55:25.063 に答える