检查节点是否在屏幕外
Check if nodes are off the screen
我的屏幕上有一个节点随陀螺仪移动。但我不能将它的位置限制在屏幕上,我试过了,但它只对底部和顶部有效:
override func update(currentTime: CFTimeInterval){
...
if sprite.position.x <= sprite.frame.width / 2 {
print("out on the left of the screen")
}
if sprite.position.x >= self.frame.width - sprite.frame.width / 2 {
print("out on the right of the screen")
}
if sprite.position.y <= sprite.frame.height / 2 {
print("out on the top of the screen")
//worked
}
if sprite.position.y <= self.frame.height - sprite.frame.height / 2 {
print("out on the bottom of the screen")
//worked
}
}
override func update(currentTime: CFTimeInterval){
...
if sprite.position.x <= 0 {
print("out on the left of the screen")
}
if sprite.position.x >= self.size.width {
print("out on the right of the screen")
}
if sprite.position.y <= sprite.frame.height / 2 {
print("out on the top of the screen")
//worked
}
if sprite.position.y <= self.frame.height - sprite.frame.height / 2 {
print("out on the bottom of the screen")
//worked
}
}
要检测精灵是否向左离开屏幕,您将精灵 x 坐标与 x=0
进行比较,并查看精灵是否向右离开屏幕,您可以与场景宽度。
既然你想阻止精灵离开屏幕,我建议你使用 Sprite Kit 的物理引擎。
首先,给你的精灵添加一个SKPhysicsBody
,例如:
sprite.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size)
其次,需要在场景中添加物理体,例如:
override func didMoveToView(view: SKView) {
super.didMoveToView(view)
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: view.bounds)
}
最后,既然你想禁用重力,你可以只为 sprite
:
禁用它
sprite.physicsBody!.affectedByGravity = false
// force unwrapping here is okay because we're SURE sprite has a physics body.
或者,禁用场景中所有内容的重力:
self.physicsWorld.gravity = CGVector.zeroVector
// self in this case is your `SKScene` subclass.
有关详细信息 The Sprite Kit Programming Guide is really useful, in the case of physics see the Simulating Physics 部分。
guard let position = spaceMan?.position else { return }
print(position)
let screenWidth = UIScreen.main.bounds.size.width
let screenHeight = UIScreen.main.bounds.size.height
if abs(position.x) > screenWidth/2 { // off the horizontal axis
spaceMan?.position = CGPoint(x: 0, y: 0)
}
if abs(position.y) > screenHeight/2 { // off the vertical axis
spaceMan?.position = CGPoint(x: 0, y: 0)
}
我的屏幕上有一个节点随陀螺仪移动。但我不能将它的位置限制在屏幕上,我试过了,但它只对底部和顶部有效:
override func update(currentTime: CFTimeInterval){
...
if sprite.position.x <= sprite.frame.width / 2 {
print("out on the left of the screen")
}
if sprite.position.x >= self.frame.width - sprite.frame.width / 2 {
print("out on the right of the screen")
}
if sprite.position.y <= sprite.frame.height / 2 {
print("out on the top of the screen")
//worked
}
if sprite.position.y <= self.frame.height - sprite.frame.height / 2 {
print("out on the bottom of the screen")
//worked
}
}
override func update(currentTime: CFTimeInterval){
...
if sprite.position.x <= 0 {
print("out on the left of the screen")
}
if sprite.position.x >= self.size.width {
print("out on the right of the screen")
}
if sprite.position.y <= sprite.frame.height / 2 {
print("out on the top of the screen")
//worked
}
if sprite.position.y <= self.frame.height - sprite.frame.height / 2 {
print("out on the bottom of the screen")
//worked
}
}
要检测精灵是否向左离开屏幕,您将精灵 x 坐标与 x=0
进行比较,并查看精灵是否向右离开屏幕,您可以与场景宽度。
既然你想阻止精灵离开屏幕,我建议你使用 Sprite Kit 的物理引擎。
首先,给你的精灵添加一个SKPhysicsBody
,例如:
sprite.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size)
其次,需要在场景中添加物理体,例如:
override func didMoveToView(view: SKView) {
super.didMoveToView(view)
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: view.bounds)
}
最后,既然你想禁用重力,你可以只为 sprite
:
sprite.physicsBody!.affectedByGravity = false
// force unwrapping here is okay because we're SURE sprite has a physics body.
或者,禁用场景中所有内容的重力:
self.physicsWorld.gravity = CGVector.zeroVector
// self in this case is your `SKScene` subclass.
有关详细信息 The Sprite Kit Programming Guide is really useful, in the case of physics see the Simulating Physics 部分。
guard let position = spaceMan?.position else { return }
print(position)
let screenWidth = UIScreen.main.bounds.size.width
let screenHeight = UIScreen.main.bounds.size.height
if abs(position.x) > screenWidth/2 { // off the horizontal axis
spaceMan?.position = CGPoint(x: 0, y: 0)
}
if abs(position.y) > screenHeight/2 { // off the vertical axis
spaceMan?.position = CGPoint(x: 0, y: 0)
}