腳本延遲交互呼叫的用法

  • Creator 版本:2.3.3

對於延遲和繼承的用法之請教~

出現錯誤: Cannot read property ‘function的名稱’ of null

Game.js

 var Game = cc.Class({
     properties: () => ({
         item: {
             default: null,
             type: require("Item")
         }
     }),
     start(){
        this.item.FuncB() // 請問這樣呼叫有錯誤嗎?
     },
     FuncA(){
        cc.log("AAA")
     },
 });

 module.exports = Game;

Item.js

 var Item = cc.Class({
     properties: () => ({
         game: {
             default: null,
             type: require("Game")
         }
     }),
     start(){
        this.game.FuncA()  // 請問這樣呼叫有錯誤嗎?
     },
     FuncB(){
        cc.log("BBB")
     },
 });

 module.exports = Item;

請個位幫忙解惑 謝謝

这样在编译阶段就出错了,循环引用了

這段是官方提供的
說是使用 lambda 表達式 就不會有循環引用
但沒有細講要如何呼叫

[quote=“Mingo428, post:1, topic:96931”]
start(){
this.item.FuncB() // 請問這樣呼叫有錯誤嗎?
},
[/quote]这个呼叫没问题,但是你还没给 this.item 赋值,你看上面的default:null,这时候它还是 null
所以出現錯誤: Cannot read property ‘function的名稱’ of null

http://www.ruanyifeng.com/blog/2015/11/circular-dependency.html
看看这个CommonJS的规则,或许会明白点

哦对,也许没赋值,楼下正解

1赞

你看你的这里是不是这样
把带着 Item.js 脚本的 node 拖到 Item 这里就是给 this.item 赋值了,同理给 Item.js 的 this.game 赋值

1赞

原來還是要拉進去 非常感謝 !!
後來找到替代方式 直接呼叫Node上的腳本組件