{"id":1534,"date":"2016-11-06T02:05:34","date_gmt":"2016-11-05T23:05:34","guid":{"rendered":"http:\/\/hxml.me\/blog\/?p=1534"},"modified":"2016-11-06T02:12:11","modified_gmt":"2016-11-05T23:12:11","slug":"cocos2d-js-zagruzit-vneshnie-audio-fajly-i-javascript-kod","status":"publish","type":"post","link":"https:\/\/hxml.me\/blog\/coding\/cocos2d-js-zagruzit-vneshnie-audio-fajly-i-javascript-kod\/","title":{"rendered":"<!--:en-->Cocos2d-JS: load external audio assets and javascript codes<!--:--><!--:ru-->Cocos2d-JS: \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0432\u043d\u0435\u0448\u043d\u0438\u0435 \u0430\u0443\u0434\u0438\u043e \u0444\u0430\u0439\u043b\u044b \u0438 javascript \u043a\u043e\u0434<!--:-->"},"content":{"rendered":"<p><!--:en--><img loading=\"lazy\" decoding=\"async\" class=\"alignleft\" src=\"http:\/\/hxml.me\/img\/post\/cocos\/cocos2d.png\" alt=\"\" width=\"256\" height=\"192\" \/>Today I would like to tell about a quite complicated problem, which I have recently managed to resolve. The major sense of it can be found in the title of the post. One may as a question: what is the way this feature can be useful? It seems like no one was wondering this problem before, because Google was extremely poor for the answers. It is very logical and obvious:<strong> this is so much easier to debug<\/strong>, isn&#8217;t it? In order to try out new sounds or changes in the level logic, <strong>you do not need to recompile the entire project<\/strong> each time, you don&#8217;t even need to restart the application instead. Let me tell you how I managed to do it.<\/p>\n<p><!--:--><!--:ru--><img loading=\"lazy\" decoding=\"async\" class=\"alignleft\" src=\"http:\/\/hxml.me\/img\/post\/cocos\/cocos2d.png\" alt=\"\" width=\"256\" height=\"192\" \/>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u044f \u0445\u043e\u0442\u0435\u043b \u0431\u044b \u043f\u043e\u0432\u0435\u0434\u0430\u0442\u044c \u043e \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u043d\u0435 \u043d\u0435\u0434\u0430\u0432\u043d\u043e \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u0440\u0435\u0448\u0438\u0442\u044c. \u041a\u0440\u0430\u0442\u043a\u0443\u044e \u0441\u0443\u0442\u044c \u0435\u0435 \u043c\u043e\u0436\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0432 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0435 \u043f\u043e\u0441\u0442\u0430. \u041c\u043e\u0436\u0435\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442\u044c \u0432\u043e\u043f\u0440\u043e\u0441: \u0437\u0430\u0447\u0435\u043c \u0442\u0430\u043a\u043e\u0435 \u0432\u043e\u043e\u0431\u0449\u0435 \u043c\u043e\u0433\u043b\u043e \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f? \u0422\u0430\u043a\u043e\u0435 \u043e\u0449\u0443\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e \u043d\u0438\u043a\u0442\u043e \u0434\u043e \u043c\u0435\u043d\u044f \u044d\u0442\u0438\u043c \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u043c \u043d\u0435 \u0437\u0430\u0434\u0430\u0432\u0430\u043b\u0441\u044f, \u0438\u0431\u043e \u0433\u0443\u0433\u043b \u0431\u044b\u043b \u043a\u0440\u0430\u0439\u043d\u0435 \u0441\u043a\u0443\u0434\u0435\u043d \u043d\u0430 \u043e\u0442\u0432\u0435\u0442\u044b. \u0412\u0435\u0434\u044c \u044d\u0442\u043e \u0436\u0435 \u043e\u0447\u0435\u043d\u044c \u043b\u043e\u0433\u0438\u0447\u043d\u043e \u0438 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e &#8212; <strong>\u0442\u0430\u043a \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u043b\u0435\u0433\u0447\u0435 \u043e\u0442\u043b\u0430\u0436\u0438\u0432\u0430\u0442\u044c<\/strong>. \u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0437\u0432\u0443\u043a \u0438\u043b\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432 \u0443\u0440\u043e\u0432\u043d\u0435\u0432\u043e\u0439 \u043b\u043e\u0433\u0438\u043a\u0435, <strong>\u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043f\u0440\u043e\u0435\u043a\u0442<\/strong>, \u043c\u043e\u0436\u043d\u043e \u0434\u0430\u0436\u0435 \u043d\u0435 \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435. \u0421\u0435\u0439\u0447\u0430\u0441 \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443, \u043a\u0430\u043a \u043c\u043d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u044d\u0442\u043e\u0433\u043e \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f.<\/p>\n<p><!--:--><!--more--><\/p>\n<p><!--:en-->It is important to make a little remark. Though the project is cross-platform (target platforms &#8212; iOS and Android), the given solution is going to be basically for iOS, because the considering functionality is basically required for the period of debugging, and I prefer to debug mobile projects on iOS devices. However,\u00a0this solution is applicable for Android as well, you just\u00a0need to add a bit of native code. Here is the way it works briefly: a <strong>collection of game sounds <\/strong>is<strong>\u00a0<\/strong>stored <strong>on an external server<\/strong> (where they can be manipulated: add, delete, change), also there is a sort of service screen in the game, where the assets are being loaded. <img decoding=\"async\" class=\"alignnone\" src=\"http:\/\/hxml.me\/img\/post\/cocos\/1a.png\" alt=\"\" width=\"480\" \/> Files are stored locally in the <strong>standard Documents folder<\/strong>. I also provided a kind of versioning based on checksums calculation &#8212; it allows to avoid not to download all files every time, only those that was really changed. I designed a web-based console for more convenience. <img decoding=\"async\" class=\"alignnone\" src=\"http:\/\/hxml.me\/img\/post\/cocos\/2.png\" alt=\"\" width=\"640\" \/> I&#8217;m not going to describe all updating mechanics here &#8212; it is not the essence of this\u00a0article. Let&#8217;s see how these <strong>files get into the JS environment<\/strong>. Sounds playback is performed by AudioEngine module which is the part of <strong>Cocos2d framework<\/strong>. You can playback audio file with following:<\/p>\n<pre lang=\"javascript\">cc.audioEngine.playEffect(soundPath, repeat);<\/pre>\n<p>here soundPath is the path to audio asset. The main mysterious was that all manuals and forums claimed this parameter was a relative path, and Cocos2d is looking for specified file in the directories listed in searchPath collection. You can add the item to this collection the following way:<\/p>\n<pre lang=\"javascript\">jsb.fileUtils.addSearchPath(resPath);<\/pre>\n<p>but, as I figured\u00a0out, resPath is also considered by Cocos2d as a relative, so it looks for this file in the Application Bundle folder. The salvation was in the fact that as a parameter soundPath playEffect function, you can specify an <strong>absolute path<\/strong>! All we have to do is to supply playEffect function with the path to an audio file that can be located anywhere in the file system. How to tell a javascript code, where to get the audio file? In order to do this, the creators of cocos2d provided a <strong>binding<\/strong> mechanism for <strong>javascript<\/strong> (JSB), with the help of which <strong>native Objective-C methods can be called from javascript<\/strong> code. One option of this interaction is to create Objective-C class with a set of static methods, which will be called from a javascript-code. Here is the example:<\/p>\n<pre lang=\"objc\">#import \r\n\r\n#define LOAD_REMOTE_ASSETS YES\r\n\r\n@interface NativeOcClass : NSObject \r\n+(NSString *)callNativeLoaderPathForAudioAsset:(NSString *)soundId;\r\n+(NSString *)callNativeLoaderLevelSource:(NSString *)campaign :(NSString *)levelName;\r\n\r\n@end<\/pre>\n<p>I declared two methods here (the second one will be described a bit later). For now we going to speak about first method callNativeLoaderPathForAudioAsset: &#8212; this method returns the\u00a0absolute path to an audio file with soundId identifier specified. Here is the implementation:<\/p>\n<pre lang=\"objc\">+(NSString *)callNativeLoaderPathForAudioAsset:(NSString *)soundId {\r\n    if (!LOAD_REMOTE_ASSETS)\r\n        return @\"\";\r\n    NSUserDefaults * ud = [NSUserDefaults standardUserDefaults];\r\n    NSDictionary * files = [ud objectForKey: @\"remoteAssets\"];\r\n    NSString * revisionKey = [NSString stringWithFormat:@\"revision_%@\", soundId];\r\n    NSString * assetsDirPath = [NativeOcClass callNativeDocumentsResPath];\r\n    NSString * audioPath = [assetsDirPath stringByAppendingPathComponent:@\"res\/audio\"];\r\n    NSString * soundIdRevision = [NSString stringWithFormat:@\"%@_%@\", soundId, [files objectForKey:revisionKey]];\r\n    NSString * filePath = [audioPath stringByAppendingPathComponent:soundIdRevision];\r\n    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])\r\n        return filePath;\r\n    return @\"\";\r\n}<\/pre>\n<p>Don&#8217;t look at the logic related to download mechanics. The major idea is that this method can return an absolute path to the file or empty string if it doesn&#8217;t find a requested file in the Documents folder. In this case javascript code will use asset from Application bundle folder. This method is <strong>called from javascript<\/strong>:<\/p>\n<pre lang=\"javascript\">soundPathForCode: function(soundId) {\r\n  var filePath = (jsb.reflection.callStaticMethod(\"NativeOcClass\", \"callNativeLoaderPathForAudioAsset:\", soundId));\r\n  if (filePath!=\"\")\r\n    return filePath;\r\n  return \"res\/audio\/\" + Sounds[soundId];\r\n},<\/pre>\n<p>here NativeOcClass is the class where callNativeLoaderPathForAudioAsset: method is declared. As you can see it&#8217;s pretty straight forward. As I previously said we specify the path we received from callNativeLoaderPathForAudioAsset methods as an argument of playEffect function.<\/p>\n<pre lang=\"javascript\">var soundPath = this.soundPathForCode(\"player_jump\");\r\ncc.audioEngine.playEffect(soundPath, repeat);<\/pre>\n<p>I used the same approach for implementing <strong>remote downloads<\/strong> of level&#8217;s <strong>javascript code<\/strong>. callNativeLoaderLevelSource is designed for this purpose. It returns javascript code string of level which is synchronized from the server exactly as the sound assets. The only difference with\u00a0audio files is that this code is being interpreted by eval mechanism of Javascript engine.<\/p>\n<pre lang=\"javascript\">recreateLevelIfNeeded: function(campaign, levelNumber) {\r\n  var jsFileName = \"level\"+levelNumber;\r\n  var levelJsCode = jsb.reflection.callStaticMethod(\"NativeOcClass\", \"callNativeLoaderLevelSource::\", campaign.toLowerCase(), jsFileName);\r\n  if (levelJsCode != \"\") {\r\n    eval(levelJsCode);\r\n  }\r\n},<\/pre>\n<p><!--:--><!--:ru-->\u0421\u043b\u0435\u0434\u0443\u0435\u0442 \u0441\u0440\u0430\u0437\u0443 \u043e\u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c\u0441\u044f. \u0425\u043e\u0442\u044c \u043f\u0440\u043e\u0435\u043a\u0442 \u0438 \u043a\u0440\u043e\u0441\u0441\u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435\u043d\u043d\u044b\u0439 (\u0446\u0435\u043b\u0435\u0432\u044b\u0435 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u044b &#8212; iOS \u0438 Android), \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0434\u043b\u044f iOS, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u043c\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043c\u043d\u0435 \u043d\u0443\u0436\u043d\u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u043f\u0435\u0440\u0438\u043e\u0434 \u043e\u0442\u043b\u0430\u0434\u043a\u0438, \u0430 \u043e\u0442\u043b\u0430\u0436\u0438\u0432\u0430\u0442\u044c \u044f \u043f\u0440\u0435\u0434\u043f\u043e\u0447\u0438\u0442\u0430\u044e \u043d\u0430 iOS \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u0445. \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435,\u00a0\u0434\u043b\u044f \u0430\u043d\u0434\u0440\u043e\u0438\u0434\u0430 \u044d\u0442\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c\u043e, \u043d\u0443\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0434\u043e\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0439 \u043a\u043e\u0434. \u0418 \u0442\u0430\u043a, \u043a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 \u0434\u0432\u0443\u0445 \u0441\u043b\u043e\u0432\u0430\u0445: <strong>\u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f \u0438\u0433\u0440\u043e\u0432\u044b\u0445 \u0437\u0432\u0443\u043a\u043e\u0432 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u043d\u0430 \u0432\u043d\u0435\u0448\u043d\u0435\u043c \u0441\u0435\u0440\u0432\u0435\u0440\u0435<\/strong> (\u0438\u043c\u0438 \u0442\u0430\u043c \u043c\u043e\u0436\u043d\u043e \u043c\u0430\u043d\u0438\u043f\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c: \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c, \u0443\u0434\u0430\u043b\u044f\u0442\u044c, \u043c\u0435\u043d\u044f\u0442\u044c), \u0437\u0430\u0442\u0435\u043c \u0432 \u0441\u0430\u043c\u043e\u0439 \u0438\u0433\u0440\u0435 \u0435\u0441\u0442\u044c \u043d\u0435\u043a\u0438\u0439 \u0441\u043b\u0443\u0436\u0435\u0431\u043d\u044b\u0439 \u044d\u043a\u0440\u0430\u043d, \u0433\u0434\u0435 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0430\u0441\u0441\u0435\u0442\u043e\u0432. <img decoding=\"async\" class=\"alignnone\" src=\"http:\/\/hxml.me\/img\/post\/cocos\/1a.png\" alt=\"\" width=\"480\" \/> \u0424\u0430\u0439\u043b\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u044e\u0442\u0441\u044f \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e \u0432\u043d\u0443\u0442\u0440\u0438 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u043f\u0430\u043f\u043a\u0438 Documents. \u0422\u0430\u043a\u0436\u0435 \u044f \u043f\u0440\u0435\u0434\u0443\u0441\u043c\u043e\u0442\u0440\u0435\u043b \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u043e\u043d\u043d\u043e\u0441\u0442\u044c, \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u0443\u044e \u043d\u0430 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c\u043d\u044b\u0445 \u0441\u0443\u043c\u043c\u0430\u0445 &#8212; \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u0432\u0441\u0435 \u0444\u0430\u0439\u043b\u044b, \u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0432\u0448\u0438\u0435\u0441\u044f. \u0414\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u044f \u0441\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u043e\u0432\u0430\u043b \u0432\u0435\u0431-\u043a\u043e\u043d\u0441\u043e\u043b\u044c. <img decoding=\"async\" class=\"alignnone\" src=\"http:\/\/hxml.me\/img\/post\/cocos\/2.png\" alt=\"\" width=\"640\" \/> \u0412\u0441\u044e \u043c\u0435\u0445\u0430\u043d\u0438\u043a\u0443 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u044f \u0437\u0434\u0435\u0441\u044c \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043d\u0435 \u0441\u0442\u0430\u043d\u0443 &#8212; \u044d\u0442\u043e \u043d\u0435 \u0435\u0441\u0442\u044c \u0441\u0443\u0442\u044c \u0441\u0442\u0430\u0442\u044c\u0438. \u041f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u0442\u043e\u043c\u0443, <strong>\u043a\u0430\u043a \u044d\u0442\u0438 \u0430\u0441\u0441\u0435\u0442\u044b \u043f\u043e\u043f\u0430\u0434\u0430\u044e\u0442 \u0432 JS \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u0435<\/strong>. \u041f\u0440\u043e\u0438\u0433\u0440\u044b\u0432\u0430\u043d\u0438\u0435\u043c \u0437\u0432\u0443\u043a\u043e\u0432 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442\u0441\u044f \u043c\u043e\u0434\u0443\u043b\u044c AudioEngine, \u044f\u0432\u043b\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0447\u0430\u0441\u0442\u044c\u044e \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430 Cocos2d. \u041f\u0440\u043e\u0438\u0433\u0440\u044b\u0432\u0430\u043d\u0438\u0435 \u0430\u0443\u0434\u0438\u043e-\u0444\u0430\u0439\u043b\u0430 \u043c\u043e\u0436\u043d\u043e \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c<\/p>\n<pre lang=\"javascript\">cc.audioEngine.playEffect(soundPath, repeat);<\/pre>\n<p>\u0433\u0434\u0435 soundPath &#8212; \u043f\u0443\u0442\u044c \u043a \u0430\u0443\u0434\u0438\u043e-\u0430\u0441\u0441\u0435\u0442\u0443. \u0417\u0430\u0433\u0432\u043e\u0437\u0434\u043a\u0430 \u0431\u044b\u043b\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0432\u043e \u0432\u0441\u0435\u0445 \u043c\u0430\u043d\u0443\u0430\u043b\u0430\u0445 \u0438\u00a0\u043d\u0430 \u0444\u043e\u0440\u0443\u043c\u0430\u0445 \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u0441\u044f, \u0447\u0442\u043e \u044d\u0442\u043e \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u0443\u0442\u044c, \u0438 \u0447\u0442\u043e \u043a\u043e\u043a\u043e\u0441 \u0438\u0449\u0435\u0442 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b \u0432 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u044f\u0445, \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0445 \u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 searchPath. \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u044d\u0442\u0443 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e \u043c\u043e\u0436\u043d\u043e \u0442\u0430\u043a:<\/p>\n<pre lang=\"javascript\">jsb.fileUtils.addSearchPath(resPath);<\/pre>\n<p>\u043d\u043e, \u043a\u0430\u043a \u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c, resPath \u0442\u0430\u043a \u0436\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c, \u0442\u043e \u0435\u0441\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0439 \u043f\u0443\u0442\u044c \u0431\u0443\u0434\u0435\u0442 \u0432\u043d\u0443\u0442\u0440\u0438 \u043f\u0430\u043f\u043a\u0438, \u0433\u0434\u0435 \u043b\u0435\u0436\u0430\u0442 \u0432\u0441\u0435 \u0437\u0430\u043f\u0447\u0430\u0441\u0442\u0438 \u0431\u0438\u043b\u0434\u0430 (Applicaition Bundle). \u0421\u043f\u0430\u0441\u0435\u043d\u0438\u0435\u043c \u0441\u0442\u0430\u043b\u043e \u0442\u043e, \u0447\u0442\u043e \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 soundPath \u0444\u0443\u043d\u043a\u0446\u0438\u0438 playEffect \u043c\u043e\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c <strong>\u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u044b\u0439 \u043f\u0443\u0442\u044c<\/strong>! \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0432\u0441\u0435, \u0447\u0442\u043e \u043d\u0430\u043c \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u044d\u0442\u043e \u043f\u043e\u0434\u0441\u0443\u043d\u0443\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 playEffect \u043f\u0443\u0442\u044c \u043a \u0430\u0443\u0434\u0438\u043e\u0444\u0430\u0439\u043b\u0443, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u044e\u0431\u043e\u043c \u043c\u0435\u0441\u0442\u0435 \u0444\u0430\u0439\u043b\u043e\u0432\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b. \u041a\u0430\u043a \u0441\u043e\u043e\u0431\u0449\u0438\u0442\u044c javascript \u043a\u043e\u0434\u0443, \u0433\u0434\u0435 \u0431\u0440\u0430\u0442\u044c \u0430\u0443\u0434\u0438\u043e \u0444\u0430\u0439\u043b? \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u0435\u043b\u0438 cocos2d \u043e\u0447\u0435\u043d\u044c \u0440\u0430\u0437\u0443\u043c\u043d\u043e \u043f\u0440\u0435\u0434\u0443\u0441\u043c\u043e\u0442\u0440\u0435\u043b\u0438 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c <strong>javascript binding<\/strong> (JSB), \u0437\u0430 \u0441\u0447\u0435\u0442 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e <strong>\u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0438\u0437 javascript \u043a\u043e\u0434\u0430 \u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b Objective-C<\/strong> \u041e\u0434\u0438\u043d \u0438\u0437 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 Objective-C \u043a\u043b\u0430\u0441\u0441\u0430 \u0441 \u043d\u0430\u0431\u043e\u0440\u043e\u043c \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438 \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u0438\u0437 javascript-\u043a\u043e\u0434\u0430. \u0412\u043e\u0442 \u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre lang=\"objc\">#import \r\n\r\n#define LOAD_REMOTE_ASSETS YES\r\n\r\n@interface NativeOcClass : NSObject \r\n+(NSString *)callNativeLoaderPathForAudioAsset:(NSString *)soundId;\r\n+(NSString *)callNativeLoaderLevelSource:(NSString *)campaign :(NSString *)levelName;\r\n\r\n@end<\/pre>\n<p>\u042f \u0437\u0434\u0435\u0441\u044c \u043e\u0431\u044a\u044f\u0432\u0438\u043b \u0434\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430, \u043e \u0432\u0442\u043e\u0440\u043e\u043c \u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u0447\u0443\u0442\u044c \u043d\u0438\u0436\u0435. \u0421\u0435\u0439\u0447\u0430\u0441 \u0440\u0435\u0447\u044c \u043f\u043e\u0439\u0434\u0435\u0442 callNativeLoaderPathForAudioAsset: &#8212; \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u044b\u0439 \u043f\u0443\u0442\u044c \u043a \u0430\u0443\u0434\u0438\u043e \u0444\u0430\u0439\u043b\u0443 \u0441 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u043c soundId. \u0412\u043e\u0442 \u0435\u0433\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f:<\/p>\n<pre lang=\"objc\">+(NSString *)callNativeLoaderPathForAudioAsset:(NSString *)soundId {\r\n    if (!LOAD_REMOTE_ASSETS)\r\n        return @\"\";\r\n    NSUserDefaults * ud = [NSUserDefaults standardUserDefaults];\r\n    NSDictionary * files = [ud objectForKey: @\"remoteAssets\"];\r\n    NSString * revisionKey = [NSString stringWithFormat:@\"revision_%@\", soundId];\r\n    NSString * assetsDirPath = [NativeOcClass callNativeDocumentsResPath];\r\n    NSString * audioPath = [assetsDirPath stringByAppendingPathComponent:@\"res\/audio\"];\r\n    NSString * soundIdRevision = [NSString stringWithFormat:@\"%@_%@\", soundId, [files objectForKey:revisionKey]];\r\n    NSString * filePath = [audioPath stringByAppendingPathComponent:soundIdRevision];\r\n    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])\r\n        return filePath;\r\n    return @\"\";\r\n}<\/pre>\n<p>\u041d\u0435 \u043e\u0431\u0440\u0430\u0449\u0430\u0439\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u043b\u043e\u0433\u0438\u043a\u0443 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u0443\u044e \u0441 \u043c\u0435\u0445\u0430\u043d\u0438\u043a\u043e\u0439 \u043f\u043e\u0434\u0433\u0440\u0443\u0437\u043a\u0438 \u0444\u0430\u0439\u043b\u043e\u0432 \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430. \u0413\u043b\u0430\u0432\u043d\u0430\u044f \u0438\u0434\u0435\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u043c\u043e\u0436\u0435\u0442 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u044b\u0439 \u043f\u0443\u0442\u044c \u043a \u0430\u0443\u0434\u0438\u043e\u0444\u0430\u0439\u043b\u0443 \u0438\u043b\u0438 \u043f\u0443\u0441\u0442\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443, \u0435\u0441\u043b\u0438 \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c\u044b\u0439 \u0444\u0430\u0439\u043b \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0432 \u043f\u0430\u043f\u043a\u0435 Documents. \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 javas\u0441ript \u043a\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u0441\u0441\u0435\u0442, \u043d\u0430\u0445\u043e\u0434\u044f\u0449\u0438\u0439\u0441\u044f \u0432 \u0431\u0430\u043d\u0434\u043b\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f (Application Bundle). \u0418\u0437 javascript \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0442\u0430\u043a:<\/p>\n<pre lang=\"javascript\">soundPathForCode: function(soundId) {\r\n  var filePath = (jsb.reflection.callStaticMethod(\"NativeOcClass\", \"callNativeLoaderPathForAudioAsset:\", soundId));\r\n  if (filePath!=\"\")\r\n    return filePath;\r\n  return \"res\/audio\/\" + Sounds[soundId];\r\n},<\/pre>\n<p>\u0437\u0434\u0435\u0441\u044c NativeOcClass &#8212; \u043a\u043b\u0430\u0441\u0441, \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d \u043c\u0435\u0442\u043e\u0434 callNativeLoaderPathForAudioAsset: \u041a\u0430\u043a \u0432\u0438\u0434\u0438\u0442\u0435, \u0432\u0441\u0435 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e. \u0414\u0430\u043b\u0435\u0435, \u043a\u0430\u043a \u044f \u0443\u0436\u0435 \u0438 \u0433\u043e\u0432\u043e\u0440\u0438\u043b, \u043c\u044b \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0439 \u043f\u0443\u0442\u044c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 \u0432\u044b\u0437\u043e\u0432\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 playEffect<\/p>\n<pre lang=\"javascript\">var soundPath = this.soundPathForCode(\"player_jump\");\r\ncc.audioEngine.playEffect(soundPath, repeat);<\/pre>\n<p>\u0422\u0430\u043a\u0438\u043c \u0436\u0435 \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u044f \u0441\u0434\u0435\u043b\u0430\u043b <strong>\u0443\u0434\u0430\u043b\u0435\u043d\u043d\u0443\u044e \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 javascript \u043a\u043e\u0434\u0430<\/strong> \u0443\u0440\u043e\u0432\u043d\u0435\u0439. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432 \u043a\u043b\u0430\u0441\u0441\u0435 NativeOcClass \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d \u043c\u0435\u0442\u043e\u0434 callNativeLoaderLevelSource &#8212; \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0441\u0442\u0440\u043e\u043a\u0443 \u0441 javascript \u043a\u043e\u0434\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0442\u0430\u043a \u0436\u0435 \u043a\u0430\u043a \u0438 \u0430\u0443\u0434\u0438\u043e \u0430\u0441\u0441\u0435\u0442\u044b \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430. \u0422\u043e\u043b\u044c\u043a\u043e \u0432 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 \u0430\u0443\u0434\u0438\u043e\u0444\u0430\u0439\u043b\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u043e\u043c eval \u0434\u0432\u0438\u0436\u043a\u0430 Javascript.<\/p>\n<pre lang=\"javascript\">recreateLevelIfNeeded: function(campaign, levelNumber) {\r\n  var jsFileName = \"level\"+levelNumber;\r\n  var levelJsCode = jsb.reflection.callStaticMethod(\"NativeOcClass\", \"callNativeLoaderLevelSource::\", campaign.toLowerCase(), jsFileName);\r\n  if (levelJsCode != \"\") {\r\n    eval(levelJsCode);\r\n  }\r\n},<\/pre>\n<p><!--:--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I would like to tell about a quite complicated problem, which I have recently managed to resolve. The major sense of it can be found in the title of the post. One may as a question: what is the way this feature can be useful? It seems like no one was wondering this problem [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[142,61,112,8],"class_list":["post-1534","post","type-post","status-publish","format-standard","hentry","category-coding","tag-cocos2d","tag-ios","tag-javascript","tag-objective-c"],"_links":{"self":[{"href":"https:\/\/hxml.me\/blog\/wp-json\/wp\/v2\/posts\/1534","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hxml.me\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hxml.me\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hxml.me\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hxml.me\/blog\/wp-json\/wp\/v2\/comments?post=1534"}],"version-history":[{"count":17,"href":"https:\/\/hxml.me\/blog\/wp-json\/wp\/v2\/posts\/1534\/revisions"}],"predecessor-version":[{"id":1552,"href":"https:\/\/hxml.me\/blog\/wp-json\/wp\/v2\/posts\/1534\/revisions\/1552"}],"wp:attachment":[{"href":"https:\/\/hxml.me\/blog\/wp-json\/wp\/v2\/media?parent=1534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hxml.me\/blog\/wp-json\/wp\/v2\/categories?post=1534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hxml.me\/blog\/wp-json\/wp\/v2\/tags?post=1534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}