coocs creator模块化

cocos creator模块化的几种方式:

方式一

Rotate.js

    var Rotate = cc.Class({
    extends: cc.Component,
    properties: {
        speed: 1
    },
    update: function () {
        this.transform.rotation += this.speed;
    }
});

在helloworld.js中进行引用,这里使用require引入脚本后,因为Rotate.js中没有使用module.exports暴露出一个对象出来,所以我们在helloworld.js中进行引用的时候,需要生成对象:

let Rotate = require('Rotate');

cc.Class({
    extends: cc.Component,
    properties: {
        label: {
            default: null,
            type: cc.Label
        },
        // defaults, set visually when attaching this script to the Canvas
        text: 'Hello, World!',

        guagua: {
            default: null,
            type: cc.Node,
        }
    },

    // use this for initialization
    onLoad: function () {
        this.label.string = this.text
        //方法一:模块化require只是引入了这个类,如果调用类内部的成员变量和方法,那么需要实例化一个对象才行
        let rotate = new Rotate()
        cc.info('speed:' + rotate.speed)
    },
    start() {

    },
    // called every frame
    update: function (dt) {

    },

});

方式二:

Person.js:

var Person = {
    name: 'eric',
    properties: {
        age: 20,
    },

    sayHello: function () {

        cc.info('say hello...')

    }

}
Person.sayHello()
module.exports = Person

在 helloworld.js中可以直接引用persion.js中使用module.exports = Person,进行暴露出来的对象


let p = require('Person')

cc.Class({
    extends: cc.Component,

    properties: {
        label: {
            default: null,
            type: cc.Label
        },
        // defaults, set visually when attaching this script to the Canvas
        text: 'Hello, World!',

        guagua: {
            default: null,
            type: cc.Node,
        }
    },

    // use this for initialization
    onLoad: function () {

        //方法二:在'组件'person中通过moudle.exports的方式暴露对象出来
        p.sayHello()
    },

    start() {

    },

    // called every frame
    update: function (dt) {

    },

});

方法三:

可以直接将“用户自定义组件”添加到相应的“Node”上面,然后就可以在这个node上面通过node.getComponent()的方式来进行引用
guagua.js

cc.Class({
    extends: cc.Component,
    move: function () {
        // create a action by cocos action system
        var action = cc.moveTo(2, 100, 100)
        this.node.runAction(action)
    },
    start() {
    },

    onLoad: function () {

    },
});

将guagua.js挂载到node节点上面:

node.jpg

helloworld.js中进行引用:


cc.Class({
    extends: cc.Component,

    properties: {
        label: {
            default: null,
            type: cc.Label
        },
        // defaults, set visually when attaching this script to the Canvas
        text: 'Hello, World!',

        guagua: {
            default: null,
            type: cc.Node,
        }
    },

    // use this for initialization
    onLoad: function () {
        this.label.string = this.text
        //如果需要操作'脚本组件',需要先获取'node',然后获取到挂载到这个node上面的'脚本组件',然后才能进行运行
        this.guagua.getComponent('guagua').move()

    },

    start() {

    },

    // called every frame
    update: function (dt) {

    },


});

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 模块 Node 有简单的模块加载系统。在 Node 里,文件和模块是一一对应的。下面例子里,foo.js加载同一个...
    保川阅读 679评论 0 0
  • Node.js是目前非常火热的技术,但是它的诞生经历却很奇特。 众所周知,在Netscape设计出JavaScri...
    w_zhuan阅读 3,716评论 2 41
  • 前言 js是从网页小脚本演变过来的,至今,前端的js库,也不像一个真正的模块。前端js经历了工具类库、组件库、前端...
    白昔月阅读 3,427评论 2 11
  • 用了一段时间的小程序后,发现有的小程序是很实用的,安全可以取代原生APP,例如美团外卖,饿了么等,用小程序就可以实...
    小马甲_97c1阅读 374评论 0 0
  • 序言:出现这个错误是在Extension中欲调用API调起containing App时,意思就是sharedAp...
    yehkong阅读 4,435评论 1 1

友情链接更多精彩内容