0

I have a SKSpriteNode fish and an extended SKSpriteNode class hook; I'm trying to join these together when they come in contact, and later have them move together. I am using this code :

[fish removeAllActions];
NSLog(@"Fish anchor is is (%f,%f)", fish.anchorPoint.x, fish.anchorPoint.y);

CGPoint hp = CGPointMake(self.hook.frame.size.width/2, 0);

fish.anchorPoint = CGPointMake(1, .5);

CGPoint hookPoint = [self.scene convertPoint:hp fromNode:self.hook];
NSLog(@"Current fish position  is (%f,%f)", fish.position.x, fish.position.y);

NSLog(@"Hook point is (%f,%f)", hookPoint.x, hookPoint.y);

SKAction *moveAction = [SKAction moveTo:hookPoint duration:.1];

SKAction * flip;
if (fish.direction)  //if going to the right, then turn -90 degrees
    flip = [SKAction  rotateToAngle:M_PI_4 duration:.1 ];
else
    flip = [SKAction  rotateToAngle:(-M_PI_2) duration:.1 ];

//[fish runAction:moveAction];
[fish runAction:[SKAction sequence:@[moveAction, flip]]];

SKPhysicsJointFixed *joint =
            [SKPhysicsJointFixed jointWithBodyA:self.hook.physicsBody
                                          bodyB:fish.physicsBody
                                         anchor:hookPoint];



[self.physicsWorld addJoint:joint];
NSLog(@"Joined");

I have the fish designed as such :

self.anchorPoint = CGPointMake(1, .05);
[self setFishSize:[self getRandomSize]];
self.xScale *= .3;
self.yScale *= .3;    
self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.frame.size]; // 1
self.physicsBody.collisionBitMask = 0;
self.physicsBody.contactTestBitMask = hookCategory;
self.physicsBody.categoryBitMask = fishCategory;
self.physicsBody.usesPreciseCollisionDetection = true;

and the hook as such :

    _hook = [SKSpriteNode spriteNodeWithImageNamed:@"hook"];
    _hook.anchorPoint = CGPointMake(.5, 0);


    _hook.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_hook.frame.size];

    _hook.position = CGPointMake(100, self.frame.size.height - self.hook.size.height/2);
    _hook.physicsBody.dynamic = YES;
    _hook.physicsBody.categoryBitMask = hookCategory;
    _hook.physicsBody.contactTestBitMask = fishCategory;
    _hook.physicsBody.collisionBitMask = 0;
    _hook.physicsBody.usesPreciseCollisionDetection = YES;
    _hook.physicsBody.affectedByGravity = false;
    _hook.physicsBody.allowsRotation = false;
    self.physicsWorld.gravity = CGVectorMake(0,0);
    self.physicsWorld.contactDelegate = self;

and yet when they are joined, and I move the hook, the fish does not come along. Are there any glaring mistakes I'm making?

Any help greatly appreciated.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
pikovayadama
  • 808
  • 2
  • 8
  • 26

1 Answers1

1

You should have the joint formation in an action block, I suggest something like this,

 SKPhysicsJointFixed *joint = [SKPhysicsJointFixed jointWithBodyA:self.hook.physicsBody bodyB:fish.physicsBody anchor:hookPoint];

 SKAction *createJoint = [SKAction runBlock:^{   
 [self.physicsWorld addJoint:joint];}]

 // run this action when the condition is met
 [fish runAction:[SKAction createJoint]];

Also I see that you are using a lot of collision customization (e.g. collision groups, rotation limitation etc.) I suggest you use the bare minimum (or non) to start and get that working and then add the customization one by one to ensure it all works.

user2421700
  • 149
  • 8
  • thank you, I will try that. What is the difference between adding an action normally as opposed to doing it through an action block? – pikovayadama Nov 12 '13 at 22:24
  • This way you can also detach and remove the joint with an action so its free again. Since I assume the hook remains but the fish needs to detach from the hook and go to a basket or something. – user2421700 Nov 13 '13 at 19:31