user3529025
user3529025

Reputation: 15

BodyWithBodies on Xcode

How to create SKPhysicsBody bodyWithBodies?
i am new and i tried but it just wouldn't appear on the simulator. somebody can give an example of it? here is part of my code:

Bike = [SKSpriteNode node];
    Bike.physicsBody = [SKPhysicsBody bodyWithBodies:bikeArray];
    Bike.position = CGPointMake(100, 100);
    //Bike.size = CGSizeMake(40, 50);
    Bike.physicsBody.mass = 0.1;
    //Bike.physicsBody.dynamic = YES;
    Bike.physicsBody.affectedByGravity = YES;
    Bike.physicsBody.restitution = 0;
    Bike.physicsBody.friction = 0;
    Bike.physicsBody.allowsRotation = YES;
    Bike.name = @"Bike";
    [self addChild:Bike];

tanks,

Upvotes: 1

Views: 453

Answers (1)

sangony
sangony

Reputation: 11696

You can do it like this:

object1.physicsBody = [SKPhysicsBody bodyWithBodies:@[object2, object3, object4]];

You can either list each sprite object like I did above, or stick them into an array and do it like in your code sample.

Just remember that the sprite objects you have in the array should already have their respective positions declared (object.position = CGPointMake(x, y);) and be added to the view.

Other important issues to keep in mind are:

The shapes of the physics bodies passed into this method are used to create a new physics body whose covered area is the union of the areas of its children. These areas do not need to be contiguous. If there is space between two parts, other bodies may be able to pass between these parts. However, the physics body is treated as a single connected body, meaning that a force or impulse applied to the body affects all of the pieces as if they were held together with an indestructible frame.

Upvotes: 1

Related Questions