3
votes

I'm learning Swift to create a game for iOS, but I'm struggling to work out how the sprites will collide and interact with the background.

The basic game will be a ball that you guide down a maze, but the maze can be several times bigger than the screen. The maze could be a large bitmap, or made up of tiles but I haven't decided yet based on how to make the ball interact with the maze.

I could make all the tiles in the maze sprite nodes and then let SpriteKit work it out, but I think that's not a great choice.

Then there's SpriteKit edge bodies as show here - https://developer.apple.com/library/mac/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Physics/Physics.html Could I use these on the parts of the maze that the ball might collide with?

The other issue is that the maze surfaces might be rounded so the collisions and physics would need to cope with that too.

All of the examples I can find seem to be sprites colliding with other sprites. But what about sprites and a background?

Does anyone have any clever ideas on how to solve the problem, or can you point me at an example that may help please?

Mark

1
I recommend making the walls into sprites (L shaped, I shaped, etc.), and then just creating a SpriteKit scene and drop the walls. This will allow you to set the physics category of the walls easy and allows easy interaction with the ball.Jose Ramirez

1 Answers

1
votes

There is no right or wrong way to do this.

In the manner of tiles vs 1 solid background you need to ask yourself some questions:

How much memory do I want to consume?
How much loading time do I want to use on it?
How detailed do I want the background?
How versatile (amount of change) do I want to make the background?
Is the background larger than max texture size?

After you decide what you want, you will find it easier to determine the method you will use.

Your physics is a different problem, remember, physics and nodes are 2 different things here, so your physics body does not have to reflect a node design, allowing you to do things like invisible walls. You have a few approaches with this:

You can use SKPhysicsBody(texture:) to use a texture to make your walls (transparency would not have collision.)
If you are doing tiles, you can attach a body to each tile.
Finally, You can generate a ton of bodies and merge them together into your background node.

Again, depending on how you want your map to work will tell you which option is best. Personally I would start with the last option (merge a bunch of bodies together,) since this will allow you to merge edge bodies, for all concrete walls, then if you need to make things like doors or temporary walls, I would add them in separate nodes.

To the issue with round parts.

SKPhysicsBody allows for CGPath to be used, so just create a path for any shape you need.