How to change center of gravity in sprite kit game?

前端 未结 2 1992
隐瞒了意图╮
隐瞒了意图╮ 2021-01-14 08:37

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 o

2条回答
  •  余生分开走
    2021-01-14 08:51

    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
    

提交回复
热议问题