Pergunta

I'm diving in and trying to learn cocos2d. Currently I'm trying to swap a tileset on button press.

Example: Player is standing on grass, you press a button, and the tileset changes to a snow texture.

I used tiled to create the map, and I was hoping I would be able to just switch the image sheet that tiled is using to display the tiles.

The image sheets are the exact same size, with the exact same image positions..

If anyone has any advice or input, I would greatly appreciate it! :)

Foi útil?

Solução

The CCTMXLayer class that uses the tileset inherits from CCSpriteBatchNode. You could try to send the CCTMXLayer the setTexture: message with the new tileset's texture.

However just skimming the code there are areas where this will be an issue. For example setting the texture will certainly not change the texture of any tiles currently being child nodes of the batch node, which means they'll fail to render or things will crash because they would then use a different texture than the batch node. It may be possible to update all sprites' texture at the same time, but I don't think this will work because whether the batch node or a sprite's texture changes, it will be different from the batch node's texture and trigger an error.

One possible way seems to be to remove the entire CCTMXLayer and replace it with a new one. However the CCTMXLayer is initialized with structures used only internally, and at least some may only be available at load time. So in the end probably the only way to replace a tileset in plain cocos2d is to replace the entire CCTMXTiledMap node, which means a lot of overhead for something this simple.

You can of course add both grass and snow tiles to the same tileset and simply change the tile GID, if that is all you wanted to do.

And as I mentioned, KoboldTouch allows changing tilesets at runtime. KoboldTouch itself is using cocos2d-iphone as its renderer.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top