动态添加精灵该怎样添加?

代码如下:
properties: {
huahewu:[String],
huahewuInfo:[String],
},

// use this for initialization
onLoad: function () {
    this.huahewu=['Fe(OH)2','NaOH'];
    this.huahewuInfo=['qing yang hua tie ','qing yang hua na '];
    var pp=this.node.parent.getChildByName("scrollview1").getChildByName("view").getChildByName("content");
    for(var i=0;i<this.huahewu.length;i++){
        var node = new cc.Node('Sprite');
        var sp = node.addComponent(cc.Sprite);
        sp.SpriteFrame=new cc.SpriteFrame(cc.url.raw('resources/huahewucard.png'));
        var thisLabel=node.addComponent(cc.Label);
        thisLabel.string=this.huahewu[i];
        thisLabel.name=this.huahewuInfo[i];
        thisLabel.fontSize=34;
        thisLabel.lineHeight=50;
        node.x=-269+(i%3)*252;
        node.y=-151+parseInt(i/3)*310;
        node.parent=pp;
    }
},

结果有两个问题:
1、精灵的背景图不显示;
2、提示一个报错信息:Should not add cc.Label to a node which size is already used by its other component.

这两个元素能显示出来名字,但显示不出来图片。
resourse下,有这个图片。


上传中…

1.应该是sp.spriteFrame
2.Sprite和Label组件不能添加到同一个节点上,你可以考虑把其中一个作为子节点添加

多谢,问题解决了。

求助:

onLoad: function () {
this.menu.getComponent(‘menu’).init(this);

    var node = new cc.Node('Sprite');
    var sp = node.addComponent(cc.Sprite);

   sp.SpriteFrame=new cc.SpriteFrame(cc.url.raw('resources/PurpleMonster.png'));  //这样死活不出来图片,图片位置确认没错。

   //sp.spriteFrame = this.blockSprite;  //改成这个就可以把图片显示出来,搞不清楚为什么不能用上面那一句new出来。
    node.setPosition(0,400);
    node.width = 400;
    node.height = 400;
    this.node.addChild(node);
},