节点离开屏幕左侧进入屏幕的另一侧
node leave left side screen enter opposite side of screen
好的,所以我有以下代码使我的 skspritenode 在 x 轴上移动,请参见下面的代码:
manager.startAccelerometerUpdates()
manager.accelerometerUpdateInterval = 0.1
manager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue()){
(data, error) in
let currentX = self.Player.position.x
if data!.acceleration.x < 0 {
self.destX = currentX + CGFloat(data!.acceleration.x * 1000)
}
else if data!.acceleration.x > 0 {
self.destX = currentX + CGFloat(data!.acceleration.x * 1000)
}
print(currentX)
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
let action = SKAction.moveToX(destX, duration: 0.1)
self.Player.runAction(action)
}
现在,当 skspritenode 离开屏幕的左侧或右侧时,我希望它重新出现在屏幕的另一侧。我尝试添加以下行代码:
else if currentX >= self.size.width-5 {
self.destX = 5
}
但这很遗憾没有奏效。有人可以向我解释该怎么做吗?
好的,更新函数现在是这样的:
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)
}
}
关于播放器和屏幕,你为什么不在更新方法中添加这样的东西
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。
试试这个。
在您的 GameScene 上面创建这个 class 在 import SpriteKit 行下面
struct PhysicsCategory {
static let player: UInt32 = 0x1 << 0
static let enemy: UInt32 = 0x1 << 1
}
创建您的物理类别。如果您对此不确定,请阅读一些关于 SKPhysicsBodies 的教程,网上有很多。无论如何你迟早会不得不这样做。
现在给你一个物理 body。转到您正在创建播放器的位置并在将其添加到场景之前添加此代码。
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 值以获得所需的速度。
希望对您有所帮助
好的,所以我想通了,这正是我想要的方式:
在 crashoverride777 的帮助下,我找到了以下解决方案:
所以我在操作代码周围放置了一个 if 语句,这样它只会在屏幕边界内触发,如果它不在屏幕边界内,crashoverrides 代码将启动 运行.
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) }
}
好的,所以我有以下代码使我的 skspritenode 在 x 轴上移动,请参见下面的代码:
manager.startAccelerometerUpdates()
manager.accelerometerUpdateInterval = 0.1
manager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue()){
(data, error) in
let currentX = self.Player.position.x
if data!.acceleration.x < 0 {
self.destX = currentX + CGFloat(data!.acceleration.x * 1000)
}
else if data!.acceleration.x > 0 {
self.destX = currentX + CGFloat(data!.acceleration.x * 1000)
}
print(currentX)
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
let action = SKAction.moveToX(destX, duration: 0.1)
self.Player.runAction(action)
}
现在,当 skspritenode 离开屏幕的左侧或右侧时,我希望它重新出现在屏幕的另一侧。我尝试添加以下行代码:
else if currentX >= self.size.width-5 {
self.destX = 5
}
但这很遗憾没有奏效。有人可以向我解释该怎么做吗?
好的,更新函数现在是这样的:
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)
}
}
关于播放器和屏幕,你为什么不在更新方法中添加这样的东西
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。
试试这个。
在您的 GameScene 上面创建这个 class 在 import SpriteKit 行下面
struct PhysicsCategory {
static let player: UInt32 = 0x1 << 0
static let enemy: UInt32 = 0x1 << 1
}
创建您的物理类别。如果您对此不确定,请阅读一些关于 SKPhysicsBodies 的教程,网上有很多。无论如何你迟早会不得不这样做。
现在给你一个物理 body。转到您正在创建播放器的位置并在将其添加到场景之前添加此代码。
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 值以获得所需的速度。
希望对您有所帮助
好的,所以我想通了,这正是我想要的方式:
在 crashoverride777 的帮助下,我找到了以下解决方案: 所以我在操作代码周围放置了一个 if 语句,这样它只会在屏幕边界内触发,如果它不在屏幕边界内,crashoverrides 代码将启动 运行.
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) }
}