Search code examples
iosiphonegamekitskshapenode

Add sprite to other sprite position


I have two body

SKShapeNode *hero = [SKShapeNode shapeNodeWithCircleOfRadius:HERO_SIZE];
hero.lineWidth = 1;
hero.fillColor = [SKColor orangeColor];
hero.name = @"Hero";

    hero.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:HERO_SIZE];
    hero.physicsBody.categoryBitMask = hero;
    hero.physicsBody.collisionBitMask = friendlyCategory | enemyCategory;
    hero.physicsBody.contactTestBitMask = friendlyCategory | enemyCategory;       

    hero.position = [self getStartPosition];


  SKShapeNode *circle = [SKShapeNode shapeNodeWithCircleOfRadius:BALL_SIZE];
    circle.position = [self randomPossition];
    circle.lineWidth = 2;
    circle.strokeColor = [SKColor blueColor]; 
    circle.fillColor = color;
    circle.name = @"Circle"; 
    circle.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:BALL_SIZE];
    circle.physicsBody.categoryBitMask = friendlyCategory;


    circle.physicsBody.collisionBitMask = hero;
    circle.physicsBody.contactTestBitMask = hero;

then,

- (void)didEndContact:(SKPhysicsContact *)contact
{
 uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);
    if (collision == (friendlyCategory | hero ))
    {
 SKShapeNode* node = (SKShapeNode*)contact.bodyA.node;

        if ([node.name isEqualToString:@"Hero"])
        {
            self.activeFriendlyBall = (SKShapeNode*)contact.bodyB.node;
        }
        else
        {
            self.activeFriendlyBall = (SKShapeNode*)contact.bodyA.node;
        }



           self.hero.position = self.activeFriendlyBall.position;

}
}

Hero must be added above activeFriendlyBall, for him center position. But it added near him. I think it because physic body added. But I need to use physic body for other logic.

http://cl.ly/image/0g1S0d3h1h0w

must be like, how in top screen.


Solution

  • I find solution.

    self.heroBall.physicsBody.velocity = CGVectorMake(0, 0);
    [self.heroBall runAction: [SKAction moveTo:self.activeFriendlyBall.position duration:0.0]];