cocos 2dx 通过循环实现界面图形的摆放

首先创建一个一维数组

this.starSprites = new Array();

然后知道星星的间距和坐标后通过如下代码实现位置的摆放

for(var i = 0; i < 6; i++)
{
this.starSprites[i] = new cc.Sprite(res.Stars02_png);
this.starSprites[i].attr({
x: (975 + 40*i),
y: size.height-27,
});
this.addChild(this.starSprites[i], 0);
}



取代了如下代码
this.starSprite1 = new cc.Sprite(res.Stars02_png);
this.starSprite1.attr({
x: 975,
y: size.height-27,
});
this.addChild(this.starSprite1, 0);

//add stars 2
this.starSprite2 = new cc.Sprite(res.Stars02_png);
this.starSprite2.attr({
x: 1015,
y: size.height-27,
});
this.addChild(this.starSprite2, 0);


//add stars02 3
this.starSprite3 = new cc.Sprite(res.Stars02_png);
this.starSprite3.attr({
x: 1055,
y: size.height-27,
});
this.addChild(this.starSprite3, 0);


//add stars02 4
this.starSprite4 = new cc.Sprite(res.Stars02_png);
this.starSprite4.attr({
x: 1095,
y: size.height-27,
});
this.addChild(this.starSprite4, 0);


//add stars02 5
this.starSprite5 = new cc.Sprite(res.Stars02_png);
this.starSprite5.attr({
x: 1135,
y: size.height-27,
});
this.addChild(this.starSprite5, 0);


//add stars02 6
this.starSprite6 = new cc.Sprite(res.Stars02_png);
this.starSprite6.attr({
x: 1175,
y: size.height-27,
});
this.addChild(this.starSprite6, 0);
原文地址:https://www.cnblogs.com/fenr9/p/5166926.html