I have a number of individual nodes (tiles) that make up the background of my game. They're each 32x32p, and are positioned next to each other to form floor/ roof/ obstacles. I load a map from a JSON-file, and position the nodes on a background-layer/node based on the contents of the JSON-file when the scene is initialized.
This is working fine, however, I'm experiencing some bugs when moving the background layer/ the physics engine is doing its thing. Some of the nodes move 1 point/pixel away from each other, creating a gap between them. And since the background is a different color, this looks really bad.
The problem mostly occurs further out on the map (not right away), at the same time as I'm either applying impulses to the player or the physics engine is bouncing the player (or similar).
Here's a picture that illustrate my problem:
(Click to open in separate tab)As you can see, right between the o and d in nodes, there is a gap (more easily visible in the full sized image in the link). There are many gaps like these occurring now and then while playing the game, and they only appear for a split second or two. Sometimes they stay when the player stops moving.
I move the player in the didSimulatePhysics
by increasing/ decreasing its x-value
. Then lastly in the didSimulatePhysics
I call this method to center the worldNode
on the player:
- (void)centerViewOn:(CGPoint)centerOn {
CGFloat x = Clamp(centerOn.x, self.size.width / 2, _bgLayer.layerSize.width - self.size.width / 2);
_worldNode.position = CGPointMake(-x, _worldNode.position.y);
}
The worldNode
contains both the bgLayer
which contains all the individual background nodes, and the player itself.
What is the best way to solve this? One plausible solution could be to make every tile 33x32p, so they're overlapping. In that case you won't see the gaps occurring now and then. But I think it would be better to "kill" the problem rather than hide it. Anyone have experience laying out their map like this? Or have anyone experienced the same problems before? And if so, how did you solve it?
Thanks in advance!
This is the casting to Int solution based on @LearnCocos2D comment:
- (void)centerViewOn:(CGPoint)centerOn {
CGFloat x = Clamp(centerOn.x, self.size.width / 2, _bgLayer.layerSize.width - self.size.width / 2);
_worldNode.position = CGPointMake(Int(-x), Int(_worldNode.position.y));
}
来源:https://stackoverflow.com/questions/24921017/spritekit-nodes-adjusting-position-a-tiny-bit