SKPhysicsJointFixed.joint 放慢雪碧
SKPhysicsJointFixed.joint slowing down the Sprite
我有一个需要 SKPhysicsJointFixed.joint 的小游戏,它一切正常,除了一个事实,即关节在活动时 10 秒会减慢精灵的速度,速度似乎是没有关节活动的精灵。
我已经尝试过恢复原状、密度甚至速度,但它们都有效。有人可以指导我正确的方向吗?我在这里找到的任何东西或 Google 似乎都不起作用。
没有护盾的宇宙飞船图片:
带护盾的宇宙飞船图片:
带有护盾和 showPhysics = true 的宇宙飞船图像:
func activateShield() {
let shield1 = SKTexture(imageNamed: "shield-1")
let shieldImages = [shield1, SKTexture(imageNamed: "shield-2"), SKTexture(imageNamed: "shield-3"), SKTexture(imageNamed: "shield-4"), SKTexture(imageNamed: "shield-5"), SKTexture(imageNamed: "shield-6")]
let animateShield = SKAction.animate(with: shieldImages, timePerFrame: 0.10)
let animateRepeatShield = SKAction.repeatForever(animateShield)
shield = SKSpriteNode(texture: shield1)
shield.name = "ShieldActive"
shield.setScale(2.5)
shield.position = player.position
shield.zPosition = 3
shield.physicsBody = SKPhysicsBody(rectangleOf: shield.size)
shield.physicsBody!.affectedByGravity = false
shield.physicsBody!.categoryBitMask = PhysicsCategories.ShieldActive
shield.physicsBody!.collisionBitMask = PhysicsCategories.Enemy
shield.physicsBody!.contactTestBitMask = PhysicsCategories.Enemy | PhysicsCategories.LifePu
shield.physicsBody!.isDynamic = true
shield.physicsBody!.density = 0
self.addChild(shield)
shield.run(animateRepeatShield)
let joint = SKPhysicsJointFixed.joint(withBodyA: player.physicsBody!, bodyB:shield.physicsBody!, anchor:player.position)
self.physicsWorld.add(joint)
//turns off the shield in between 5-10 seconds
shield.run(.wait(forDuration: 10, withRange: 0)) {
self.shield.removeFromParent()
}
}
我在@Ron 的帮助下解决了这个问题,方法是完全删除 PhysicsJoint,然后添加 shield.position.x += amountDragged,方法与为 Spaceship 所做的相同,并且效果很好。
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches{
let pointOfTouch = touch.location(in: self)
let previousPointOfTouch = touch.previousLocation(in: self)
let amountDragged = pointOfTouch.x - previousPointOfTouch.x
if currentGameState == gameState.inGame{
player.position.x += amountDragged
shield.position.x += amountDragged
}
if player.position.x > gameArea.maxX - player.size.width/2{
player.position.x = gameArea.maxX - player.size.width/2
}
if player.position.x < gameArea.minX + player.size.width/2{
player.position.x = gameArea.minX + player.size.width/2
}
if shield.position.x > gameArea.maxX - player.size.width/2{
shield.position.x = gameArea.maxX - player.size.width/2
}
if shield.position.x < gameArea.minX + player.size.width/2{
shield.position.x = gameArea.minX + player.size.width/2
}
}
}
我有一个需要 SKPhysicsJointFixed.joint 的小游戏,它一切正常,除了一个事实,即关节在活动时 10 秒会减慢精灵的速度,速度似乎是没有关节活动的精灵。
我已经尝试过恢复原状、密度甚至速度,但它们都有效。有人可以指导我正确的方向吗?我在这里找到的任何东西或 Google 似乎都不起作用。
没有护盾的宇宙飞船图片:
带护盾的宇宙飞船图片:
带有护盾和 showPhysics = true 的宇宙飞船图像:
func activateShield() {
let shield1 = SKTexture(imageNamed: "shield-1")
let shieldImages = [shield1, SKTexture(imageNamed: "shield-2"), SKTexture(imageNamed: "shield-3"), SKTexture(imageNamed: "shield-4"), SKTexture(imageNamed: "shield-5"), SKTexture(imageNamed: "shield-6")]
let animateShield = SKAction.animate(with: shieldImages, timePerFrame: 0.10)
let animateRepeatShield = SKAction.repeatForever(animateShield)
shield = SKSpriteNode(texture: shield1)
shield.name = "ShieldActive"
shield.setScale(2.5)
shield.position = player.position
shield.zPosition = 3
shield.physicsBody = SKPhysicsBody(rectangleOf: shield.size)
shield.physicsBody!.affectedByGravity = false
shield.physicsBody!.categoryBitMask = PhysicsCategories.ShieldActive
shield.physicsBody!.collisionBitMask = PhysicsCategories.Enemy
shield.physicsBody!.contactTestBitMask = PhysicsCategories.Enemy | PhysicsCategories.LifePu
shield.physicsBody!.isDynamic = true
shield.physicsBody!.density = 0
self.addChild(shield)
shield.run(animateRepeatShield)
let joint = SKPhysicsJointFixed.joint(withBodyA: player.physicsBody!, bodyB:shield.physicsBody!, anchor:player.position)
self.physicsWorld.add(joint)
//turns off the shield in between 5-10 seconds
shield.run(.wait(forDuration: 10, withRange: 0)) {
self.shield.removeFromParent()
}
}
我在@Ron 的帮助下解决了这个问题,方法是完全删除 PhysicsJoint,然后添加 shield.position.x += amountDragged,方法与为 Spaceship 所做的相同,并且效果很好。
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches{
let pointOfTouch = touch.location(in: self)
let previousPointOfTouch = touch.previousLocation(in: self)
let amountDragged = pointOfTouch.x - previousPointOfTouch.x
if currentGameState == gameState.inGame{
player.position.x += amountDragged
shield.position.x += amountDragged
}
if player.position.x > gameArea.maxX - player.size.width/2{
player.position.x = gameArea.maxX - player.size.width/2
}
if player.position.x < gameArea.minX + player.size.width/2{
player.position.x = gameArea.minX + player.size.width/2
}
if shield.position.x > gameArea.maxX - player.size.width/2{
shield.position.x = gameArea.maxX - player.size.width/2
}
if shield.position.x < gameArea.minX + player.size.width/2{
shield.position.x = gameArea.minX + player.size.width/2
}
}
}