Skip to content

Instantly share code, notes, and snippets.

@iAladdin
Forked from jareguo/creator-debug-scripts.md
Last active June 2, 2016 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iAladdin/ae976376b6110068c7a05a284537e02b to your computer and use it in GitHub Desktop.
Save iAladdin/ae976376b6110068c7a05a284537e02b to your computer and use it in GitHub Desktop.
运行时 Creator 调试工具集

在收藏夹中新增一个网页,将网址设为下面的其中一个源码。然后打开任一 Creator 页面,接着直接在收藏夹点击刚刚收藏的网址即可。

打印 Hierarchy

javascript:(function () {
    function visitNode (node, visitor) {
        visitor(node);
        var children = node._children;
        for (var i = 0; i < children.length; ++i) {
            visitNode(children[i], visitor);
        }
    }
    cc.Node.prototype.printPath = function () {
        var path = this.name;
        for (var parent = this.parent; parent instanceof cc.Node; parent = parent.parent) {
            path = parent.name + '/' + path;
        }
        console.log(path);
    };
    visitNode(cc.Canvas.instance.node, function (node) {
        node.printPath();
    });
})();

载入调试用的扩展方法

javascript:cc.Node.prototype.printPath = function () {
    var path = this.name;
    for (var parent = this.parent; parent instanceof cc.Node; parent = parent.parent) {
        path = parent.name + '/' + path;
    }
    console.log(path);
};

打印 Hierarchy 附带 uuid 及获取node实例的方法

javascript:(function () {
    function visitNode (node, visitor) {
        visitor(node);
        var children = node._children;
        for (var i = 0; i < children.length; ++i) {
            visitNode(children[i], visitor);
        }
    }
    cc.Node.prototype.printPath = function (name) {
        var path = this.name + "(" + this.uuid + ")";
        for (var parent = this.parent; parent instanceof cc.Node; parent = parent.parent) {
            path = parent.name + '/' + path;
        }
        if (name == null || name != null && name.length>0 && path.indexOf(name)>-1){
            console.log(path);
        }
    };
    cc.Node.prototype.getInstance = function (uuid) {
        var obj = null;
        if (this.uuid == uuid){
            obj = this    
        }
        return obj;
    };
    cc.Node.prototype.printPathByName = function(name){
        visitNode(cc.Canvas.instance.node, function (node) {
            if (node.name == name){
                node.printPath(name);    
            }
        });
    }
    cc.Node.prototype.printPathFromRootName = function(name){
        visitNode(cc.Canvas.instance.node, function (node) {
            node.printPath(name);    
        });
    }
    cc.Node.prototype.getInstanceByUUID = function(uuid){
        var target = null;
        visitNode(cc.Canvas.instance.node, function (node) {
            var obj = node.getInstance(uuid);    
            if (obj != null){
                target = obj;
            }
        });
        return target;
    }
})();

根据uuid获取某一node实例

//在console中输入上面的代码,加载方法。

//1. 获取UUID
cc.Canvas.instance.node.printPathFromRootName("RootNodeName");

//2.从输出中寻找到你希望获取的uuid
var targetNode = cc.Canvas.instance.node.getInstanceByUUID("YourNodeUUID");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment