大神们,求救,小弟使用 cocos2d-js 3.0 rc3 遇到一个问题
LabelTTF 定位 代码如下:
var newOptionLabel = cc.LabelTTF.create(OptionsArray);
newOptionLabel.setFontSize(optionLabel.getFontSize());
newOptionLabel.setHorizontalAlignment(cc.TEXT_ALIGNMENT_LEFT);
newOptionLabel.setVerticalAlignment(cc.VERTICAL_TEXT_ALIGNMENT_TOP);
newOptionLabel.setContentSize(optionLabel.getContentSize());
newOptionLabel.boundingWidth = optionLabel.boundingWidth;
newOptionLabel.boundingHeight = optionLabel.boundingHeight;
在cocos code ide 调试的结果和在浏览器上的结果不同


完整的代码
var checkBox;
var jsonData;
var LineSpacing = 25;
var IndexLayer = cc.Layer.extend({
sprite:null,
ctor:function () {
this._super();
//cc.FileUtils.fullPathForFilename("json2.json");
cc.loader.loadJson("res/json/data_Topic.json", function(err, data){
if (err) {
return cc.log("load failed");
} else {
jsonData = data;
}
});
this.schedule(this.timeCallback, 0.1);
return true;
},
timeCallback: function() {
if(jsonData != null) {
this.unschedule(this.timeCallback);
var Layer = ccs.uiReader.widgetFromJsonFile(res.Cocos2dJson);
var submit = ccui.helper.seekWidgetByName(Layer, "Submit_Button");
var list = ccui.helper.seekWidgetByName(Layer, "ListView");
var panel = ccui.helper.seekWidgetByName(Layer, "Panel_10");
var box = ccui.helper.seekWidgetByName(Layer, "CheckBox_11");
var answer = ccui.helper.seekWidgetByName(Layer, "Label_13");
var optionLabel = ccui.helper.seekWidgetByName(Layer, "Label_12");
checkBox = new Array();
for(var i = 0; i < jsonData.length; i++) {
var question = jsonData*"Question"];
var id = jsonData*"ID"];
var Options = jsonData*"Options"];
var OptionsArray = Options.split("|");
var newPanels = ccui.Layout.create();
newPanels.setBackGroundColorType(ccui.Layout.BG_COLOR_SOLID);
newPanels.setBackGroundColor(cc.color(10 * j, 10*j, 10*j));
//newPanel.setContentSize(panel.getContentSize());
newPanels.width = panel.width;
var addHeight = ((OptionsArray.length - 1) * box.height);
newPanels.height = panel.height + addHeight + OptionsArray.length * LineSpacing;
//复制题目层
//var label = new cc.LabelTTF.create(i + 1 + "、" + question, "", 32, cc.size(50, 50), cc.TEXT_ALIGNMENT_LEFT);
var label = new cc.LabelTTF("alignment left", "", 32, cc.size(50, 50), cc.TEXT_ALIGNMENT_LEFT);
label.setFontSize(answer.getFontSize());
label.setHorizontalAlignment(cc.TEXT_ALIGNMENT_LEFT);
label.setVerticalAlignment(cc.VERTICAL_TEXT_ALIGNMENT_TOP);
label.setContentSize(answer.getContentSize());
label.x = answer.x;
label.y = answer.y + addHeight + (OptionsArray.length - 1) * LineSpacing;
label.boundingWidth = answer.boundingWidth;
label.boundingHeight = answer.boundingHeight;
newPanels.addChild(label);
//复制单选框层
checkBox* = new Array();
checkBox*"id"] = id;
for(var j = 0; j < OptionsArray.length; j++) {
checkBox* = new Array();
checkBox*"value"] = ccui.CheckBox.create();
checkBox*"value"].setTouchEnabled(true);
checkBox*"value"].loadTextures("res/publish/GUI/selected01.png", "res/publish/GUI/selected02.png", "res/publish/GUI/selected02.png", "", "")
checkBox*"value"].x = box.x;
var checkBoxHeight = box.height;
//checkBox*"value"].y = box.y;
checkBox*"value"].boundingWidth = box.boundingWidth;
checkBox*"value"].boundingHeight = box.boundingHeight;
checkBox*"value"].addTouchEventListener(this.checkBoxEvent, this);
//选项内容
var newOptionLabel = new cc.LabelTTF.create(OptionsArray);
newOptionLabel.setFontSize(optionLabel.getFontSize());
newOptionLabel.setHorizontalAlignment(cc.TEXT_ALIGNMENT_LEFT);
newOptionLabel.setVerticalAlignment(cc.VERTICAL_TEXT_ALIGNMENT_TOP);
newOptionLabel.setContentSize(optionLabel.getContentSize());
newOptionLabel.boundingWidth = optionLabel.boundingWidth;
newOptionLabel.boundingHeight = optionLabel.boundingHeight;
newOptionLabel.x = optionLabel.x;
if(j > 0) {
var yPos = label.y - checkBoxHeight / 2 - LineSpacing - (checkBoxHeight * j) - (j * LineSpacing);
}
else {
var yPos = label.y - checkBoxHeight / 2 - LineSpacing;
}
checkBox*"value"].y = yPos;
cc.log(checkBoxHeight);
newOptionLabel.y = yPos;
newPanels.addChild(checkBox*"value"]);
newPanels.addChild(newOptionLabel);
}
list.pushBackCustomItem(newPanels);
}
submit.addTouchEventListener(this.touchEvent, this);
this.addChild(Layer);
}
},
touchEvent: function(sender, type) {
switch (type) {
case ccui.Widget.TOUCH_ENDED:
break;
default:
break;
}
},
checkBoxEvent: function(sender, type) {
switch (type) {
case ccui.Widget.TOUCH_ENDED:
break;
default:
break;
}
},
});
var IndexScene = cc.Scene.extend({
onEnter:function () {
this._super();
var layer = new IndexLayer();
this.addChild(layer);
}
});