[]【求助】关于Cannot read property 'visible' of undefined问题

我的代码是这样子的:
private function showFoods():void{
foodSprite = new Sprite();
Laya.stage.addChild(foodSprite);

this.index = 0;

foodsList = new Array();
foodsList.push(new Food("菜品1", "1", 0));
foodsList.push(new Food("菜品2", "2", 1));
foodsList.push(new Food("菜品3", "3", 2));
foodsList.push(new Food("菜品4", "4", 3));
foodsList.push(new Food("菜品5", "5", 4));

foodSprite.addChildren(foodsList);
addFoodListener();
}

private function addFoodListener():void{
console.log(foodsList[this.index]);
this.currentFood = this.foodsList[this.index];
console.log(currentFood);
this.currentFood.on(Event.MOUSE_DOWN, this, onMouseDown);
this.currentFood.on(Event.MOUSE_UP, this, onMouseUp);
}
currentFood也是Food类,是用来指向在foodList中处于下标index的元素,我是想在用currentFood指向某个元素,给这个元素添加事件,让其可以执行一些动作,但是这段代码运行后,报出了Cannot read property 'visible' of undefined这个错误:
TypeError: Cannot read property 'visible' of undefined
at RenderSprite3D.__proto._childs (file:///D:/Projects/Study/three/bin/h5/Main.max.js:6529:29)
at Sprite.__proto.render (file:///D:/Projects/Study/three/bin/h5/Main.max.js:21868:42)
at RenderSprite3D.__proto._childs (file:///D:/Projects/Study/three/bin/h5/Main.max.js:6529:45)
at RenderSprite3D.__proto._transform (file:///D:/Projects/Study/three/bin/h5/Main.max.js:20706:15)
at Stage.__proto.render (file:///D:/Projects/Study/three/bin/h5/Main.max.js:21868:42)
at Stage.__proto.render (file:///D:/Projects/Study/three/bin/h5/Main.max.js:25799:29)
at Stage.__proto._loop (file:///D:/Projects/Study/three/bin/h5/Main.max.js:25729:8)
at loop (file:///D:/Projects/Study/three/bin/h5/Main.max.js:5798:15)
求高手解答,或者可以实现我这个需求的另一种思路,谢谢各位了。
已邀请:

Laya_XS

赞同来自:

你这个报错,显然是用的变量出现的空对象,你断点跟踪下就知道原因了,这个自己应该可以解决。

要回复问题请先

商务合作
商务合作