loading.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. var Loading = cc.Scene.extend({
  2. _interval: null,
  3. _label: null,
  4. _className: "Loading",
  5. init: function () {
  6. var self = this;
  7. //loading percent
  8. var label = self._label = cc.LabelTTF.create(Localize[lang]['loading'] + '... 0%', 'Arial', 40);
  9. label.setPosition(winSize.width / 2, winSize.height / 2);
  10. label.setColor(cc.color(180, 180, 180));
  11. self.addChild(label);
  12. var logo = ccui.ImageView.create();
  13. logo.loadTexture(res.logo_ingame);
  14. logo.attr({
  15. x: winSize.width / 2,
  16. y: 55,
  17. touchEnabled: true
  18. });
  19. logo.addTouchEventListener(function (sender, type) {
  20. if (type == ccui.Widget.TOUCH_ENDED) {
  21. window.location.href = 'http://m.softgames.de';
  22. }
  23. }, self);
  24. self.addChild(logo);
  25. return true;
  26. },
  27. /**
  28. * custom onEnter
  29. */
  30. onEnter: function () {
  31. var self = this;
  32. cc.Node.prototype.onEnter.call(self);
  33. self.schedule(self._startLoading, 0.3);
  34. },
  35. /**
  36. * init with resources
  37. * @param {Array} resources
  38. * @param {Function|String} cb
  39. */
  40. initWithResources: function (resources, cb) {
  41. if (cc.isString(resources))
  42. resources = [resources];
  43. this.resources = resources || [];
  44. this.cb = cb;
  45. },
  46. _startLoading: function () {
  47. var self = this;
  48. self.unschedule(self._startLoading);
  49. var res = self.resources;
  50. cc.loader.load(res,
  51. function (result, count, loadedCount) {
  52. var percent = (loadedCount / count * 100) | 0;
  53. percent = Math.min(percent, 100);
  54. self._label.setString(Localize[lang]['loading'] + '... ' + percent + '%');
  55. }, function () {
  56. if (self.cb)
  57. self.cb();
  58. });
  59. }
  60. });
  61. Loading.preload = function (resources, cb) {
  62. var _cc = cc;
  63. if (!_cc.loaderScene) {
  64. _cc.loaderScene = new Loading();
  65. _cc.loaderScene.init();
  66. }
  67. _cc.loaderScene.initWithResources(resources, cb);
  68. cc.director.runScene(_cc.loaderScene);
  69. return _cc.loaderScene;
  70. };