oke所以我有以下代码使我的skspritenode在x轴上移动,请参阅下面的代码:
else if currentX >= self.size.width-5 {
self.destX = 5
}
现在当skspritenode离开屏幕的左侧或右侧时,我希望它重新出现在屏幕的另一侧。我尝试添加以下行代码:
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
let action = SKAction.moveToX(destX, duration: 0.1)
self.Player.runAction(action)
if self.Player.position.x < CGRectGetMinX(self.frame) - (Player.size.width / 2) {
self.player.position.x = CGRectGetMaxX(self.frame) - (self.Player.size.width / 2)
}
else if self.Player.position.x > CGRectGetMaxX(self.frame) + (Player.size.width / 2) {
self.player.position.x = CGRectGetMinX(self.frame) + (self.Player.size.width / 2)
}
}
但这可悲的是没有用。有人可以解释我怎么做吗?
oke所以这就是更新功能现在的样子:
{{1}}
答案 0 :(得分:1)
关于播放器和屏幕,为什么不在更新方法中添加这样的内容
if self.Player.position.x < CGRectGetMinX(self.frame) - (Player.size.width / 2) {
self.player.position.x = CGRectGetMaxX(self.frame) - (self.Player.size.width / 2)
}
else if self.Player.position.x > CGRectGetMaxX(self.frame) + (Player.size.width / 2) {
self.player.position.x = CGRectGetMinX(self.frame) + (self.Player.size.width / 2)
}
我试图让它与您的移动代码一起使用但我无法在updateMethod中认为它是SKAction。
试试这个。
在导入SpriteKit行下面的GameScene类上面创建这个
struct PhysicsCategory {
static let player: UInt32 = 0x1 << 0
static let enemy: UInt32 = 0x1 << 1
}
创建物理类别。如果你不确定这个,请阅读一些关于SKPhysicsBodies的教程,网上有很多。无论如何你迟早都要这么做。
现在给你玩家一个物理身体。转到创建播放器的位置,然后在将其添加到场景之前添加此代码。
player.physicsBody = SKPhysicsBody(rectangleOfSize: player.size)
player.physicsBody?.categoryBitMask = PhysicsCategory.player
player.physicsBody?.collisionBitMask = 0
player.physicsBody?.dynamic = true
player.physicsBody?.affectedByGravity = false
addChild(player)
并将您的动作代码更改为此
manager.startAccelerometerUpdates()
manager.accelerometerUpdateInterval = 0.1
manager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue()){
(data, error) in
/// use this to limit the max speed of player in both directions
guard player.physicsBody!.velocity.dx > -500 && node.physicsBody!.velocity.dx < 500 else { return }
/// if your game is in landscape
if UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft {
self.player.physicsBody!.applyForce(CGVectorMake(-500 * CGFloat(data!.acceleration.y), 0))
} else {
self.player.physicsBody!.applyForce(CGVectorMake(500 * CGFloat(data!.acceleration.y), 0))
}
/// if your game is in portrait
self.player.physicsBody!.applyForce(CGVectorMake(500 * CGFloat(data!.acceleration.y), 0))
}
根据您的方向,注释掉您不需要的代码。 玩500值以获得所需的速度。
希望这有帮助
答案 1 :(得分:0)
oke所以我想通了,所以这就是我想要的方式:
在crashoverride777的所有帮助下,我得到了以下解决方案: 所以我在动作代码周围放了一个if语句,所以它只会在屏幕边界触发,如果它不在屏幕崩溃范围内,代码就会开始运行。
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
print(self.Player.position.x)
if (self.Player.position.x > CGRectGetMinX(self.frame)-5) && (self.Player.position.x < CGRectGetMaxX(self.frame)+5){
let action = SKAction.moveToX(self.destX, duration: 1)
self.Player.runAction(action)
}
所以我在更新函数中添加了crashoverrides代码,见下文:
if self.Player.position.x < CGRectGetMinX(self.frame) - (Player.size.width / 2) {
self.Player.position.x = CGRectGetMaxX(self.frame)
}
else if self.Player.position.x > CGRectGetMaxX(self.frame) + (Player.size.width / 2) {
self.Player.position.x = CGRectGetMinX(self.frame) }
}