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
}
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.
I haven’t tried it - perhaps you could report back what you find . I would guess as SKTileMapNode has a physicBody property of SKPhysicsBody, you would be able to use any of the SKPhysicsBody initializers.
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
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.
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]
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?
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.
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
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!
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.
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?
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.