简体   繁体   中英

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. 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:

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)    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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