问题
I have been trying to find, but did not succeed. Is there a way to change physicsworld gravity property in such way, that objects would not be attracted towards the bottom of the screen, but to the centre of it instead?
回答1:
Use an SKFieldNode
for that. Just place it at the center of your map and disable gravity by putting code in that sets the gravity to zero (CGVector(0,0)
)
I would give you code, but I don't use Objective C, so I don't know the exact syntax for it. I can give it a shot though... [physicsBody setGravity: CGVector(0,0)];
PLEASE NOTE I HAVE NO IDEA IF THAT IS CORRECT SYNTAX
EDIT:
The asker requested an example of SKFieldNode in Swift, so here it goes.
For the question asked, what you would do is create a Radial Gravity Field
node at the center. This code goes in GameScene.swift (or .m if you're using Objective C, and make sure you change the syntax to Obj-C).
let gravField = SKFieldNode.radialGravityField(); // Create grav field
gravField.position.x = size.width/2; // Center on X axis
gravField.position.y = size.height/2; // Center on Y axis (Now at center of screen)
addChild(gravField); // Add to world
回答2:
You are probably looking for a SKFieldNode. There are a couple of different types so you will have to read the docs. The one you are probably looking for is called radialGravityField
.
来源:https://stackoverflow.com/questions/29615651/how-to-change-center-of-gravity-in-sprite-kit-game