2014-06-27 22 views
1
var tf:TextFormat = myTextField.getTextFormat(); 
trace(typeof tf.color); // "number" 
trace(tf.color is uint); // true 
var myColor:uint = tf.color; // error: 1118: Implicit coercion of a value with static type Object to a possibly unrelated type Number. 

为什么?为什么TextFormat.color不是数字?

var myColor:uint = int(tf.color); //有效。但为什么我必须施展它?

回答

0

从Adobe的API参考:

color:Object 

所以颜色是对象的类型,第二行描绘出号类型,因为它是默认或代码分配,但并不一定意味着颜色只能是数字。我们可以将字符串类型,颜色对象一样,所以tf.color的类型可以是数字或字符串:

tf.color = "0x00ff00"; 
myTextField.setTextFormat(tf); // Change text color to green 

如果我们比较以下两行:

var myColor:uint = "0x00ff00"; // 1067: Implicit coercion of a value of type String to an unrelated type uint. 
var myColor:uint = tf.color; // 1118: Implicit coercion of a value with static type Object to a possibly unrelated type Number. 
// var myColor:uint = new Object(); // This line gives same 1118: Implicit coercion of a value with static type Object to a possibly unrelated type uint. 

我们可以看到编译器抱怨说它需要明确的指令来执行转换。从这一点来看,我们有足够的理由相信编译器的设计方式。另请注意,您可以使用构造函数uintint将Object转换为数字。 uint and int都是Object的派生类。

var myColor:uint = new uint(tf.color); 

我希望这个灯光。

+0

太棒了!谢谢你的伟大答案。 –

相关问题