I really need to know how to lock the x axis of the SKSpriteNode and its physical device. I need to keep SKSpriteNode dynamic
and affectedByGravity
. node is on a slope, which is why the x axis moves due to gravity. However, I do not want the x axis of this SKSpriteNode to move due to gravity. Is there a way to lock the x axis to achieve this? Thanks for any help :D
Edit: I tried applying the constraint to the x value as follows:
let xConstraint = SKConstraint.positionX(SKRange(constantValue: 195)) node.constraints?.append(xConstraint)
However, this does not work, and I'm not sure why and how to fix it. Does anyone know of a solution?
Edit 2: SKPhysicsJointPin
actually looks more promising. In the comments of the first answer / answer to this question, I try to understand how to use it correctly in my situation.
An example of my code:
let node = SKSpriteNode(imageNamed: "node") enum collisionType: UInt32 { case node = 1 case ground = 2 case other = 4
The node is on top of the earth, and the earth consists of various SKSpriteNode lines that have a physical device based on volume. The earth continues to add new lines in front and delete them in the back, and also change the x value of each line negative (so that the earth seems moving - this process is performed in SKAction). These lines (parts of the earth) are at an angle, so the node x axis moves. I want the node to always be in front of the earth (for example, always on top of a line just created). Currently setting the node position is similar to locking the x axis (solving my problem):
override func didSimulatePhysics() {
Note: ^ This function is inside the GameScene class.
The x axis actually remains the same. However , the problem is that now the physical body of the node is smaller than the center of the node (which was not there before).
source share