简体   繁体   English

如何在场景外正确删除节点

[英]how to properly remove nodes once outside of scene

这就是它们卡住时的样子 I am making a game where the "arrow" in this case, is a node that falls every few seconds in a random area from top to bottom of the screen.我正在制作一个游戏,在这种情况下,“箭头”是一个节点,它每隔几秒钟就会从屏幕顶部到底部的随机区域中落下。 The problem is that when the node reaches around the last 10th of the screen, it seems to almost get stuck on the screen, and stops, not fully going off the screen and disappearing.问题是当节点到达屏幕的最后 10 个左右时,它似乎几乎卡在屏幕上,然后停止,没有完全离开屏幕并消失。 Is there any way to fix this?有没有什么办法解决这一问题?

func startTheArrow() {

    run(SKAction.repeatForever(SKAction.sequence([SKAction.run(spawnArrow), SKAction.wait(forDuration: 5.0)])))
    
} 




func spawnArrow() {
    
    let arrow = SKSpriteNode(imageNamed: "arrow")
    
    arrow.size = CGSize(width: 50, height: 50)
    arrow.physicsBody = SKPhysicsBody(rectangleOf: arrow.size)
    arrow.physicsBody?.affectedByGravity = false
    arrow.physicsBody?.categoryBitMask = ColliderType.arrow
    
    arrow.name = "Arrow"
    
    arrow.zPosition = 1
    
    arrow.position = CGPoint(x: frame.size.width * random(min: -0.45, max: 0.45), y: frame.size.height * random(min: 0.6, max: 0.7))
    
    addChild(arrow)
    
    arrow.run(
        SKAction.moveBy(x: 0.0 , y: -size.height - arrow.size.height,
                        duration: TimeInterval(random(min: 2, max: 2))))
    
    self.enumerateChildNodes(withName: "Arrow") { (node:SKNode, nil) in
        if node.position.y < -500 || node.position.y > self.size.height + 550 {
            node.removeFromParent()
            
                 
       }
   }   
} 

So I finally found the solution.所以我终于找到了解决方案。 For some reason, Xcode doesn't like this code:出于某种原因,Xcode 不喜欢这段代码:

arrow.run(
    SKAction.moveBy(x: 0.0 , y: -size.height - arrow.size.height,
                    duration: TimeInterval(random(min: 2, max: 2))))

self.enumerateChildNodes(withName: "Arrow") { (node:SKNode, nil) in
    if node.position.y < -500 || node.position.y > self.size.height + 550 {
        node.removeFromParent()
        
             
   }
}   

I instead used the following code in place of this and the nodes were fixed, although they don't fully go off the screen, there isn't some weird stoppage and pileup at the bottom of the scene.我改为使用以下代码代替此代码,并且节点已修复,尽管它们没有完全离开屏幕,但在场景底部没有一些奇怪的停止和堆积。

  let moveEnemy = SKAction.moveTo(y: -800, duration: 4.0)
    let deleteEnemy= SKAction.removeFromParent()
    let enemySequence = SKAction.sequence([moveEnemy, deleteEnemy])
    enemy.run(enemySequence)    

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

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