Search code examples
physicsgravitysprite-kitskphysicsbody

How do I set a different value for gravity?


I am using Sprite Kit in Xcode and I want to change the default value that's set on the gravity. This is the code I have:

SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"sprite1.png"];
        sprite.position = CGPointMake(-100+CGRectGetMidX(self.frame), 50 + CGRectGetMidY(self.frame));
        [self addChild:sprite];
        sprite.color = [SKColor blueColor];
        sprite.colorBlendFactor = 1.0;
        sprite.xScale = 0.2;
        sprite.yScale = 0.2;
        [SKPhysicsBody bodyWithCircleOfRadius:50];
        sprite.physicsBody.dynamic = YES;
        sprite.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:sprite.size.width * 0.5];

And Now I know that using this line of code should give the default settings but I can't run the project now:

//self.physicsWorld.gravity = CGPointMake(0.0, -9.8);

Help?


Solution

  • self.physicsWorld.gravity property is represented by CGVector. So you should use CGVectorMake to change gravity:

            self.physicsWorld.gravity = CGVectorMake(0.0f, -9.8f);