PDA

View Full Version : change texfield color with button?



patrickjv
March 3rd, 2008, 04:41 PM
Is there a way to "extract/trace()" the color of a dynamically generated texfield? Then use a button to change text color on mouse over and return original on mouse out?




var textvalue:TextField = new TextField();;
var format:TextFormat = new TextFormat();
var colorinmemory:int;
var colorArray:Array = new Array(0xFF0000,0x00FF00,0x0000FF);

format.color = colorArray[Math.floor(Math.random()*colorArray.length)];
textvalue.defaultTextFormat = format;

addChild(textvalue);
textvalue.text = "hello world";

function mouseOverA (event:MouseEvent):void{
//colorinmemory = textvalue.color; // won't work
trace(colorinmemory);
// some way to set text to black
}
a_btn.addEventListener (MouseEvent.MOUSE_OVER, mouseOverA);

function mouseOutA (event:MouseEvent):void{

// some way to re-set text to color
}
a_btn.addEventListener (MouseEvent.MOUSE_OUT, mouseOutA);


looking for some way to fill in the comment lines. Seems so simple but nothing I try works
anyone?
P

FlashingDan
March 3rd, 2008, 05:38 PM
Howdy Patrick!

If the only formatting you're looking to do is to change the text color, you can use the textColor property of the text field instead. The code would then look like this:



var textvalue:TextField = new TextField();

var colorinmemory:int;
var colorArray:Array = new Array(0xFF0000,0x00FF00,0x0000FF);

textvalue.textColor = colorArray[Math.floor(Math.random()*colorArray.length)];

addChild(textvalue);
textvalue.text = "hello world";

function mouseOverA(event:MouseEvent):void
{
colorinmemory = textvalue.textColor;
trace(colorinmemory);
// some way to set text to black
textvalue.textColor = 0x000000;
}
a_btn.addEventListener(MouseEvent.MOUSE_OVER, mouseOverA);

function mouseOutA(event:MouseEvent):void
{
// some way to re-set text to color
textvalue.textColor = colorinmemory;
}
a_btn.addEventListener(MouseEvent.MOUSE_OUT, mouseOutA);


If you do want to do more text formatting that that, you'll need to pull the TextFormat information to access it. In order to do that, you would:



var curFormat:TextFormat = textvalue.getTextFormat();
colorinmemory = int(curFormat.color);


Hope that helps!
Dan

patrickjv
March 3rd, 2008, 05:54 PM
Excellent!
.textColor, much simpler then .color from TextFormat. Does the trick nicely.

I already tried creating a TexFormat duplicate to extract and change .color, but found it overwrote original TextFormat ... which made sense. Your suggestion about getTextFromat to extract color should solve that, will keep it as backup solution.

textColor works fine and is much simpler, probably will implement it that way
Tnx!
P

FlashingDan
March 3rd, 2008, 06:01 PM
Awesome!

You're welcome and happy coding!
-Dan