cocos2d-js:定制加载界面

前端之家收集整理的这篇文章主要介绍了cocos2d-js:定制加载界面前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
http://blog.csdn.net/jonahzheng/article/details/38348255

cocos2d进入游戏前都有一个默认的加载界面,如图,如果要自己定制这个加载界面,应该如何做呢


以下是main.js的代码

[javascript] view plain copy print ?
  1. cc.game.onStart=function(){
  2. //1,
  3. //cc.view.adjustViewPort(true);
  4. //2.根据不同的环境设置不同的分辨率
  5. if(cc.sys.isMobile)
  6. cc.view.setDesignResolutionSize(320,500,cc.ResolutionPolicy.FIXED_WIDTH);
  7. elsecc.view.setDesignResolutionSize(320,480,cc.ResolutionPolicy.SHOW_ALL)
  8. //cc.view.setDesignResolutionSize(800,450,cc.ResolutionPolicy.EXACT_FIT);
  9. cc.view.resizeWithBrowserSize(true);
  10. //3.预加载
  11. cc.LoaderScene.preload(g_resources,function(){
  12. //4.加载第一场景
  13. cc.director.runScene(newGameScene());
  14. },this);
  15. };
  16. cc.game.run();

其中cc.LoaderScene就是Loading界面的实现类,对应的文件就是CCLoaderScene.js,我们只需参考这个文件自定义一个LoaderScene类就可以了。

类名叫MyLoading,代码如下

    varlogoData="data:image/jpeg;base64,/9.............";//图片的base64数据
  1. MyLoading=cc.Scene.extend({
  2. _interval:null,
  3. _length:0,
  4. _count:0,
  5. _label:null,
  6. _winSize:null,153); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> _className:"MyLoading",248)"> _processLayer:null,153); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> _processLayerLength:null,153); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> init:function(){
  7. varself=this;
  8. //logo
  9. varlogoWidth=160;
  10. varlogoHeight=200;
  11. //bg
  12. varbgLayer=self._bgLayer=cc.LayerColor.create(cc.color(32,32,255));
  13. bgLayer.setPosition(cc.visibleRect.bottomLeft);
  14. self.addChild(bgLayer,0);
  15. //imagemovetoCCSceneFile.js
  16. varfontSize=24,lblHeight=-logoHeight/2+100;
  17. if(cc._loaderImage){
  18. //loadinglogo
  19. cc.loader.loadImg(logoData,{isCrossOrigin:false},function(err,img){
  20. logoWidth=img.width;
  21. logoHeight=img.height;
  22. self._initStage(img,cc.visibleRect.center);
  23. });
  24. fontSize=14;
  25. lblHeight=-logoHeight/2-10;
  26. //}
  27. //loadingpercent
  28. varlabel=self._label=cc.LabelTTF.create("Loading...0%","Arial",fontSize);
  29. label.setPosition(cc.pAdd(cc.visibleRect.center,cc.p(0,lblHeight)));
  30. label.setColor(cc.color(180,180,180));
  31. bgLayer.addChild(this._label,10);
  32. //定义进度条层
  33. this._winSize=cc.director.getWinSize();
  34. varcenterPos=cc.p(this._winSize.width/2,this._winSize.height/2);
  35. self._processLayerLength=500;
  36. self._processLayer=cc.LayerColor.create(cc.color(255,100,128),1,30);
  37. self._processLayer.setPosition(cc.pAdd(centerPos,cc.p(-this._processLayerLength/2,-logoHeight/2-50)));
  38. self._bgLayer.addChild(this._processLayer);
  39. returntrue;
  40. _initStage:function(img,centerPos){
  41. varself=this;
  42. vartexture2d=self._texture2d=newcc.Texture2D();
  43. texture2d.initWithElement(img);
  44. texture2d.handleLoadedTexture();
  45. varlogo=self._logo=cc.Sprite.create(texture2d);
  46. logo.setScale(cc.contentScaleFactor());
  47. logo.x=centerPos.x;
  48. logo.y=centerPos.y;
  49. self._bgLayer.addChild(logo,10);
  50. onEnter:function(){
  51. cc.Node.prototype.onEnter.call(self);
  52. self.schedule(self._startLoading,0.3);
  53. onExit:function(){
  54. cc.Node.prototype.onExit.call(this);
  55. vartmpStr="Loading...0%";
  56. this._label.setString(tmpStr);
  57. /**
  58. *initwithresources
  59. *@param{Array}resources
  60. *@param{Function|String}cb
  61. */
  62. initWithResources:function(resources,cb){
  63. if(typeofresources=="string")resources=[resources];
  64. this.resources=resources||[];
  65. this.cb=cb;
  66. },153); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> _startLoading:function(){
  67. self.unschedule(self._startLoading);
  68. varres=self.resources;
  69. self._length=res.length;
  70. self._count=0;
  71. cc.loader.load(res,function(result,count){self._count=count;},function(){
  72. if(self.cb)
  73. self.cb();
  74. });
  75. self.schedule(self._updatePercent);
  76. _updatePercent:function(){
  77. varcount=self._count;
  78. varlength=self._length;
  79. varpercent=(count/length*100)|0;
  80. percent=Math.min(percent,100);
  81. self._label.setString("Loading..."+percent+"%");
  82. //更新进度条的长度
  83. this._processLayer.changeWidth(this._processLayerLength*percent/100);
  84. if(count>=length)self.unschedule(self._updatePercent);
  85. }
  86. MyLoading.preload=function(resources,248)"> var_cc=cc;
  87. if(!_cc.myLoading){
  88. _cc.myLoading=newMyLoading();
  89. _cc.myLoading.init();
  90. _cc.myLoading.initWithResources(resources,cb);
  91. cc.director.runScene(_cc.myLoading);
  92. return_cc.myLoading;
  93. };
原文链接:https://www.f2er.com/cocos2dx/342844.html

猜你在找的Cocos2d-x相关文章