Game Development Reference
In-Depth Information
of one physics body with another physics body. Here we have set this to 0 , in or-
der to make the objects move freely, without slowing down.
3. Every game object is inherited from the SKSpriteNode class, which allows the
physics body to hold on to the node. Let us take an example and create a game
object using the following code:
// 1
SKSpriteNode* gameObject = [SKSpriteNode
spriteNodeWithImageNamed: @"object.png"];
gameObject.name = @"game_object";
gameObject.position =
CGPointMake(self.frame.size.width/3,
self.frame.size.height/3);
[self addChild:gameObject];
// 2
gameObject.physicsBody = [SKPhysicsBody
bodyWithCircleOfRadius:gameObject.frame.size.width/2];
// 3
gameObject.physicsBody.friction = 0.0f;
We are already familiar with the first few lines of code wherein we are creating
the sprite reference and then adding it to the scene. Now in the next line of code,
we are associating a physics body with that sprite. We are initializing the circular
physics body with radius and associating it with the sprite object.
Then we can update various other properties of the physics body such as friction,
restitution, linear damping, and so on.
4. The physics body properties also allow you to apply force. To apply force you
need to provide the direction where you want to apply force.
[gameObject.physicsBody
applyForce:CGVectorMake(10.0f, -10.0f)];
In the code we are applying force in the bottom-right corner of the world. To
provide the direction coordinates we have used CGVectorMake , which accepts
the vector coordinates of the physics world.
Search WWH ::




Custom Search