ããå¨ä½¿ç¨ C++ ç¼å Cocos2d-x 游æçæ¶åï¼é常å¨è¿è¡æ¸¸æä¹åï¼éè¦å 载游æèµæºï¼è¿æ ·æ¯ä¸ºäºè®©æ¸¸æå¨è¿è¡æ¶æ´ä¸ºæµç
ï¼é¿å
äºå¨è¿è¡æ¶å è½½èµæºï¼èåºç°å¡é¡¿ç°è±¡ï¼å½±åç¨æ·ä½éªï¼å 为å è½½èµæºæ¯é常èæ¶ãèèµæºçæä½ãå¨ Cocos2d-html5 ä¸ä¹æ¯åæ ·ï¼å¨è¿è¡æ¸¸æä¹åï¼é¢å
å 载好ææçèµæºï¼å è½½å°æµè§å¨ç¼åï¼ï¼ä»¥ä¿è¯æ¸¸æçæµç
è¿è¡ã
ããCocos2d-html5 çå è½½æµç¨
ããå¨å¼å§æ们çæ¿æ¢å·¥ä½ä¹åï¼å¤§è´è¯´ä¸ä¸å¿
è¦çï¼åªæ³¨éæ们å¨ä¹çç»èé®é¢ï¼åæçå è½½æµç¨ï¼ä»¥**HelloHTML5World**ä¸ºä¾ ãä»å
¶ä¸»é¡µé¢ index.htmlå¼å§ï¼æ们éè¦äºè§£ä¸ä¸ªæ件çè¿ä½æ¹å¼ï¼index.htmlãcocos2d.js å main.jsï¼
ããæµè§å¨é¦å
å è½½ index.html 页é¢ï¼å¼å¾æ³¨æçæ两ç¹ï¼å¨é¡µé¢ç DOM æ ä¸ï¼è½çå°å½å为**gamecanvas**ç**Canvas**å
ç´ ï¼å®å°ä¼æ¯æ¸¸æçç»å¸ï¼å¦ä¸ç¹ï¼å¨é¡µé¢çæåï¼å è½½äº cocos2d.js æ件ã
ããcocos2d.js å
ä¸ï¼å®ä¹äºç¨åºè¿è¡éè¦çä¸äºåæ°ï¼å¦ æ¾ç¤º FPSï¼æ¯å¦å è½½æ©å±åºï¼ç©çå¼æåºçï¼å
¶ä¸ engineDir 设置äºå¼ææå¨çä½ç½®ï¼appFiles 设置äºï¼å½å项ç®æç¨å°éè¦å è½½ç js ç¨åºä»£ç ã并å®ä¹äºå½ DOM å è½½å®ææ¶è¿è¡ç代ç (ä½ å¯ä»¥å¨ ãè¿éã æ¥çææ代ç ã)ï¼
ããwindow.addEventListener('DOMContentLoaded', function () {
ãã// æ·»å èæ¬
ããvar s = d.createElement('script');
ãã// è¿éå¤æäºæ¯å¦ä½¿ç¨èªå®ä¹ç åæ件ä½ä¸ºåºå è½½ï¼å¯¹åºçä¼åå缩æ件
ããif (c.SingleEngineFile && !c.engineDir) {
ããs.src = c.SingleEngineFile;
ãã}
ããelse if (c.engineDir && !c.SingleEngineFile) {
ããs.src = c.engineDir + 'platform/jsloader.js';
ãã}
ããelse {
ããalert('You must specify either the single engine file OR the engine directory in "cocos2d.js"');
ãã}
ããdocument.ccConfig = c;
ããs.id = 'cocos2d-html5';
ãã// å°èæ¬å è½½å°å½åææ¡£ï¼å°åæ¯ jsloader.js çå®é
å°å
ããd.body.appendChild(s);
ãã});
ããjsloader.js éé¢è®¾ç½®äºä¸å éè¦å è½½çå¯æ§è¡èæ¬ï¼ä¿åå¨ engine åéä¹ä¸ï¼å¨æ件çæåï¼æ们è½å¤çå°è¿æ ·ç代ç ï¼
ãã// å°ææç appFiles æ·»å å° engine ä¸ï¼è¿åå°æ°å®ä¹çåé que ä¹ä¸
ããvar que = engine.concat(c.appFiles);
ããque.push('main.js');
ãã...
ãã// åé¢ä¸ä¸ª for 循ç¯ï¼æ·»å æææä»¶å° document ä¸å»
ããç±ä»¥ä¸ä»£ç ï¼ä¾¿å°æ们èªå®ä¹ç使ç¨èæ¬å main.js æ·»å è¿å»äºï¼èæåæ§è¡çä¹æ¯ main.js èæ¬ï¼æ¸¸æç第ä¸ä¸ªè¿è¡åºæ¯å°±ç±æ¤å¼å§ãmain.js éé¢å建äºä¸ä¸ª cocos2dApp çç±»åï¼å® 继æ¿(è½ç¶å¨ js ä¸æ²¡æ继æ¿çæ¦å¿µï¼ä½æ类似äºç»§æ¿çæºå¶) èª cc.Applicatoinï¼å
¶ä¸æ们çå°é常çæçå½æ° applicationDidFinishLaunching ï¼æè¿æ ·ä¸æ®µä»£ç ï¼
ãã// è°ç¨ cc.LoaderScene Loading çé¢ï¼ç¨ä»¥å è½½èµæº
ããcc.LoaderScene.preload(g_resources, function () {
ãã// å½èµæºå è½½å®æ¯ï¼åè°å½æ°ï¼è¿è¡ç¬¬ä¸ä¸ªåºæ¯ï¼èè¿ä¸ªåºæ¯æ¯ç± cocos2dApp çæé å½æ°ä¼ å
¥
ããdirector.runScene(new this.startScene());
ãã}, this);
ãã// main.js æåä¸è¡
ããvar myApp = new cocos2dApp(GameLayer.scene);
ããå¨ cocos2dApp çæé å½æ°ä¸ï¼åå§åäºä¸äºå¿
è¦ä¿¡æ¯ï¼å¹¶è°ç¨äºcc.AppController.shareAppController().didFinishLaunchingWithOptions();ï¼è¿ä¼é´æ¥çè°ç¨ Application ç run æ¹æ³ï¼ä»è导è´applicationDidFinishLaunchingæ¹æ³è¢«è§¦åãå®è¿è¡äºcc.LoaderSceneç preLoad æ¹æ³ï¼è¿å
ä¸å°±æ¯ Loading çé¢çå®ç°äºï¼å®ä¼ å
¥äºä¸ä¸ªåè°å½æ°ï¼ç¨äºç¡®å®å¨èµæºå è½½å®æ¯ä¹åå¯å¨ç¬¬ä¸ä¸ªåºæ¯ï¼Loading å
¶æ¬èº«ä¹æ¯ä¸ä¸ªåºæ¯ï¼ã
ããææ ·èªå®ä¹ Loading çé¢
ããåé¢æ们äºè§£äº Cocos2d-html5 ç大è´å è½½æµç¨ï¼èç°å¨æ们å
³æ³¨çæ¯ cc.LoaderScene æå¨çæ件 CCLoader.js çå
é¨å®ç°ãéé¢å®ä¹äº cc.Loader å cc.LoaderScene ç±»åï¼Loader å
é¨å®æäºï¼å¯¹èµæºå è½½çæææä½æ¥éª¤å®ç°ï¼è LoaderScene åæ¯å¯¹ Loader çè¿ä¸æ¥å°è£
ï¼å°å è½½çè¿ç¨ï¼ç¨ä¸ä¸ªçé¢æ¥å¯è§åçå®ç°åºï¼å¦ç¨ä¸ä¸ªå è½½åºæ¯ï¼ä¸é¢ä¸ä¸ª Logo æ¾ç¤ºï¼åæ¶æ¾ç¤ºäºå½åå è½½èµæºçè¿åº¦ç¾åæ¯ãæ们è¦èªå®ä¹å®ç° Loading çé¢ï¼é£å°±æ¯éæ°å®ç° LoaderScene å³å¯ãå¯¹äº LoaderScene çå®ç°æ¯è¾ç®åï¼æ们åèå
¶å®ç°ï¼èªå®ä¹ä¸ä¸ª Loader.js æ件ï¼å®ç° Loader ç±»ï¼å®æèªå®ä¹Loading çé¢çå
·ä½å®ç°ï¼å
¶ä¸å¤§å¤åèï¼å®é
æ¯copyï¼äº LoaderScene çå®ç°ï¼å¨å
¶ä¸ä¿®æ¹æ©å
ï¼å®å®æäºä¿®æ¹ Logo å¾çï¼å¹¶æ·»å äºä¸ä¸ªç®åçè¿åº¦æ¡ï¼æ¯å è½½è¿ç¨æ´ä¸ºä¸ç®äºç¶ï¼è¿é并没æå¤ä¹ç«çææï¼ä½è¶³ä»¥è®©ä½ äºè§£ï¼ä½ 该å¦ä½èªå®ä¹ä¸ä¸ª Loading çé¢ï¼åªè´´åºäºç¸æ¯è¾ LoaderScene æä¿®æ¹çé¨åï¼ä½ä½ å¯ä»¥å¨ ãè¿éã è·åå°æºç ï¼ï¼
ãã// è¿éå®ä¹äº Logo å¾çç Base64 ç¼ç ï¼è³äºä¸ºä»ä¹ï¼åé¢å°ä¼è¯´æï¼è¿éçç¼ç å
容æºå¤ï¼æ
åç®å
ããlogoData = "data:image/png;base64,...";
ããLoading = cc.Scene.extend(
ãã_logo: null,
ãã_logoTexture: null,
ãã_texture2d: null,
ãã_bgLayer: null,
ãã_label: null,
ãã_winSize:null,
ãã_processLayer: null, // ç¸æ¯ LoaderScene çå®ç°ï¼æ·»å äºä¸¤ä¸ªå±æ§ï¼æ 示è¿åº¦æ¡å±åè¿åº¦æ¡é¿åº¦
ãã_processLayerLength: null,
ãã// æé å½æ°
ããctor: function () {
ããthis._super();
ããthis._winSize = cc.Director.getInstance().getWinSize();
ãã},
ããinit:function(){
ããcc.Scene.prototype.init.call(this);
ãã// logo å¾çå label çæ·»å .. è¿éçç¥ï¼äº LoaderScene åæ ·
ãã// 设置è¿åº¦æ¡å±ï¼å®å°±æ¯ä¸ä¸ªçº¢è²é¢è²å±ï¼éè¿é¿åº¦æ¥æ 示å è½½çè¿åº¦
ããthis._processLayerLength = 500;
ããthis._processLayer = cc.LayerColor.create(cc.c4b(255, 100, 100, 128), 1, 30);
ããthis._processLayer.setPosition(cc.pAdd(centerPos, cc.p(- this._processLayerLength / 2, -logoHeight / 2 - 50)));
ãã// å¯ä»¥å¯ç¨éç¹ï¼å¹¶è®¾ç½®ä»¥æ»¡è¶³èªå·±çéè¦
ãã// this._processLayer.ignoreAnchorPointForPosition(false);
ãã// this._processLayer.setAnchorPoint(cc.p(0, 0));
ããthis._bgLayer.addChild(this._processLayer);
ãã},
ãã// 以ä¸æ¹æ³çå®ç°å¹¶æ²¡æè· LoaderScene æä»ä¹ä¸å
ãã// _initStage: ...
ãã// onEnter ...
ãã// onExit ...
ãã// initWithResources ...
ãã// _startLoading ...
ãã// _logoFadeIn
ãã// æ¯å¸§æ´æ°
ãã_updatePercent: function () {
ããvar percent = cc.Loader.getInstance().getPercentage();
ããvar tmpStr = "Loading... " + percent + "%";
ããthis._label.setString(tmpStr);
ãã// 设置å½åè¿åº¦æ¡å±çé¿åº¦
ããthis._processLayer.changeWidth(this._processLayerLength * percent / 100);
ããif (percent >= 100)
ããthis.unschedule(this._updatePercent);
ãã}
ãã});
ãã// è¿éäº LoaderScene çå®ç°åæ ·
ããLoading.preload = function (resources, selector, target) {
ããif (!this._instance) {
ãã// å建ä¸ä¸ª Loading
ããthis._instance = new Loading();
ããthis._instance.init();
ãã}
ãã// ...
ããreturn this._instance;
ãã};
ããè¿éæ们åªæ¯ copy äºä¸ä»½ LoaderSceneï¼copy ä¿®æ¹ä¼è®©è¿éçæä½æ¥éª¤ç®åï¼ ï¼éæ°å½å Loading ç¶åå¨æ¤åºç¡æ·»å äºä¸ä¸ªè¿åº¦æ¡æ¾ç¤ºï¼å½ç¶è¿éç Loading ç±»å®å
¨ç±ä½ èªå·±å®ä¹ï¼å®æåªäºæ¾ç¤ºï¼ä½ å¯ä»¥éæå®å¶ï¼åªè¦å¨ _updatePercent æ¹æ³å®æ¶è·åå½åè¿åº¦ï¼å¹¶ä¸æ´æ°å°çé¢æ¾ç¤ºå³å¯ã
ããæ们注æå°å¨ Loading æ件ï¼å®ä¹äºä¸ä¸ª logoData åéï¼å®ä¿åçæ¯ä¸å¼ å¾çç Base64 ä½æ ¼å¼ç¼ç ï¼è¿æ ·åç好å¤æ¯ï¼å¨è¿è¡æ¾ç¤ºå¾çä¹æ¶ï¼ä¸ä¼åºç°æ¤å¾æ²¡æå è½½çæ
åµï¼ä½æ¯ä¹ç±äºå®æ¯ Base64 ç¼ç çå¾çï¼æ以å¾çä¸å®è¿å¤§ï¼å¦åç¼ç åçæ°æ®éå¾å¤§ï¼å
¶æ¬¡è§£ç ä¹éè¦èæ¶ã
ããå®æ Loading.js åï¼æ们éè¦å è½½å®ï¼å¹¶è®©å®è¿è¡ï¼ä»¥æ¿æ¢ LoaderScene çè¿è¡ãé¦å
ä¿®æ¹é¡¹ç® cocos2d.js æ件ï¼å¨**appFiles**æ·»å Loading.js æ件ãå
¶æ¬¡ä¿®æ¹ main.js æ件å
cc.LoaderScene.preload 为 Loading.preload å³å¯ï¼å·æ° index.html çé¢ï¼å°è½çè§ Loading çé¢å·²ç»æ¿æ¢ãå®å è½½é度çå¿«æ
¢åå³äºæ¸¸æèµæºå
容çå¤å° ~
温馨提示:答案为网友推荐,仅供参考