How do you tell if a node is on the screen spritekit swift

前端 未结 6 2008
予麋鹿
予麋鹿 2020-12-09 12:48

I am trying to figure out how to determine if a node is visible on the screen or off the screen. Is this just a true/false property of the node? Thanks. (Using swift spritek

相关标签:
6条回答
  • 2020-12-09 13:14

    Not directly but you could use its position to check. Therefore you might do something like:

    if (/*the node's position is between 0 and the screen's .x and .y*/) {
        //on screen
    }
    

    Hope that helps :)

    0 讨论(0)
  • 2020-12-09 13:17

    If your SKScene fits within its containing view, this should work:

    if !node.intersectsNode(node.parent!) {
         // node is off-scene/out-of-view
    }
    

    where node is an SKNode that is the child of your SKScene.

    0 讨论(0)
  • 2020-12-09 13:21

    You could use CGRectIntersectsRect supplying the frame of your SKSpriteNode and the SKScene. For example:

    if !CGRectIntersectsRect(frame, spriteNode.frame) {
        // Outside the bounds of the scene because the frames are no longer intersecting.
    }
    
    0 讨论(0)
  • 2020-12-09 13:32

    You can use the following to test if a node is in the scene:

    if (!intersectsNode(sprite)) {
      println("node is not in the scene")
    }
    

    This assumes that self is an SKScene subclass, such as GameScene.

    0 讨论(0)
  • 2020-12-09 13:33

    the best answer here will be:

    if scene.frame.contains(myNode.position) {
    //do stuff
    }
    

    "intersect" method is very heavy and involves too much computing, can produce lag if used on many instances

    0 讨论(0)
  • 2020-12-09 13:38

    If you put a SKCameraNode in the scene, you can check if a node is inside the view of the camera using the contains method:

    https://developer.apple.com/documentation/spritekit/skcameranode

    You can also get all the nodes visible to the camera using the containedNodeSet instance method.

    0 讨论(0)
提交回复
热议问题