SKPhysicsJointFixed.joint减慢Sprite的速度

时间:2018-04-12 20:38:52

标签: ios swift sprite-kit skphysicsjoint

我有一个小游戏,需要一个SKPhysicsJointFixed.joint并且它都可以工作,除了一个事实,当它处于活动状态时,10秒的关节减慢了精灵的速度,似乎只有精灵的速度的一半没有联合活跃。

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

没有盾牌的宇宙飞船的形象:
Image of Spaceship without Shield

带盾牌的宇宙飞船图片:
Image of Spaceship with Shield

带有盾牌的太空飞船的图像和showPhysics = true:
Image of Spaceship with Shield and 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()
    }
}

1 个答案:

答案 0 :(得分: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
        }
    }
}