user9611921
user9611921

Reputation:

SKPhysicsJointFixed.joint slowing down the Sprite

I have a little game that requires a SKPhysicsJointFixed.joint and it all works, except for one the fact that the joint while it is active which is 10 seconds slows down the sprite by what seems half the speed of the sprite without the joint active.

I have tried, restitution, density and even velocity, but of them worked. Could someone please guide me in the right direction. Nothing I find on here or Google seems to work.

Image of Spaceship without Shield:
Image of Spaceship without Shield

Image of Spaceship with Shield:
Image of Spaceship with Shield

Image of Spaceship with Shield and 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()
    }
}

Upvotes: 0

Views: 60

Answers (1)

user9611921
user9611921

Reputation:

I solved this with the help of @Ron by removing the PhysicsJoint altogether and then adding the shield.position.x += amountDragged same way it was done for the Spaceship and it worked perfectly.

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
        }
    }
}

Upvotes: 1

Related Questions