我照着官方文档做的:制作动态生成内容的列表 http://www.cocos.com/docs/creator/ui/list-with-data.html
代码:
//---------------------------------------------ItemList.js---------------------------------------------------
var Item = cc.Class({
name: 'Item',
properties: {
id: 0,
itemName: '',
itemPrice: 0,
iconSF: cc.SpriteFrame
}
});
cc.Class({
extends: cc.Component,
properties: {
items: {
default: [],
type: [Item]
},
itemPrefab: cc.Prefab
},
// use this for initialization
onLoad () {
for (var i = 0; i < this.items.length; i++) {
var item = cc.instantiate(this.itemPrefab);
var data = this.items[i];
this.node.addChild(item);
item.getComponent('ItemTemplate').init({
id: data.id,
itemName: data.itemName,
itemPrice: data.itemPrice,
iconSF: data.iconSF
});
}
cc.log('length = '+ this.items.length);
for (var value in this.items) {
var element = this.items[value];
cc.log(value + "=====" + JSON.stringify(element));
}
},
// called every frame, uncomment this function to activate update callback
// update: function (dt) {
// },
});
//---------------------------ItemTemplate.js---------------------------------------------
cc.Class({
extends: cc.Component,
properties: {
id: 0,
icon: cc.Sprite,
itemName: cc.Label,
itemPrice: cc.Label
},
init: function (data) {
this.id = data.id;
this.icon.spriteFrame = data.iconSF;
this.itemName.string = data.itemName;
this.itemPrice.string = data.itemPrice;
cc.log('data2 = '+JSON.stringify(data));
},
// use this for initialization
onLoad: function () {
},
// called every frame, uncomment this function to activate update callback
// update: function (dt) {
// },
});
但是做出来没有任何显示,这是怎么回事?我所有操作都是照文档操作的
你们不会怪我啰嗦吧,老是有那么多问题
谢谢你了,因为按照文档来,而且第一次弄,这么简单的问题我都不知道