Search code examples
sprite-kitxcode10gameplay-kitswift4.2

SpriteKit: Removing gravity from SKSpriteNode doesn't work


I'm trying to remove the gravity from SKSpriteNode but is not working. Here is my code:

I add add the SKSpriteNode:

let block: SKSpriteNode = self.childNode(withName: "block") as! SKSpriteNode

I add the gravity thru the Scene.sks

I tried all this ways to remove the gravity but none of the worked:

block.physicsBody?.affectedByGravity = false
block.physicsBody?.isDynamic = false
block.physicsBody?.velocity.dy = 0
block.physicsBody?.categoryBitMask = 0

How can I remove the gravity from SKSpriteNode ? any of you knows

I'll really appreciate your help.


Solution

  • It looks like (from the given code.) that your sprite is a color sprite in GameScene.sks. If so, this is an easy fix.

    Just remove gravity from inside the GameScene.sks file:

    Image 1

    Image 2

    Image 3

    Switch physics definition to none, or remove the tick from "Affected By Gravity"

    If you are trying to remove gravity from the sprite in the code, correct me if I'm wrong, but I don't think you can. I prefer to set up the physics in my scene manually to avoid errors like this.