问题描述
我一直在努力寻找,但没有成功.有没有办法以这种方式改变物理世界的重力属性,使物体不会被吸引到屏幕底部,而是被吸引到屏幕的中心?
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?
推荐答案
使用 SKFieldNode
为此.只需将其放在地图的中心并通过将重力设置为零的代码来禁用重力 (CGVector(0,0)
)
我会给你代码,但我不使用 Objective C,所以我不知道它的确切语法.不过我可以试一试... [physicsBody setGravity: 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
提问者请求了 Swift 中的 SKFieldNode 示例,所以就到这里了.对于提出的问题,您要做的是在中心创建一个 Radial Gravity Field
节点.此代码位于 GameScene.swift 中(如果您使用的是 Objective C,则为 .m,并确保将语法更改为 Obj-C).
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
这篇关于如何在精灵套件游戏中改变重心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!