Problem with TMX file and content

Topics: Content Building and Deploying, Sprites and SpriteBatch
Oct 3, 2013 at 2:28 PM
Hi I'm developing for windows and using visual studio with cocos2dxna.
I come from cocos2d for iPhone so I have some backgrond on how cocos2d works.
The thing is that now I'm having problem using the tmx importer. I have this right now:
var tiledMap = new CCTMXTiledMap();
        tiledMap = CCTMXTiledMap.Create("TileMap.tmx");

        AddChild(tiledMap);
And when I compile and run I get this message

__An unhandled exception of type 'Microsoft.Xna.Framework.Content.ContentLoadException' occurred in cocos2d-xna.dll

Additional information: Could not load the contents of TileMap.tmx as raw text.__

Anybody knows where or why the problem is happening?
Oct 3, 2013 at 7:06 PM
Do you have the cocos2d.Content.Pipeline.Importers project included in your solution? Do you have it referenced in your content project? Do you have the TMX file set to compile and to use the appropriate importer (TMXImporter) and processor (TMXProcessor)?

I had the same problem not too long ago and it was because of the importer and processor settings. I added my TMX file to the content project before referencing the cocos2d content pipeline project, so it couldn't default to the correct values when added and I forgot to correct the settings once the correct values were available. Took me longer to notice than I'd like to admit lol
Oct 4, 2013 at 5:14 PM
Probably I haven`t installed them correctly. What I dead was install NuGet package manager from VS and then get the cocos2d template.
I did some research and apparently to install cocos2dxna I need to do it using git right? I downloaded the project from

https://github.com/Cocos2DXNA/cocos2d-xna

and tried to run the build.bat but then I got an error that say that 'nant' is not recognized as an internal or external command. I think that I probably haven't
installed the framework at all. It was easier on OS X run a .sh file an everything would get installed.
So correct me if I'm wrong, but I need to follow the steps from github to install cocos2xna from any git client right?
Thanks for your respones1
Coordinator
Oct 7, 2013 at 6:28 AM
You don't need the content pipeline extensions if you are loading the assets as raw files.

in your case make sure the tiled map file is located in your Content folder in the output of your project. If the file is not found in this directory, then the runtime can not find it.
Nov 7, 2013 at 5:34 AM
I have the same problem, does anybody know how to solve this problem?
Nov 10, 2013 at 11:27 AM
After a six month hiatus from Cocos2D-XNA, I'm back and eager to reattemp creating a game with it. Good to see everything is coming along nicely with it and that more documentation is being released for people like me. I'm now facing the same issue with TMX files. From my understanding, the content pipeline extensions are no longer needed, so what is the process we should be doing now to get this working?

Here's what I am doing (which doesn't work).

1) Create a new solution in VS 2012 using the online extensions for Cocos2D-XNA. I choose the Windows GL one.
2) In the Content Folder created, I add a subfolder called TileMaps, then I add the existing file TestMap.tmx.
3) In one of my scenes, I try to load the map with:
 var map = CCTMXTiledMap.Create("TileMaps/TestMap");
 // or
 var map = CCTMXTiledMap.Create("TileMaps/TestMap.tmx");
4) When I run the game, I get the following exception when I try to load the tmx: "Could not load the contents of TileMaps/TestMap.tmx as raw text."

I've got the latest Cocos2D-XNA source from git (including its mono dependancy) to see how the Tests do this. The only difference I can see is that the content they attempt to load is compiled as an XNB file. But as far as I know it doesn't have to be pre-compiled now?

Totallyevil, Is there an up to date reference online you could point us to for the how we should be loading our TileMaps or assets in general?

Thanks!
Coordinator
Nov 11, 2013 at 4:12 PM
Hi, welcome back.

I recommend using raw streams for some of this content, like the TMX files, FNT files, etc. You can still use the custom content builder code, but you need the windows XNA core to build the XNB files.

Any time you see "FOO.Create(path)" the path is for the XNA ContentManager container. That's why you see the error now.

It doesn't look like the TMX support uses any of the embedded resource file support that has been added to some of the other custom content classes. We'll look at adding that for the next release.
Nov 14, 2013 at 2:16 AM
I meet the same problem,now I find the way out.
Here's what I am doing.
1)Create a new "MonoGame Content Project",add files you need into it.In my case ,"background0.png" and "Level0.tmx"
2)Add reference to cocos2d.Content.Pipeline.Importers
3)Set properties of tmx file,set "Content Importer" to "TMX Importer" and "Content Processor" to "TMX Processor"
4)Build Project,now you can find xnb file in "bin\PSM\Content" folder.In my case ,"background0.xnb" and "Level0.xnb"
5)Add xnb files into your project's Content folder and set properties "Copy To Output Folder"
6)Run your project and you can find the problem is solved.
Nov 14, 2013 at 9:23 AM
Thank you FranckyTang, that was exactly the kind of walk-through I was looking for!
Nov 14, 2013 at 10:23 AM
I may be missing a trivial step here, but when I add a "Cocos2D-XNA MonoGame Content Builder" project (from the online templates), files added to the project do not get Content Importer and Content Processor properties in Visual Studio 2010 or 2012. I do however get these properties if I add an XNA Empty Content Project (4.0) and reference cocos2d.Content.Pipeline.Importers, however I cannot then Build this project in order to create XNB files.

Any idea what I need to do in order to get the Cocos2D-XNA MonoGame Content Builder working as a Content project in Visual Studio?
Nov 14, 2013 at 4:56 PM
Hi ,Arkiliknam
The same thing happened to me.There is no "Content Importer" and "Content Processor" properties in "Cocos2D-XNA MonoGame Content Builder" project.I can't find a solution.
So I used "MonoGame Content Project" in MonoGame ,not "Cocos2D-XNA MonoGame Content Builder" in Cocos2D.
You can find MonoGame installer here:https://monogame.codeplex.com/releases/view/102870.
Coordinator
Nov 16, 2013 at 10:47 PM
Hello, the MonoGame Content Project and the Cocos2D-XNA MonoGame Content builder should be the same, so using the one that works properly should be fine.

If you can create an XNA 4 content project, then just create a dummy windows project that links to the XNA 4 content project. Then add a post build task that copies the xnb files from the dummy windows project over to your PSM project "Content" directory.

that solution will only work while you can use the XNA Game Studio 4 refresh 1. On a fresh windows 8 install without upgrading a prior windows install the XNA 4 studio will not work.
Dec 6, 2013 at 9:40 PM
hi everybody,

I stumpled across this same problem and tried every solution discribed here and they dont seem to work for me or i can't get them to work, well either way I would like to try using raw streams as you pointed out, totallyevil. but i dont find any option to load a TMX map in a CCTMXTiledMap object via raw stream.
Maybe I'm looking wrong, any advice or help would be highly appreciated.
Coordinator
Dec 7, 2013 at 6:55 PM
So you don't have any access to a Windows 7 machine with XNA Studio 4r1 on it?

I thought we had a test case in the test bed that loaded a TMX file from a stream, such as an embedded stream.
Dec 7, 2013 at 9:20 PM
Edited Dec 7, 2013 at 9:54 PM
I am using windows 7 and i have XNA Studio 4r1

if i add a .tmx file to my project i dont see any "Content Importer" and "Content Processor" properties and i can't find the cocos2d.Content.Pipeline.Importers to add to references.
(i just downloaded the project template "Cocos2D-XNA Empty Game Windows GL")

I see if i can find the tests and take look at them thanks for the tip.
Coordinator
Dec 7, 2013 at 10:07 PM
I suggest you use the github source, where you will find both the framework project and the pipeline importer project.

Once there, you add the tmx file to your XNA4 content project. Set it to process using the cocos2d provided content processor, and then reference it using the ContentManager.

The project templates don't work well for this kind of project work because they can't include the content project extensions. VSIX is limited to a single project template, and XNA4 projects need to also have the content project.
Dec 8, 2013 at 11:11 AM
Edited Dec 8, 2013 at 11:49 AM
so after looking through the source code of the Tests i couldn't find any test that would something else then "CCTMXTiledMap.Create(...)", and as i understand it that always uses the XNA Content Manager and not a raw stream.
I would still prefer to just use a relativ or absolut path to a TMX file without the whole XNA Content hassle.

anyway after looking through the tests i downloaded the github source as you suggested and compiled the pipline importer project, added a XNA4 content project to my solution (with the Cocos2D game project) and added the TMX file to it, referenced the cocos content pipline and changed the Importer and Processer to TMX.

But i still get the same Error "Could not load the contents of TileMap.tmx as raw text."

i tried it with: CCTMXTiledMap tiledMap = CCTMXTiledMap.Create("TileMap.tmx");
      or    CCTMXTiledMap tiledMap = CCTMXTiledMap.Create("Content/TileMap.tmx");
      or    CCTMXTiledMap tiledMap = CCTMXTiledMap.Create("Content1/TileMap.tmx"); //because the Content Root Directory from the content project is called content1 (also renaming it or the contentfolder of the Cocos2dgame didn't change anything)
same error everytime (well only the path in the error changes)

I think i'm missing something crucial here, which is kinda frustrating, but regardless thank you very much for your help so far!
Dec 8, 2013 at 11:42 AM
Draugor, as you know, I also had lots of issues with getting this working. What I ended up doing was following a combination FrankyTang and TotallyEvil's advice.

1) Download and instal MonoGameContentBuilder. (I tried using the Cocos2D-XNA ContentBuilder, but I couldn't build content files with it.)
2) Add a MonoGameContent project to you solution
3) To the (Content) project, add a Reference to cocos2d.Content.Pipeline.Importers
4) Add you tmx files to this project
5) Set the tmx file properties to Compile and use TMX Importer and TMX Processor (these should be available once you add the reference to cocos2d.Content.Pipeline.Importers
6) Build the MonoGameContent project (not the (Content) one), this should compile the tmx files and add them to the output dir "bin\PSM\Content" of the project as .xnb files
7) Add the the .xnb files to the content folder of your Cocos2D-XNA game project
8) Set the properties of the xnb files to Content and Copy If Newer to ensure they get copied to the output dir of the game
9) Add the map to the game like so:
var map = new CCTMXTiledMap();
map.InitWithTmxFile("MyMapName");
AddChild(map);
I don't think the Create methods exist anymore, or are being made obsolete as they go. I also don't think the loading as stream is implemented yet (at least not in the source I am using from Git last time I pulled it).

That should do it.

I had so many issues with VS along the way as well. I recommend using a Windows 7 PC with VS 2010 as dev environment. Windows 8 and VS 2012 have been nothing but headaches for me.


Off topic a bit, but I've got a few other issues at the moment with tmx and Cocos2d-xna worth mentioning...

Cocos2d-xna seems to render the tiles incorrectly in my implementation. It always seems to grab the bounds of the tile with 1 pixel from its neighbour. So my Gun tile which is next to my Wall tile has the 1 pixel wide wall running along the side. I first thought that my tiles were too big, so I deleted that one pixel, so the Gun tile worked, but the wall tile then was missing 1 pixel at its side. I then added a 1 pixel grid to my tile map and set spacing to 1 pixel, but its the same issue, and now the grid makes it into the tiles when Cocos2d-xna renders them.

Also, I loop through all the tiles in my main layer to find out if they are walls or not using:
var tileGid = layer.TileGIDAt(new CCPoint(x, y));
if (tileGid > 0)
{
    var props = map.PropertiesForGID(tileGid);
    // do something
}
but I notice that a bunch of the GIDs that TileGIDAt returns apparently don't exist when I use map.PropertiesForGID(tileGid). If I look at the TMX file, that GID is in there, and it does have properties, but for some reason Cocos2d-xna throws and exception, so I've resulted in using a try catch block around them for now.
Dec 8, 2013 at 2:23 PM
thank you very much Arkiliknam, it looks like it is at least trying to load the map now.
It still gives me an Error, a "DivideByZeroException", but that can just be a problem with my map.
Dec 8, 2013 at 5:00 PM
Just a guess, but the divide by zero error may be due to how the tmx file is referencing the path to the images it uses. If you have them in the same folder, just open the tmx file up in a text editor and delete the ../xyz/etc/ until its just the image file you want.
Coordinator
Dec 8, 2013 at 5:03 PM
Arkiliknam, the problem with the 1 pixel bleed may be from the stretch-by-1-texel option in the TMX renderer. In some cases, this option is necessary to remove a 1-pixel-short rendering problem that causes red lines to appear on the map. I really can't say what combinations of options causes this, as I have only seen it once on an xbox 360.

As for loading TMX files, know that the "can not load TMX file" error may also be an indication of it not being able to load the textures associated with the TMX file. Check the log output to see if there are any content manager exceptions.
Dec 8, 2013 at 5:27 PM
TotallyEvil, I think I may have stumble across the bleeding issue. I think I was referencing the Cocos2d-xna dll downloaded through VS, so updated it to use the one compiled by your git source. Problem goes away. However, if I turn on Anti Aliasing, it comes back. I think the may be something funny going on with the scaling too, as I'm noticing rows/columns of pixels are being cut out when rendered. It's strange though, as I am using Scale = 1 and would expect it to render as it is. I'm not sure if it makes any difference, but I'm doing all this in a WindowsGL project (for easier testing).
Coordinator
Dec 8, 2013 at 6:03 PM
Coordinator
Dec 8, 2013 at 6:04 PM
We'll check the WindowsGL tests to see if they suffer the same issues.
Dec 10, 2013 at 3:11 PM
Edited Dec 10, 2013 at 3:20 PM
FYI. The pixels being cut out/definition loss (not the border bleeding you mentioned) is on all sprites, not just TMX related and is caused (in this case) by the scaling which is done by:
CCDrawManager.SetDesignResolutionSize(preferredWidth, preferredHeight, resPolicy);
So not really an issue. I just have to be aware of how images will be scaled.

Regarding TMX border bleeding: turning off Anti-aliasing seems to have fixed it, but I was experimenting with scrolling the entire map to follow the character using a MoveBy action on the CCLayer itself. Most of the time it looks fine, though occasionally the bleeding borders flash into the rendered image. This is happening in a WindowsGL project, and is visible even after stripping out the SetDesignResolutionSize so that scale remains 1:1.
Coordinator
Dec 11, 2013 at 4:31 AM
That code comes out of the vsix template.

The design resolution should not be based on your preferred window width/height. it's the dimensions that you used to design your game. The design resolution scale will adjust your assets so that they fit into the viewport dimensions according to the resolution policy you choose. It's the resolution policy that determines how your sprites are scaled.

You should not remove this line of code. Just make sure you choose the correct combination of parameters. the vsix template is a little misleading in its naming of the dimension variables.