how do i solve this bad error
as you see this is a class create 4 text Fields , what i woona do is in this line of code first1[i].text = k1[i]; in the for loop to write the randomize numbers in the TextFields
that's my code
import flash.display.Sprite;
import flash.display.DisplayObjectContainer;
import flash.display.InteractiveObject;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
public class addClass extends Sprite {
public var first1:Array = new Array();
public var i:uint;
public var k1:Array = new Array();
public function addCl开发者_开发知识库ass() {
for (i= 1 ; i<= 5; i++)
{ first1[i]= createCustomTextField(100,(i*40),50,30);
k1[i]=Math.round(Math.random()*10);
// here is the problem
first1[i].text = k1[i];
}
private function createCustomTextField(x:Number, y:Number, width:Number, height:Number):TextField
{
var result:TextField=new TextField ;
var format:TextFormat = new TextFormat();
result.x=x;
result.y=y;
result.width=width;
result.height=height;
result.background=false;
result.border=true;
result.selectable=false;
result.restrict="0-9";
format.size = 24;
format.color = 0xFFFFFF;
result.defaultTextFormat = format;
addChild(result);
return result;
}
What problem are you having? Compile or runtime errors? If so, what error are you receiving? My first guess is that you need to cast your reference to your text field, something like so:
(first1[i] as TextField).text = k1[i];
Another issue I see in your code is that you are adding children to your sprite in the constructor. That could be problematic since you shouldn't really be adding children until later in the lifecycle.
for (i= 1 ; i<= 5; i++)
well this for loop you are using seems to be improper to generate four textfields. I think you want :
for (i= 0 ; i< 5; i++)
Also as wade suggested you might also wanna make sure that the textbox returned is cast into an array element properly.
Wasn't able to reproduce the error. This code is working , the problem may be elsewhere. If anything, you should do this:
first1[i].text = k1[i].toString();
but it still works. Also, yes , you are creating 5 boxes . not 4
精彩评论