What’s New In SpriteKit on iOS 10: A Look At Tile Maps

@stefano -

var physicsBodyArray = [SKPhysicsBody]()
let physicsBody = SKPhysicsBody(rectangleOf: size)
physicsBodyArray.append(physicsBody)

When the array has all the bodies:

tileMapNode.physicsBody = SKPhysicsBody(bodies: physicsBodies)

(might be bodiesWithBody for Swift 2)

And for checking by name, you can use userData for this. That’s in the tutorial - you get the tile definition for the row/column and then get the tile definition’s user data:

if let _ = tile?.userData?.value(forKey: "Wall") {
  // make something
}

Thank you very much!
But what function i need, to check all tiles in SKTileMapNode?

You would have to loop through all the tiles:

for row in 0..<tileMapNode.numberOfRows {
  for column in 0..<tileMapNode.numberOfColumns {
    let tile = tileMapNode.tileDefinition(atColumn: column, row: row)
    // do something to the SKTileDefinition
   }
}

But I wouldn’t do that to check a single tile. To check a single tile, you use a CGPoint, for example the player’s position and get the tile from that.

Or if you’re checking a collision with a wall tile, create a physics body with a category of type wall.

Caroline, thank you for help!

And last question, can I use SKPhysicsBody(texture: , size: ), In SKTileMapNode?

I haven’t tried it - perhaps you could report back what you find :slight_smile:. I would guess as SKTileMapNode has a physicBody property of SKPhysicsBody, you would be able to use any of the SKPhysicsBody initializers.

What texture were you thinking of using though?

Oh, i just want to set physicsBody for every tile using SKPhysicsBody(texture: , size: ) not SKPhysicsBody(rectangleOf: size), but i have a problem because SKPhysicsBody(texture: , size: ) don’t have a position :frowning:

That sounds as if it might be slow, but you can always try it.

If you are creating an SKNode per tile, then all you do is create the physics body for that SKNode using your texture, and then you move the SKNode into the correct position. You can find out the x, y position by multiplying the tile column by the tile width and multiplying the tile row by the tile height.

Caroline it works fine! Thank you for all your answers :slight_smile: They are so useful!

Hi,

I’m trying to load a tileset, which I’ve created before using the Xcode 8 Beta 2 tile set editor using this code:

tileSet = SKTileSet(named: GameConstants.basicTileSet)

But the tileSet - Variable stays nil and I get the following output a couple of times on the Xcode console:

SKUtil.m: MGGetBoolAnswer is not available in the simulator.

I’ve search Google but couldn’t find a answer. Any ideas on that this means and why the tile set is NOT loaded?

Thanks in advance.

Cheers,

Frank

@marxfr - I made this comment in the tutorial:

Initially, I wanted to create the tile map node in code and fill it with a named tile set, but at the time of this writing, I wasn’t able to retrieve an SKTileSet using the name initializer.

Perhaps you could file a radar?

I did manage to create a SKTileset completely in code by first creating a texture from an image, then a rule, then an adjacency mask, then a tile group then do SKTileSet(tileGroups: [tileGroup]

Hi,

well I can create a tile set tile by tile - more or less the same way you explained it.

But from my understanding it should be also possible to create a tile set using this new fancy editor in Xcode and then
load the newly created tile set using the SKTileSet( named: “nameOfTiletSet(.sks)” ) .
At least this is my understanding of the API.

Were you able to load a pre-defined tile set, which you’ve created in the editor, this way?

Cheers,

Frank

PS: What is a “radar”?

Hi,

one approach would be to somehow get all the files or image sets in a certain asset catalog. And create the
Tiles and then the tile set from there.

But it would still be a work around which wouldn’t be satisfying - I hope they fix this SKTileSet( named: “name” ) - problem.

Cheers,

Frank

Basically - because all the other stuff somehow fails - at least on the simulator, haven’t tried it on my real AppleTV because I don’t want to update it to a beta version and I haven’t bought a second device so far - I want something like this:

Give me all the images which are in a particular asset catalog - or get my hand on a particular asset catalog somehow and
iterate over it in some way.

Loading each texture with it’s name is the last resort. I’m still curious while the SKTileSet with Name doesn’t work.

Some things don’t work on the Simulator, for example getting the X- and Y-Coordiantes of the DPAD using callbacks fail completely but works on a the real device.

Cheers,

Frank

@marxfr - a radar is an old-fashioned way of filing an Apple bug. Now they have a site for it:

https://bugreport.apple.com/

I haven’t been able to get a named group from a .sks file. But it’s always possible I’ve been doing it wrong :smiley:

The only way I can think of going through all the images in an asset catalog is not to use an asset catalog, and have the images in a folder. You can then use FileManager to iterate through the files. But that’s a pain.

You could create an array with constant names for all the images and iterate through that. Actually that would be preferable to not using the asset catalog.

Remember Xcode 8 is still in Beta. Keep hoping it comes together in the final release!

Hi,

I guess it has to do with the beta status.

I’m pretty sure that I’m using the SKTileSet - Initializer right. So there is no way right now to load a tile set programmatically. Neither with SKTileSet nor with NSAssetData which is supposed to look into the compiled assets
of the bundle.

I developed a workaround which loads the tiles each separately using some enumeration and array stuff.

It encapsulates this functionality so it can be later changed without effecting the rest of the Application.
So if it works the way it supposed to work in the future I can just change this part of the code and it will affect nothing else.

Cheers,

Frank

PS: Thinks have to get better in the final release - they cannot ship this buggy piece of software.

Hi,

I’ve posted this issue on one of the Apple-Forums and I got a response. They were able to load a tile set usind:

SKTileSet( name: “nameOfTileset” )

See SKTileSet( named: "" ) not working… | Apple Developer Forums

I will try it out tonight to see if it works.

Cheers,

Frank

@marxfr - I can confirm this works. I’m pretty sure I tried this in previous betas. But it’s working in Beta 4.

:grinning:

Hi,

but in previous posts you said it didn’t work - so it didn’t work in Beta 2 & Beta 3 ?

“I wanted to create the tile map node in code and fill it with a named tile set, but at the time of this writing, I wasn’t able to retrieve an SKTileSet using the name initializer” - this sounds a kind of confusing now.

So it worked once and stopped working and now it works again?

And it works now in Beta 4?

Cheers,

Frank

When it works in something I always wonder whether I was doing it wrong previously. It’s always possible, but I did spend a large amount of time on it. I don’t have beta 2, so can’t check.

But it worked for me in Beta 4. And may also in beta 3.

However, I can’t advise upgrading to Beta 4 because the SpriteKit scene editor crashes immediately - so don’t lose your copy of beta 3.