0

I am working on a game that involves objects(fish) that move across the screen that you need to catch with another object(hook) that you can move up and down. Once the objects make contact the fish's position is supposed to become the same as the hook's. So in did begin contact (which does work, Im sure) I said, when the hook and fish touch make the fishes position become the same as the hooks.

func didBeginContact(contact: SKPhysicsContact) {

        let firstBody = contact.bodyA
        let secondBody = contact.bodyB




     if firstBody.categoryBitMask == physicsCategory.fishHook && secondBody.categoryBitMask == physicsCategory.greenFish || firstBody.categoryBitMask == physicsCategory.greenFish && secondBody.categoryBitMask == physicsCategory.fishHook{

                CollisionWithfishHook(firstBody.node as! SKSprit

    eNode, greenFish: secondBody.node as! SKSpriteNode)

        }
    }

    func CollisionWithfishHook (fishHook: SKSpriteNode, greenFish: SKSpriteNode){

        greenFish.position = CGPointMake(fishHook.position.x, fishHook.position.y)

    }

For some reason when I run it and they make contact nothing happens. I know that did begin contact is working correctly because when I told it to remove the fish when it touches the hook it works. The problem is I am unable to change the location of the green fish. Why is this and what can I do to fix it?

james
  • 69
  • 5

1 Answers1

0

Changing the position of the fish in the didBeginContact method is only going to change the fish's position once - the moment the fish and hook touch. You need to continue updating the fish's position in the update method.

I would create a class variable in your GameScene class called fishToUpdate and when you 'hook' one, set fishToUpdate = greenFish (or whatever was caught) in your didBeginContact method. Then in the update method, update the fish's position by fishToUpdate.position = fishHook.position.

claassenApps
  • 1,137
  • 7
  • 14
  • Are you talking about the override func update? If so what do need to do in it. I am confused. Should I say if the objects touched then change the position or what. – james Jul 12 '16 at 05:31