require引用失败,xxx is not define

  • Creator 版本:2.07

      const Alert = require('Alert');
    
      cc.Class({
    
          extends: cc.Component,
    
          properties: {
              alert : Alert,
          },
    
          // LIFE-CYCLE CALLBACKS:
    
          onLoad () {
              console.log(this.alert);
              this.alert.init();
              //this.alert.show("提示啦", "钻石不足,创建房间失败!",this.clickCallback,true);
          },
    

我require引用了但是无效

我有这个脚本文件,而且定义了
引用格式是按照官网来的:https://docs.cocos.com/creator/manual/zh/scripting/execution-order.html

具体报错信息,截图一下,可能是Alert文件没有被编译,或者Alert没有导出任何数据。

请问一下,截取Alert文件内容和控制台报错信息吗
我先截这两个给你

cc.Class({

    extends: cc.Component,
    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        _alert:null,
        _btnOK:null,
        _btnCancel:null,
        _title:null,
        _content:null,
        _btnOKCallback:null,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        cc.log('Alert.js onLoad')
        this._alert = cc.find("Canvas/Alert")
        this._title = cc.find("Canvas/Alert/Backgroud/Title").getComponent(cc.Label)
        this._content = cc.find("Canvas/Alert/Backgroud/Content").getComponent(cc.Label)
        this._btnCancel = cc.find("Canvas/Alert/Backgroud/Btn_Cancel").getComponent(cc.Button)
        this._btnOK = cc.find("Canvas/Alert/Backgroud/Btn_OK").getComponent(cc.Button)
        if (this._alert == null) {
            console.log('空')
        }
        if (this._btnCancel instanceof cc.Button) {
            console.log('是个Button')
        } else {
            console.log('不是')
            console.log(this._alert)
        }
        this._btnOK.active = false
        //cc.vv.alert = this
        //cc.vv.alert.show("提示啦", "钻石不足,创建房间失败!",this.clickCallback,true);
     },

    start () {

    },
    onBtnClicked: function(event){
        if (event.target.name == 'Btn_OK') {
            if (this._btnOKCallback) {
                this._btnOKCallback()
            }
        }
        this._alert.active = false
        this._btnOKCallback = null
        console.log("这是全新定义的clicked!!")
    },
    cancelBtnClicked: function(){
        cc.log('我被点中了')
        this._alert.active = false
    },
    show: function(title,content,callback,needCancel){
        console.log('paras ---->: ',title,content,callback,needCancel)
        this._alert.active = true;
        this._btnOKCallback = callback;
        this._title.string = title;
        this._content.string = content;
        if (needCancel) {
            console.log("needCancel ? true");
            this._btnCancel.node.active = true;
            this._btnOK.node.x = -239.5;
            this._btnCancel.node.x = 239.5;
            if(this._btnOK){
                cc.log('也是存在的啊!');
            }
        } else {
            console.log("needCancel ? false")
            this._btnCancel.node.active = false;
            this._btnOK.node.x = 0;
        }
    },
    /*clickCallback: function(){
        cc.log("do clickCallback task.");
    },*/
    // update (dt) {},
});

这是Alert文件里面的代码


这个是网页控制台报错信息,cocos creator 控制台的报错信息是—referError:Alert is not defined
Timg.js引用Alert,alert : Alert这个属性,因为为null,所以就不能使用init方法,应该是require没有引用成功

对了,会可能是数据驱动的原因吗,cocos creator 上面映射了相关属性


才开始学,确实有很多问题

你不应该把挂有Alert脚本的节点拖到属性检查器里面吗?我看着你没拖啊

额,我之前拖了,出现的另外的问题,就是require根本没执行,他直接执行节点脚本了,我想的是直接用代码引用,像iOS开发那样直接代码实例化一个Alert实例和Canver里面的Alert节点映射,在Timg的加载方法中初始化Timg的Alert属性,这样不行吗

cocos creator的结构化数据,也是用引用的方式,引用应该可以直接用吧

可以直接用,打个比方,你可以这样写
const Alert = require(‘Alert’);
Alert.init();
或者properties那alert:cc.Node,在onLoad里面,this.alert.getComponent('Alert).init();(别忘了属性检查器拖进去)

大佬,我想问一下,像这样

一个脚本文件 里面有多个数据类型,我在其他脚本文件怎么选取其中数据类型出来,单独拿个类或者枚举之类,官方的文档里面好像没有说这个问题

大佬后面一种写法


就是这样的

我想问一下,cc.class 都没有预设的init方法吗,是要自己写吗,都是报init is not function
我要做的很简单,就是cocos编辑器里面Alert节点和代码里面的那个alert属性对应就行,属性检查器拖进去也不行
这个alert属性不是显示为空,就是没有对应方法

init没有预设的,得自己定义

建议你把官方的示例看一看,flappybird和摘星星,虽然简单,但是基本规则都有~很有帮助

好的,谢谢,我以为cc的类有预设的init方法,谢谢啊

不客气~~