繁体   English   中英

如何在Sprite Kit中的场景外删除节点

[英]How to remove node when become outside the scene in Sprite Kit

我试图删除节点时出现在场边,我尝试这种方法来做到这一点

if( CGRectIntersectsRect(node.frame, view.frame) ) {
   // Don't delete your node
} else {
   // Delete your node as it is not in your view
}

但似乎没有任何帮助将不胜感激

从性能的角度来看,这不是最好的方法,但如果你在场景中覆盖update方法,你将能够编写每帧执行的代码。

class GameScene : SKScene {

    var arrow : SKSpriteNode?

    override func update(currentTime: NSTimeInterval) {
        super.update(currentTime)

        if let
            arrow = arrow,
            view = self.view
        where
            CGRectContainsRect(view.frame, arrow.frame) == false &&
            CGRectIntersectsRect(arrow.frame, view.frame) == false {
                arrow.removeFromParent()
        }
    }
}

注意事项

请记住,您在更新方法中编写的每个代码都是每帧执行一次 (60fps游戏每秒60次),所以您应该非常小心。 除非绝对必要,否则您不希望在update写入的典型内容:

  1. 创建对象
  2. 循环
  3. 递归调用
  4. 任何需要太多时间执行的疯狂代码

希望这可以帮助。

暂无
暂无

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

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