0

我有一个需要 SKPhysicsJointFixed.joint 的小游戏,并且一切正常,除了一个事实,即关节在 10 秒处于活动状态时会减慢精灵的速度,速度似乎是没有关节活动的精灵速度的一半。

我已经尝试过,恢复原状,密度甚至速度,但它们都起作用了。有人可以指导我正确的方向。我在这里找不到任何东西,或者谷歌似乎没有用。

没有护盾的宇宙飞船图片:
没有护盾的宇宙飞船图片

带盾的宇宙飞船图片:
带盾的宇宙飞船的图像

带有 Shield 和 showPhysics = true 的宇宙飞船的图像:
带有 Shield 和 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()
    }
}
4

1 回答 1

1

我在@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
        }
    }
}
于 2018-04-13T12:13:23.577 回答