检查节点是否在屏幕之外

问题描述 投票:0回答:3

我的屏幕上有一个用陀螺仪移动的节点。但是我不能将它的位置限制在屏幕上,我已经尝试过了,但是它只适用于底部和顶部:

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
     }

}
ios swift sprite-kit skspritenode
3个回答
3
投票

由于您正试图让Sprite不在屏幕上,所以我建议您使用Sprite Kit的物理引擎。

首先,将一个SKPhysicsBody添加到您的精灵中,例如:



0
投票
x=0
© www.soinside.com 2019 - 2024. All rights reserved.