Cocos Creator 2.1.2 遇到几个Prefab的bug和 js命名和 es6转es5的bug

最近遇到了几个经常出现的bug。

第一,Prefab重命名,经常无效,编辑器里显示名字是更改之后的了,可是Finder里还是老的名字,这时我只能重新编辑器,一天要重启好多次。

第二,报错,某一个重要的Prefab打不开了:

Error: [AssetLibrary] loading JSON or dependencies failed: undefined

试了多种方法,还是不行,目前已经正在尝试删了老的,重新设计这个Prefab了!!(头很大。。。)

第三,有个小问题,多人通过git管理的时候,拉下来的资源会在编辑器中抱一堆错误,需要重启编辑器,之前的老版本不会,比如1.10.2版本正常,会自动刷新。算个小问题,提及一下。


又遇到一个问题,如下:
第四,在vs code中新建了一个js脚本,切换回编辑器中,资源管理器中就是不刷新这个脚本文件,重启编辑器,这次刷新了,可以看见这个js脚本了。但是还是编辑器控制台中依然报错:

load script [./poems/ResPrefabBodyGroup] failed : Error: Cannot find module ‘./poems/ResPrefabBodyGroup’
at Module._resolveFilename (module.js:470:15)
at Function.Module._resolveFilename [as _resolveFilenameVendor] (/Applications/CocosCreator.app/Contents/Resources/electron.asar/common/reset-search-paths.js:35:12)
at Function.s._resolveFilename (/Applications/CocosCreator.app/Contents/Resources/app.asar/editor/page/project-scripts.js:1:941)
at Function.Module._load (module.js:418:25)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.cc.require (/Applications/CocosCreator.app/Contents/Resources/app.asar/editor/page/project-scripts.js:1:589)
at /…/temp/quick-scripts/assets/Script/…/dataRes/ResourceManager.js:1:372

ps: 第四条解决方法分享一下,先把文件备份,然后在编辑器中删除那个文件,在编辑器中重新新建那个文件就好了。可能是因为在vs code中创建了那个文件后,出现了bug。

不过要大大赞一下,现在 es6 转 es5后,智能错误提示精确了不是一星半点,不需要运行时才能发现bug,这个太赞了!!解决了无数的低级失误。

唯一一点小小的遗憾是:提示的错误在es5代码中,并不是es6的代码位置,所有有时候不太确定的时候,需要去quick-scripts包中找到对应的代码仔细查看一番。

不过又发现一个es6转成es5后的bug:

下面es6的代码:

class A {
    constructor() {
      this.x = 1;
    }
  }
  
  class B extends A {
    constructor() {
      super();
      this.x = 2;
      super.x = 3;
      console.log(this.x,super.x); // 3
    }
  }
  
  let b = new B();


可以看出在浏览器中直接执行结果为:3 undefined

以上同样的代码,通过Creator运行之后结果确实2 undefined,

所以我去quick文件夹中特定查看了转成es5以后的代码如下,并执行了结果也如下:

<script>        

        "use strict";

        var _get = function get(object, property, receiver) {
            if (object === null) object = Function.prototype;
            var desc = Object.getOwnPropertyDescriptor(object, property);
            if (desc === undefined) {
                var parent = Object.getPrototypeOf(object);
                if (parent === null) {
                    return undefined;
                } else {
                    return get(parent, property, receiver);
                }
            } else if ("value" in desc) {
                return desc.value;
            } else {
                var getter = desc.get;
                if (getter === undefined) {
                    return undefined;
                }
                return getter.call(receiver);
            }
        };

        var _set = function set(object, property, value, receiver) {
            var desc = Object.getOwnPropertyDescriptor(object, property);
            console.log("desc:",desc);
            if (desc === undefined) {
                var parent = Object.getPrototypeOf(object);
                console.log("parent:",parent);
                if (parent !== null) {
                    set(parent, property, value, receiver);
                }
            } else if ("value" in desc && desc.writable) {
                desc.value = value;
            } else {
                var setter = desc.set;
                if (setter !== undefined) {
                    setter.call(receiver, value);
                }
            }
            return value;
        };

        var _createClass = function() {
            function defineProperties(target, props) {
                for (var i = 0; i < props.length; i++) {
                    var descriptor = props[i];
                    descriptor.enumerable = descriptor.enumerable || false;
                    descriptor.configurable = true;
                    if ("value" in descriptor) descriptor.writable = true;
                    Object.defineProperty(target, descriptor.key, descriptor);
                }
            }
            return function(Constructor, protoProps, staticProps) {
                if (protoProps) defineProperties(Constructor.prototype, protoProps);
                if (staticProps) defineProperties(Constructor, staticProps);
                return Constructor;
            };
        } ();

        var _class;

        function _classCallCheck(instance, Constructor) {
            if (! (instance instanceof Constructor)) {
                throw new TypeError("Cannot call a class as a function");
            }
        }

        function _possibleConstructorReturn(self, call) {
            if (!self) {
                throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
            }
            return call && (typeof call === "object" || typeof call === "function") ? call: self;
        }

        function _inherits(subClass, superClass) {
            if (typeof superClass !== "function" && superClass !== null) {
                throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
            }
            subClass.prototype = Object.create(superClass && superClass.prototype, {
                constructor: {
                    value: subClass,
                    enumerable: false,
                    writable: true,
                    configurable: true
                }
            });
            if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
        }


        var A = function A() {
            _classCallCheck(this, A);

            this.x = 1;
        };

        var B = function(_A) {
            _inherits(B, _A);

            function B() {
                _classCallCheck(this, B);

                var _this2 = _possibleConstructorReturn(this, (B.__proto__ || Object.getPrototypeOf(B)).call(this));

                _this2.x = 2;
                _set(B.prototype.__proto__ || Object.getPrototypeOf(B.prototype), "x", 3, _this2);
                console.log(_this2.x, _get(B.prototype.__proto__ || Object.getPrototypeOf(B.prototype), "x", _this2));
                return _this2;
            }

            return B;
        } (A);

        var b = new B();
  console.log("b",b);


</script>

可以看出结果确实为:2 undefined
可以看出同样的代码,执行的结果却不同。。。
我通过代码打断点查看了这个es5之后的代码super.x = 3语句,在es5中执行的结果就等于什么都没做,有兴趣的同学可以自己试试。
(我不知道ccc中是如何把es6转es5的,如有大神知道,烦请告知。。。谢谢)

总结:在ccc中使用es6编写代码时,尽量避免使用super.属性的方式取值赋值,一般不会出现这种意想不到的bug。

…没仔细看 不应该出现 es6 到es5的bug啊。。makr下

在ts中,这样写会报错的,应该是语法上不支持,只是js没有报错提示而已

ts 中会报错吗?报什么错?

我觉得ts中应该也可以这么写把,不过我还没试验,不知道结果是多少

你使用的是具体哪个rc版本或者beta版本?

这个概率应该是多大?

同理,这个prefab如果是一个简单的节点是不是也会这样?
概率多大?

这个问题,如果只在单人开发进行版本控制时会不会也这样?

这个报错好像不是引擎引起的,贴出你的代码看看。

版本是v2.1.2-rc.6 ,Mac版

概率很大,我这边新开的项目,也就10个左右的Prefab,预估一下概率大概是30%左右。

这个我不知道怎么出现的,这个概率不高,但是一旦出现,无法还原。只能恢复git上老的版本。

如果一个电脑上开发,肯定没有问题,但是多台电脑的话,比如家里开发,公司电脑也能开发,即是单人,也一样出现。

这个bug我已经修复了,在Creator中先备份,然后删除,在Creator再新建就可以了。这个bug出现我怀疑是在vs code中新建的js文件,在某种情况下没有被识别到,因为我看了quick文件夹没有这个对应对应的es5版本对应的js文件。

你看我今晚就新建了2个新的Prefab,其中一个是从另一个Prefab拷贝过来的,在编辑器重命名了,就这个又出现了这个bug。

如图:

(看下图,Creator中显示AuthorType0存在,Finder中显示的WordType0,这是bug吧,很容易重现)

你搞错了吧,没这种语法…… super 只能用来调用函数。因为 super 和 this 是同一个对象,并不存在 this 升级为父类对象这种操作,又不是指针。

谢谢这么晚了还回复。

在函数内部,super 和 this 是同一个对象。但是,super调用属性的语法应该没问题,es6中是支持的。

我上面的帖子中,
我在浏览器控制台直接编写的代码,先调用this.x =2,再调用super.x =3 执行打印输出的this.x结果是3。

但是在ccc环境编写同样的es6代码,依然是先调用this.x =2,再调用super.x =3 ,当我运行启动浏览器后,执行打印输出的this.x结果是2。

结果不一样。

都说不可以了,自己看吧 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/super

这种标准 JS 未定义的操作,理论上可以是任何结果,因为是未定义行为,无标准可言。

猜测一下,它这里的 super.prop 会不会是属性?

不清楚,这个依赖于平台实现

嗯,我以后的项目中也不会再出现那种调用方式。