royemi
royemi

Reputation: 83

How to change gravity direction on Sprite Kit?

I am using Sprite Kit in Xcode and I was wondering how to change gravity direction.As default gravity direction to "X" you can imagine on below axes graphic.What about if I would like to change to "Y".

My goal is giving to object the falling effect.Its like falling from hight point and touching the ground than getting respond with physics! (Could be dices on board game)

enter image description here

//Default gravity direction is X

    SKSpriteNode *myNode =[SKSpriteNode spriteNodeWithImageNamed:@"ball"];
    myNode.physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:self.frame.size.width/2];
    [self addChild: myNode];

Thanks in advance!

Upvotes: 3

Views: 2375

Answers (2)

Stefan
Stefan

Reputation: 5451

You can apply a vector to the Physics World of your scene using this code

self.physicsWorld.gravity=CGVectorMake(0,-10);

Upvotes: 6

Andriko13
Andriko13

Reputation: 990

In SpriteKit, X and Y are the default coordinates that you see on the screen, and the Z coordinate is the order in which the objects are positioned (the zPosition). Since SpriteKit uses a 2D game engine, you do not have a third dimension, Z, to utilize. You can change the gravity between Y and X (Top/Bottom and Left/Right of screen respectively), but not between the Z coordinate. If you want to recreate a "dice falling" effect, I would recommend you create a Sprite called Dice scaled to a large amount, and once you add it to the scene you scale it down in x amounts of seconds.

[self runAction:[SKAction scaleBy:negativeFloatHere duration:3]];

This will make the dice appear to be falling, and you might want to add some spinning animations for it if you with. If you want to use the 3D engine, go try out Metal or SceneKit

Upvotes: 2

Related Questions