pVaskou
pVaskou

Reputation: 163

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.

Upvotes: 2

Views: 37

Answers (2)

pVaskou
pVaskou

Reputation: 163

I find solution.

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

Upvotes: 2

vinchenzio
vinchenzio

Reputation: 76

I believe it is because you have collision detection enabled for both, which means they can't be put on top of one another. If you are just trying to determine if one sprite comes into contact with another all you need is the contact detection. Removing either hero or friendlyball from the collision detection bit mask will allow them to be placed on top of each other/go through each other etc.

If you don't care about collisions at all for one of them (the sprite doesn't 'bounce' off anything) I'd recommend just setting collision detection to 0 for that sprite.

Best

Upvotes: 0

Related Questions