Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 spritekit)

like image 788
bob Avatar asked May 30 '15 22:05

bob


3 Answers

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

like image 174
Anton Shevtsov Avatar answered Nov 05 '22 05:11

Anton Shevtsov


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.

like image 19
0x141E Avatar answered Nov 05 '22 06:11

0x141E


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.

like image 3
DevinDazzle Avatar answered Nov 05 '22 06:11

DevinDazzle