简体   繁体   English

forChild在for循环中的时间延迟导致错误! 迅速

[英]Time delay for addChild in for loop is causing error! Swift

This loop I have works fine, but I want to have the images from the arrays come in one after the other with a specific set amount of time between them. 我执行的此循环工作正常,但我想让数组中的图像一个接一个地传入,并在它们之间设置特定的时间。

if transitionSprite.name == nil || transitionSprite.name == "rd-d2c" || transitionSprite.name == "rd-f2c" {
        for (index, roadImage) in cityArrays[randomIndex].enumerated() {

            roadSprite = SKSpriteNode(imageNamed: roadImage)
            roadSprite.anchorPoint = CGPoint(x: 0.5, y: 0.5)
            roadSprite.position = CGPoint(x: 0, y: CGFloat(screenCounter) * roadSprite.size.height)
            roadSprite.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: self.size.width, height: self.size.height))
            roadSprite.physicsBody?.categoryBitMask = PhysicsCategory.RoadImageCategory.rawValue
            roadSprite.physicsBody?.collisionBitMask = PhysicsCategory.NoCollisionsCategory.rawValue
            roadSprite.physicsBody?.contactTestBitMask = PhysicsCategory.BroomCategory.rawValue
            roadSprite.physicsBody?.affectedByGravity = false

            roadSprite.zPosition = 1
            roadSprite.name = "Road \(index)"

            // RUNNING THE SKACTION DELAY ON THIS
            self.addChild(roadSprite)

            addCollectables()
            addJerryCans()

            if roadImage == "rdc-02_05" {
                addBackgroundDetail(detailType: "cityBridge2")
            }
            screenCounter += 1
        }

I've created this SKAction which I've added to the loop, 我创建了这个SKAction,并将其添加到循环中,

    loopDelay = SKAction.wait(forDuration: Double(index + 1))
    let addRoad = SKAction.run {self.addChild(self.roadSprite)}
    let action = SKAction.sequence([loopDelay, addRoad])
    self.run(action)

Not sure what would cause this, but when I run it with the SKAction delay I've created it crashes and gives me this error, 不知道是什么原因导致的,但是当我以SKAction延迟运行它时,我创建的它崩溃了,并给了我这个错误,

Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: 'Attemped to add a SKNode which already has a parent: 
<SKSpriteNode> name:'Road 4' texture:[<SKTexture> 'rdc-02_05' (750 x 
1334)] position:{0, 6670} scale:{1.00, 1.00} size:{750, 1334} 
anchor:{0.5, 0.5} rotation:0.00'

Any ideas of why the delay would be causing this? 为什么延迟会导致这种情况的任何想法? Thanks 谢谢

Your problem is that you store roadSprite using a property. 您的问题是您使用属性存储roadSprite。 So at each iteration of the for-in loop you replace what's stored in that property with a new Sprite. 因此,在for-in循环的每次迭代中,您都用新的Sprite替换了存储在该属性中的内容。

When each delay passes they will all try to addChild using the same Sprite (which is the last one you created), hence the first time it gets added and the second time it gives you the error above: "Attempted to add a SKNode which already has a parent". 当每个延迟都过去时,他们都会尝试使用相同的Sprite(这是您创建的最后一个Sprite)来添加addChild,因此第一次添加它,而第二次它给您上述错误:“试图添加一个已经存在的SKNode有父母”。

What you want here is to create the sprite as a local variable instead and capture that variable in the SKAction.run closure: 您要在这里创建的是作为局部变量的精灵,然后在SKAction.run闭包中捕获该变量:

let roadSprite = SKSpriteNode(imageNamed: roadImage)

...

let addRoad = SKAction.run {self.addChild(roadSprite)} //capture the local variable in your closure here

If you still want a reference to all of these Sprites then maybe you should stick them into an array instead of overriding the same property. 如果仍然需要引用所有这些Sprite,则应该将它们粘贴到数组中,而不要覆盖相同的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM