0

I'm having a problem where a file in my Resource directory is not being recognized as being in the Main Bundle.

I am creating a texture atlas using a pvr file format using texturePacker. The output of texturePacker produces two files xxx_iPhone-hd.pvr.ccz and xxx_iPhone-hd.plist. These two files are being stored in my Resource directory of my cocos2d project. Once the files are stored in the Resource folder, I go to the Resource folder and right click to 'Add Files to "myProject"' (with the copy items into destination box unchecked).

When my cocos2d project runs and tries to load this file, the absolute path is unknown to the Main Bundle. Specifically, my code executes the following line of code:

backgroundBgNode = [CCSpriteBatchNode batchNodeWithFile:@"StoreMenuBackground_iPhone.pvr.ccz"];

The cocos2d code knows that the device has a retina display and modifies the filename to create a relPath of "StoreMenuBackground_iPhone-hd.pvr.ccz". It then tries to execute the following:

NSString *imageDirectory = [relPath stringByDeletingLastPathComponent]; 
fullpath = [[NSBundle mainBundle] pathForResource:file 
                                           ofType:nil 
                                      inDirectory:imageDirectory];

This results in full path being nil. I previously created a different xxx_iPhone5-hd.pvr.ccz file with the above procedure, and the above code successfully returns the fullpath. I have very carefully checked and rechecked the spelling and don't see any problems. I have also tried removing and re-adding the files under "Copy Bundle Resources" of the "Build Phases" for the project. Is there something I can do to force the mainBundle to find this file?

JeffB6688
  • 3,782
  • 5
  • 38
  • 58

2 Answers2

1

You should verify that the file actually is in the bundle.

You can do that by right-clicking the built bundle (in your project's DerivedData folder) and select "Show Package Contents". If you don't know how to locate the built product, do an Archive Build targeting a device (archive builds are unavailable for simulators). In the Organizer window that opens right-click the app and select "Show in Finder", that brings you to the xcarchive. Run "show package contents" on the xcarchive and navigate to /Products/Applications and perform another "show package contents" on the bundle.

If the file is there but in a subfolder (not in the root of the bundle) then you have created a folder reference inside Xcode, denoted by the blue folder icon. Remove that and re-add, this time uncheck the checkbox "create folder references for..." in the Add File dialog.

If the file IS there, copy its filename and paste it back into code. Case differences can sometimes be elusive and hard to notice, even if you double-check.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • I tried what you suggested. The file IS in the bundle. I copied and pasted the filename back into the code (both with and without the -hd suffix). I'm still having the same problem. fullpath is nil. Any other ideas? Doing a clean didn't help either. FYI: I am running on the simulator targeting iPhone Retina (3.5 inch) – JeffB6688 Oct 24 '13 at 22:53
  • 1
    maybe try deleting the app from the device, though normally you'll only have an issue with files *still* being in the bundle despite being removed from the xcode project. – CodeSmile Oct 24 '13 at 23:01
  • am placing bets on that last suggestion (device delete). – YvesLeBorg Oct 24 '13 at 23:02
  • @LearnCocos2d - I actually already tried that. Do you think I should reset content and settings on the simulator? Not sure what that does. – JeffB6688 Oct 24 '13 at 23:06
  • No that shouldn't make any difference. You could test if you can get a valid path returned by manually calling pathForResource with maybe difference combinations of filenames, imageDirectory=nil and so on. – CodeSmile Oct 24 '13 at 23:11
  • @LearnCocos2D - Ok, I tried combinations of filenames. If I use xxx_iPhone-hd.pvr.ccz, the cocos2d code finds the file and is happy. But if I use xxx_iPhone.pvr.ccz, it does not find the file. The only way this will work is if I create another texture atlas with the same image and call it xxx_iPhone.pvr.ccz. Then the cocos2d code will find this file, but will use the image from the xxx_iPhone-hd.pvr.ccz file. Oddly enough, this is not a problem with the use of my xxx_iPhone5-hd.pvr.ccz texture atlas (i.e. I don't have to have an hd and non-hd version for iPhone5). Thoughts? – JeffB6688 Oct 25 '13 at 15:05
  • 1
    Do you have only the -hd version but no version without any extension? If so, just use the retina files without the -hd extension if you aren't supporting non-Retina devices, or make sure you set the file suffixes and fallbacks accordingly via CCFileUtils. – CodeSmile Oct 25 '13 at 18:53
  • @LearnCocos2D - Yes, except for this one iPhone5 case, it seems that it fails when I am not supporting the non-retina devices. Is your suggestion of using the retina files without an -hd extension the norm for cases when you don't support non-retina devices, or is this a suggested workaround? – JeffB6688 Oct 25 '13 at 21:18
  • it's something you *can* do, not sure if that changes the code paths leading to the issue. – CodeSmile Oct 25 '13 at 21:45
0

desperate measures then. Copy in finder the files on the side ... ie not in the project directory structure. Rename the newly copied files to something dead simple , like a-hd.pvr.ccz and a.plist (hoping you dont already have an a-hd.pvr.ccz in your project ;) ).

Edit a-hd.plist to make certain that the last section points to the right file name. Drag them from Finder onto the xCode resources folder, selecting to copy the files. Make certain to change the name in your code. Delete the previous files from the project. Deep clean the project. Delete the app from the device. Run. If that worked, redo the same steps, reverting the name to your favourite name.

YvesLeBorg
  • 9,070
  • 8
  • 35
  • 48
  • Unfortunately, that does not work either. The only solution that seems to work is to create a non hd version of these files to trick cocos2d. – JeffB6688 Oct 25 '13 at 16:27