I have started with a v3.9 template generated project, and was able to see the hello world example when running cocos run -p mac
. I was going through the documentation and wanted to start with adding sprites, so I added a pair of plist and png files to ./Resources and added the following four lines of code to the end of HellowWorld::init()
:
auto shadowSpriteCache = SpriteFrameCache::getInstance();
shadowSpriteCache->addSpriteFramesWithFile("shadow.plist");
auto shadowSprite = Sprite::createWithSpriteFrameName("standing.png");
this->addChild(shadowSprite, 1);
Here is a directory list of the files in Resources:
ls
CMakeLists.txt MyGame.xcworkspace bin proj.android proj.ios_mac proj.win10 proj.win8.1-universal
Classes Resources cocos2d proj.android-studio proj.linux proj.win32
Adams-MacBook-Pro:MyGame adam$ ls -l Resources/
total 120
-rw-r--r-- 1 adam staff 3596 Nov 30 13:12 CloseNormal.png
-rw-r--r-- 1 adam staff 2810 Nov 30 13:12 CloseSelected.png
-rw-r--r-- 1 adam staff 37864 Nov 30 13:12 HelloWorld.png
drwxr-xr-x 4 adam staff 136 Nov 30 13:12 fonts
drwxr-xr-x 3 adam staff 102 Nov 30 13:12 res
-rw-r--r-- 1 adam staff 6720 Nov 30 10:39 shadow.plist
-rw-r--r--@ 1 adam staff 1838 Nov 30 10:39 shadow.png
After running cocos run -p mac
again, I found that the new code ran, but the new resources were not copied from MyGame/Resources to the build directory in MyGame/bin/debug/mac/MyGame-desktop.app/Contents/Resources/. So of course the app fails with:
cocos2d: fullPathForFilename: No file found at shadow.plist. Possible missing file.
cocos2d: SpriteFrameCache: can not find shadow.plist
cocos run -p mac
kicks off xcodebuild referencing the xcodeproject file in proj.ios_mac, which has each resource file hard-coded. I was not planning on using Xcode and wasn’t able to figure out Xcode how/if those hard-coded references get automatically updated or not by Xcode.
In MyGame/proj.ios_mac/MyGame.xcodeproj/project.pbxproj
I found references to the template HelloWorld resources, with lovely lines like so:
46880B8119C43A67006E1F66 /* HelloWorld.png in Resources */ = {isa = PBXBuildFile; fileRef = 46880B7A19C43A67006E1F66 /* HelloWorld.png */; };
Since codos run -p mac
is just doing a run like so:
running: 'xcodebuild -project "/Users/adam/projects/MyGame/proj.ios_mac/MyGame.xcodeproj" -configuration Debug -target "MyGame-desktop" CONFIGURATION_BUILD_DIR="/Users/adam/projects/MyGame/bin/debug/mac"'
I know the xcodeproj file is the culprit, but I’m not sure how to update it as I add new files, or why I should have to. Any advice on this issue would be greatly appreciated!
I saw a comment by @Mazyod about using FileUtil in AppDelegate to add a search path, but it didn’t have an impact. I tried in the constructor and the beginning of applicationDidFinishLaunching. I gave it a try, but I see this as being a temporary fix to get local development working, since it still wouldn’t be in the build output.
AppDelegate::AppDelegate() {
FileUtils::getInstance()->addSearchPath("Resources");
}