Skip to main content
Inspiring
February 19, 2008
Answered

Variable Data Type Error.

  • February 19, 2008
  • 2 replies
  • 321 views
I have a fast question. I am setting up a variable with a data type of Number, when I declare the variable it has a default value.

I am trying to pass data to the variable from a input text box but I am getting an error when I run the Movie. I have tried both a input text box and a Dynamic Text box, but they both produce the same error.

If I remove the data typing from the variable declaration the Movie runs and it updates the variable data but from all that I have read this is not a good practice.

I have attached the code, could someone Please help me out. I do not remember having these types of problems with ActionScript 2.

Thanks for the help.

Code:

var rectangle:Shape;

var thick :Number = 2; // line thickness
var color:Number= 0x000000; // Holds the color value

myButton_btn.addEventListener(MouseEvent.CLICK, onClick);

function onClick(event:MouseEvent):void
{
color = colorField_txt.text; (Line 11)
thick = lineThick_txt.text; (Line 12)
rectangle = new Shape();
rectangle.graphics.lineStyle(thick, color, .5);
rectangle.graphics.drawRect(10, 10, 100, 150);
addChild(rectangle);
}

myButton_btn.buttonMode = true;


ERROR MESSAGE:

1067: Implicit coercion of a value of type String to an unrelated type Number. (Refers to line 11)
1067: Implicit coercion of a value of type String to an unrelated type Number. (Refers to line 12)

Thanks again for any help.

Mike
This topic has been closed for replies.
Correct answer Craig Grummitt
the text property of a textfield is of type String, so to place that String into a variable of type Number, you need to first convert it to a Number
eg.
thick = Number(lineThick_txt.text);

ActionScript 2 performed this conversion automatically.

2 replies

Mike_KingAuthor
Inspiring
February 20, 2008
Craig.

Thanks for the fast response. I tried it and it works great. I don't think I would have ever found that in my manuals.

I really appreciate the help.

Mike
Craig Grummitt
Craig GrummittCorrect answer
Inspiring
February 20, 2008
the text property of a textfield is of type String, so to place that String into a variable of type Number, you need to first convert it to a Number
eg.
thick = Number(lineThick_txt.text);

ActionScript 2 performed this conversion automatically.