Pretty new to Actionscript 3.0, so hopefully I can gain some experience here from someone else. :p:
I have a text field that I am trying to place its right edge along the left edge of another textfield. unfortunately, the txtField.width value is not consistant with how many characters are actually in the txtField. In fact, its pretty much double of my first textfield. What even confuses the issue even more, is the fact that I’m using mirrored code on both the first and second txtFields. I’m hoping for some advice here, as I’m running out of things to try. I’ve tried using
var metrics:textLineMetrics = txtField.getLineMetrics(0);
txtField.width = metrics.width;
but unfortunally, that returns a txt field that is truncating the characters in the txtField.
I’ve tried:
txtField.autoSize = TextFieldAutoSize.LEFT//right and center too
but they also returned a txtField that was truncated. I’m adding characters to the text Field with actionscript, so unless I write some code that measure the size of each character that is going into the txtField, and I have five different font sizes, then measure how many of each character are going into the text field, then find the total width and add it to the .width property, I would be in business. But before I go off and write some of that code, I wanted to check to ensure that there wasn’t something else that I am missing. Here is the code that I am using.
var request:URLRequest = new URLRequest("binary.css");
var ld:URLLoader = new URLLoader(request);
var style:StyleSheet = new StyleSheet();
ld.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
ld.addEventListener(Event.COMPLETE, loaderCompleteHandler);
function errorHandler(e:IOErrorEvent):void {
trace( "Couldn't load the style sheet file. " + e);
}
function loaderCompleteHandler(event:Event):void {
style.parseCSS(ld.data);
}
var counter:Number = 1;
//_________________________________________//
//Create the textField that will contain the
//the binary numbers
//_________________________________________//
var binary0:TextField = new TextField();
var binary6:TextField = new TextField();
var binary1:TextField = new TextField();
var binary7:TextField = new TextField();
var binary2:TextField = new TextField();
var binary8:TextField = new TextField();
var binary3:TextField = new TextField();
var binary9:TextField = new TextField();
var binary4:TextField = new TextField();
var binary10:TextField = new TextField();
var binary5:TextField = new TextField();
var binary11:TextField = new TextField();
for (var i = 0; i < 6; i++) {
counter=1;
addChild(this["binary"+i]);
this["binary"+i].width = stage.width*1.2;
this["binary"+i].styleSheet = style;
this["binary"+i].multiline = false;
this["binary"+i].autoSize = TextFieldAutoSize.LEFT
this["binary"+i].htmlText = '<p class='+['"binary'+i]+'">'+['"binary'+i]+randGen()+"</p>";;
/*var metric:TextLineMetrics= this["binary"+i].getLineMetrics(0);
this["binary"+i].width = metric.width;*/
}
for (var j = 6; j < 12; j++) {
counter=1;
addChild(this["binary"+(6+j)]);
this["binary"+j].width = stage.width*1.2;
this["binary"+j].styleSheet = style;
this["binary"+(j)].multiline = false;
this["binary"+(j)].autoSize = TextFieldAutoSize.LEFT;
this["binary"+(j)].htmlText = '<p class='+['"binary'+(j-6]+'">'+['"binary'+j]+randGen()+"</p>";
/*var metrics:TextLineMetrics= this["binary"+(j+6)].getLineMetrics(0);
this["binary"+(j+6)].width = metrics.width + 20;*/
}
trace(binary6.width);
binary0.y=30;
binary6.y=30;
binary6.x=binary0.x-binary6.width;
trace(binary6.x + " " + binary0.x + " " + binary6.width + " " + binary0.width);
binary1.y = 90;
binary7.y = 90;
binary7.x = binary1.x+binary1.width + 5;
binary2.y = 195;
binary8.y = 195;
binary8.x=binary2.x-binary8.width;
binary3.y=260;
binary9.y=260;
binary9.x = binary3.x+binary3.width + 5;
binary4.y=365
binary10.y=365;
binary10.x=binary4.x-binary10.width;
binary5.y=620;
binary11.y=620;
binary9.x = binary5.x+binary5.width + 5;
glide();
addEventListener(Event.ENTER_FRAME, measureAndAdd0);
/*addEventListener(Event.ENTER_FRAME, measureAndAdd1);
addEventListener(Event.ENTER_FRAME, measureAndAdd2);
addEventListener(Event.ENTER_FRAME, measureAndAdd3);
addEventListener(Event.ENTER_FRAME, measureAndAdd4);
addEventListener(Event.ENTER_FRAME, measureAndAdd5);*/
//____________________________________//
// randGen populates the
// textfields with binary numbers
//____________________________________//
function randGen() {
var str:String = "";
for (var j = 0; j < 50; j++) {
if ((j/8) == counter) {
counter++;
str += " ";
}
str += String(Math.round(Math.random()));
}
return str;
}
function glide() {
addEventListener(Event.ENTER_FRAME, right1);
/*addEventListener(Event.ENTER_FRAME, right2);
addEventListener(Event.ENTER_FRAME, right3);
addEventListener(Event.ENTER_FRAME, left3);
addEventListener(Event.ENTER_FRAME, left2);
addEventListener(Event.ENTER_FRAME, left1);*/
}
function removeGlide() {
removeEventListener(Event.ENTER_FRAME, right1);
removeEventListener(Event.ENTER_FRAME, right2);
removeEventListener(Event.ENTER_FRAME, right3);
removeEventListener(Event.ENTER_FRAME, left3);
removeEventListener(Event.ENTER_FRAME, left2);
removeEventListener(Event.ENTER_FRAME, left1);
}
function right1(e:Event) {
binary0.x +=1;
binary6.x +=1;
trace(binary6.x);
}
function right2(e:Event) {
binary2.x +=2;
binary8.x +=2;
}
function right3(e:Event) {
binary4.x +=3;
binary10.x +=3;
}
function left1(e:Event) {
binary1.x -= 3;
binary7.x -= 3;
}
function left2(e:Event) {
binary3.x -= 1;
binary9.x -= 1;
}
function left3(e:Event) {
binary5.x -= 2;
binary11.x -=2;
}
var boo0:Boolean = true;
var boo6:Boolean = true;
var boo1:Boolean = true;
var boo7:Boolean = true;
var boo2:Boolean = true;
var boo8:Boolean = true;
var boo3:Boolean = true;
var boo9:Boolean = true;
var boo4:Boolean = true;
var boo10:Boolean = true;
var boo5:Boolean = true;
var boo11:Boolean = true;
//________________________________//
// the measureAndAdd series
// takes care of the cycling
// of the textfields, creating
// a continuous flow of
// binary numbers
//________________________________//
function measureAndAdd0(e:Event) {
var a:TextField = binary0;
var b:TextField = binary6;
if (a.x > -5 && boo0 && boo6) {
trace("init");
counter = 1;
boo0 = false;
b.htmlText = '<p class="binary0">'+randGen()+"</p>";
b.x = a.x-b.width;
}
if (b.x > -5 && !boo0 &&!boo6) {
trace("init2");
counter = 1;
boo0 = true;
a.htmlText = '<p class="binary0">'+randGen()+"</p>";
a.x = b.x - a.width;
}trace(a.x + " " + boo0 + " " + boo6 + " " + b.x);
if(a.x > 100){
trace('init3');
boo6 = false;
}if(b.x > 100){
trace('init4');
boo6 = true;
}
}
function measureAndAdd1(e:Event) {
var a:TextField = binary2;
var b:TextField = binary8;
if (a.x > -5 && boo2 && boo8) {
counter = 1;
boo2 = false;
b.htmlText = '<p class="binary2">'+randGen()+"</p>";
b.x = a.x-b.width;
}
if (b.x > -5 && !boo2 &&!boo8) {
counter = 1;
boo2 = true;
a.htmlText = '<p class="binary2">'+randGen()+"</p>";
a.x = b.x - a.width;
}if(a.x > 1100)
boo8 = false;
if(b.x > 1100)
boo8 = true;
}
function measureAndAdd2(e:Event) {
var a:TextField = binary4;
var b:TextField = binary10;
if (a.x > -5 && boo4 && boo10) {
counter = 1;
boo4 = false;
b.htmlText = '<p class="binary4">'+randGen()+"</p>";
b.x = a.x-b.width;
}
if (b.x > -5 && !boo4 &&!boo10) {
counter = 1;
boo4 = true;
a.htmlText = '<p class="binary4">'+randGen()+"</p>";
a.x = b.x - a.width;
}if(a.x > 1100)
boo10 = false;
if(b.x > 1100)
boo10 = true;
}
function measureAndAdd3(e:Event) {
var a:TextField = binary1;
var b:TextField = binary7;
if ((a.x+a.width) < (stage.width +10) && boo1 && boo7) {
counter = 1;
boo1 = false;
b.htmlText = '<p class="binary1">'+randGen()+"</p>";
b.x = a.x+a.width;
}
if ((b.x+b.width) < (stage.width +10) && !boo1 &&!boo7) {
counter = 1;
boo1 = true;
a.htmlText = '<p class="binary1">'+randGen()+"</p>";
a.x = b.x + b.width;
}if(a.x <0)
boo7 = false;
if(b.x <0)
boo7 = true;
}
function measureAndAdd4(e:Event) {
var a:TextField = binary3;
var b:TextField = binary9;
if ((a.x+a.width) < (stage.width +10) && boo3 && boo9) {
counter = 1;
boo3 = false;
b.htmlText = '<p class="binary3">'+randGen()+"</p>";
b.x = a.x+a.width;
}
if ((a.x+a.width) < (stage.width +10) && !boo3 &&!boo9) {
counter = 1;
boo3 = true;
a.htmlText = '<p class="binary3">'+randGen()+"</p>";
a.x = b.x + b.width;
}if(a.x <0)
boo9 = false;
if(b.x <0)
boo9 = true;
}
function measureAndAdd5(e:Event) {
var a:TextField = binary5;
var b:TextField = binary11;
if ((a.x+a.width) < (stage.width +10) && boo5 && boo11) {
counter = 1;
boo5 = false;
b.htmlText = '<p class="binary5">'+randGen()+"</p>";
b.x = a.x+a.width;
}
if ((a.x+a.width) < (stage.width +10) && !boo5 &&!boo11) {
counter = 1;
boo5 = true;
a.htmlText = '<p class="binary5">'+randGen()+"</p>";
a.x = b.x + b.width;
}if(a.x <0)
boo11 = false;
if(b.x <0)
boo11 = true;
}
var end:Number = 100;
var storeX:Number = 0;
function zoom() {
end = 100;
storeX = 0;
addEventListener(Event.ENTER_FRAME, scale);
function scale(e:Event):void {
var str:String = String(binary0.htmlText);
end -= 4;
str = str.slice(0, end);
str+="</p>";
binary0.htmlText = str;
binary0.scaleX += 1;
binary0.scaleY += 1;
binary0.x -= (binary0.scaleX - storeX) *100;
storeX = binary0.scaleX;
if (end < 0) {
removeEventListener(Event.ENTER_FRAME, scale);
}
}
}