ios - Check if nodes are off the screen -


i have node on screen moved gyroscope. can't limit it's position screen, have tried this, worked bottom , the top:

override func update(currenttime: cftimeinterval){       ...       if sprite.position.x <= sprite.frame.width / 2 {           print("out on left of screen")      }      if sprite.position.x >= self.frame.width - sprite.frame.width / 2 {           print("out on right of screen")      }      if sprite.position.y <= sprite.frame.height / 2 {           print("out on top of screen")           //worked      }      if sprite.position.y <= self.frame.height - sprite.frame.height / 2 {           print("out on bottom of screen")           //worked      }  } 

since you're trying stop sprite going off screen i'd recommend use sprite kit's physics engine.

firstly, add skphysicsbody sprite, example:

sprite.physicsbody = skphysicsbody(rectangleofsize: sprite.size) 

secondly, need add physics body scene, example:

override func didmovetoview(view: skview) {     super.didmovetoview(view)     self.physicsbody = skphysicsbody(edgeloopfromrect: view.bounds) } 

finally, since want gravity disabled can either disable sprite:

sprite.physicsbody!.affectedbygravity = false // force unwrapping here okay because we're sure sprite has physics body. 

or, disable gravity in scene:

self.physicsworld.gravity = cgvector.zerovector // self in case `skscene` subclass. 

for more information the sprite kit programming guide useful, in case of physics see simulating physics section.


Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -