[LayaAir 1.0]Text.as中parseLines方法里的问题
是这样的,上线的小游戏,在微信后台中看到有错误日志:
TypeError: undefined is not an object (evaluating 's.width') at line 3 in https://usr/game.js
_parseLines@https://usr/game.js:3:256231
typeset@https://usr/game.js:3:255040
typeset@https://usr/game.js:48:211550
…………
查了下源码,
定位到Text.as中parseLines()中的_charSize.width = measureResult.width;这行里measureResult这个为undefined,
而measureResult是Browser.context.measureText(_testWord)这样子来的,
所以这个measureText方法可能返回空值吗?
上下文:
TypeError: undefined is not an object (evaluating 's.width') at line 3 in https://usr/game.js
_parseLines@https://usr/game.js:3:256231
typeset@https://usr/game.js:3:255040
typeset@https://usr/game.js:48:211550
…………
查了下源码,
定位到Text.as中parseLines()中的_charSize.width = measureResult.width;这行里measureResult这个为undefined,
而measureResult是Browser.context.measureText(_testWord)这样子来的,
所以这个measureText方法可能返回空值吗?
上下文:
/**
* @private
* 分析文本换行。
*/
protected function parseLines(text:String):void {
//自动换行和HIDDEN都需要计算换行位置或截断位置
var needWordWrapOrTruncate:Boolean = wordWrap || this.overflow == HIDDEN;
if (needWordWrapOrTruncate) {
var wordWrapWidth:Number = getWordWrapWidth();
}
if (_currBitmapFont) {
_charSize.width = _currBitmapFont.getMaxWidth();
_charSize.height = _currBitmapFont.getMaxHeight();
} else {
var measureResult:* = Browser.context.measureText(_testWord);
_charSize.width = measureResult.width;
_charSize.height = (measureResult.height || fontSize);
}
var lines:Array = text.replace(/\r\n/g, "\n").split("\n");
for (var i:int = 0, n:int = lines.length; i < n; i++) {
var line:String = lines[i];
// 开启了自动换行需要计算换行位置
// overflow为hidden需要计算截断位置
if (needWordWrapOrTruncate)
parseLine(line, wordWrapWidth);
else {
_lineWidths.push(getTextWidth(line));
_lines.push(line);
}
}
}[/i]
没有找到相关结果
已邀请:
1 个回复
Aar0n
赞同来自:
在引擎初始化之后
if (Browser.onMiniGame)
{
var ctxProto:Object = __JS__("CanvasRenderingContext2D.prototype");
ctxProto.measureText1 = ctxProto.measureText;
ctxProto.measureText = function(str:String):Object
{
return this.measureText1(str) || {width:12*(str+"").length};
};
}