Relationship between SKSpriteNode - swift

Note: I tried this answer: Gap between SKSpriteNodes in SpriteKit conflict detection

I get gaps between my SKSpriteNodes, 5 minutes after starting my game. Here is my code to make node:

let tileNode = SKSpriteNode(imageNamed: "world1Tile\(tileNumber).png")
tileNode.position.x = x
tileNode.position.y = y
tileNode.size.width = 128
tileNode.size.height = 128
tileNode.zPosition = 10
tileNode.physicsBody = SKPhysicsBody(rectangleOf: tileNode.size)
tileNode.physicsBody!.isDynamic = false
tileNode.physicsBody!.restitution = 0
tileNode.physicsBody!.allowsRotation = false
tileNode.name = "Tile"
row.append(tileNode)

      

When I remove the physical body, it works fine. Here are some images to show you what I mean: With accepted physics.

This image has a physical body and was taken immediately after launching the application.

This image was taken 5 minutes after launching the application.

This image was captured 5 minutes after launching the application.

Why is this happening? I assume it has something to do with the physical body, because my application looks exactly the same as the first image, even an hour after starting the application if there is no physical body. What physical body structure should I change to prevent this from happening? Any help would be appreciated.

+4


source to share


4 answers


I am assuming there is no space. you probably have "showPhysics" set to true in your gameview controller and that line looks like a space to me.



compare position with and without pb check.

+1


source


I had a similar problem not too long ago where gaps appeared between nodes that were tiled (although I didn't use physics). Based on this answer , I found that if you want perfect alignment between nodes, your best bet is to make sure that the positions of the nodes and the width and height of the nodes are integers.



I would suggest rounding up the values x

and y

positions tileNode

and see if that makes any difference.

+2


source


I had a similar problem where gaps between sprites started to appear after about 5 minutes of scrolling at a constant speed (infinite scrolling game). I didn't use physics, and I even had all positions, widths, heights rounded to whole values. I scrolled the camera and added new sprites one by one and everything worked fine, except after about 5 minutes, infinite scrolling gaps started to appear, just like in your case. I spent some time looking for a solution and it turned out that the problem was that when the positions of my objects got large, i.e. in my case the X position in the scene was around 150,000, then these gaps started to appear and I also noticed that this problem only occurred on devices that needed to scale the scene.I used the aspect format with the default scene size for the iPhone 6 resolution, and these spaces only appeared on the iPhone 5, but I didn't notice them on the iPhone 6. I was able to solve this problem by subtracting some constant value from the X position of all objects (including the camera position) from time to time so that everything in the scene relatively does not change the camera position and looks the same, but in fact the absolute positions were changed to keep them low ... I am guessing that large position values ​​such as 150000 and scene scaling are causing some kind of floating point rounding issue in SpriteKit and this is why whitespace becomes visible.subtracting some constant value from the X position of all objects (including the camera position) from time to time, so that everything in the scene relatively doesn't change the camera position and looks the same, but actually the absolute positions were changed to keep them low. I am guessing that large position values ​​such as 150000 and scene scaling are causing some kind of floating point rounding issue in SpriteKit and this is why whitespace becomes visible.subtracting some constant value from the X position of all objects (including the camera position) from time to time, so that everything in the scene relatively does not change the camera position and looks the same, but in fact the absolute positions were changed to keep them low. I am guessing that large position values ​​such as 150000 and scene scaling are causing some kind of floating point rounding issue in SpriteKit and this is why whitespace becomes visible.cause some issue with floating point rounding in SpriteKit, which is why whitespace becomes visible.cause some issue with floating point rounding in SpriteKit, which is why whitespace becomes visible.

From my experience, if you have gaps like this, I recommend using integer values ​​for all positions, widths, heights, and additionally keeping the object position values ​​of all objects low.

+1


source


In the future, for everyone in need, I have found a different answer, specific to my problem. As JohnV said, I might need to round off the values ​​when setting the position, but when I ran the code, I figured out that I also need to do this when I run SKActions.

0


source







All Articles