AS3 syntax to access state color - actionscript-3

Say I have this in mxml (sparkskin) :
<s:SolidColor id="fillColor"
color="0xff0000"
color.selectedOver="0xf74b47"
color.selectedUp="0xf74b47"/>
To change color property in AS3 the syntax is :
fillcolor.color = 0x00ff00;
Now I want to change color.selectedOver in AS3.
Is there a way ?
ie fillcolor['selectedOver'].color = 0x00ff00; ...

You can't access it directly, color is just an integer property in AS. Not sure if there is a better way but you could bind the color value to a variable and change that variable at runtime:
// place this in your Script section
[Bindable]
private var selectedOverColor:int = 0xf74b47;
// bind the color value to your variable
<s:SolidColor id="fillColor"
color="0xff0000"
color.selectedOver="{selectedOverColor}"
color.selectedUp="0xf74b47"/>
// change this variable to the new color somewhere at runtime:
selectedOverColor = 0x000000;

Thanks, I agree.
The only solution seems to override updateDisplayList and use something like :
switch (currentState){
case 'selectedOver':
fillColor.color = 0xff0000;
break;
case 'selectedUp'
fillColor.color = 0xffff00;
break;
...
}

Related

How to call upon a color transform in AS3

If I use colortransform to set a color to an object is there any way I can call on that color through the parent?
for example
var color1 = new ColorTransform(); color1.color = 0xFF0000;
thing1.transform.colorTransform = color1;
If I want to call upon that color assigned to thing1 is there any way to do that?
Some things I have tried
trace(thing1.color);
trace(thing1.colorTransform);
trace(thing1.transform);
I found an alternative that you can call upon that may actually solve most desired functionality.
if you take the object and attach .transform.colorTransform to the end you can call upon any of the 8 values set to that object by the color transform function
trace(thing1.transform.colorTransform);
trace(thing1.transform.colorTransform.blueOffset);
or call a value to relay it to another variable
color2.blueOffset = thing1.transform.colorTransform.blueOffset;

Why can't set "defaultTextFormat" directly?

for example,we can set graphics of a shape directly(without creating an external Graphics variable):
var my_shape:Shape=new Shape();
my_shape.graphics.beginFill(0);
but that's not same as defaultTextFormat
the below code doesn't work:
var my_text:TextField=new TextField();
my_text.defaultTextFormat.size=47;
typing dot after defaultTextFormat,the code hint of text format appears and there is no compiler error but still doesn't work
we must create an external TextFormat variable:
var my_text:TextField=new TextField();
var my_format:TextFormat=new TextFormat();
my_format.size=47;
my_text.defaultTextFormat=my_format;
but why can't set directly?
I don't like a lot of variables.
after that,explain the difference between textFormat and Graphics.
Thanks for your help.
When you access/read a TextField's defaultTextFormat property (which is what's happening in the line my_text.defaultTextFormat.size=47;), you end up getting a whole new object returned. Eg, it creates a new TextFormat and returns that.
Here is an example to illustrate:
var tf:TextFormat = new TextFormat();
textField.defaultTextFormat = tf;
trace(tf == textField.defaultTextFormat) //false
The TextField doesn't know anything about the TextFormat it returns from defaultTextFormat. So when you change it, it doesn't update anything automatically because it has no scope inside the TextField that generated it.
In order to see the change, you have to reassign the whole object, and
then reassign the text (if you've already set the text).
This unfortunately means you'll have do it like in your second example.
It's probably some kind of an efficiency thing under the hood to help prevent memory leaks and the like.
Here are some examples to consider further:
var txt:TextField = new TextField();
addChild(txt);
var tf:TextFormat;
txt.text = "hi"; //default formatting;
tf = txt.defaultTextFormat; //get the default formatting, which actually returns a brand new object
tf.color = 0xFF0000; //make it red;
//nothing has changed visually
txt.defaultTextFormat = tf; //this won't update it either
//nothing has changed visually
txt.text = txt.text; //now that we've 'changed' the text, you'll see red
my_text.defaultTextFormat = my_format;
my_format is the defaultTextFormat of your textField my_text. defaultTextFormat is a property of your TextField (which value is my_format).
my_format.size = 47;
47 is the size of your TextFormat my_format. size is a property of your TextFormat (which value is 47).
my_text.defaultTextFormat.size = 47;
...but size has never been a property of a defaultTextFormat.
So you cannot put properties directly on the defaultTextFormat. What
you need to do is to make a text format, set the properties, THEN set
defaultTextFormat = myTextFormat.
Adobe help about defaultTextFormat.

generating random color for a square

I have a button in flash and I want to generate an automatic color for my square.
I have this function
private function btnClick(event:MouseEvent):void
{
var mycolor :ColorTransform = new ColorTransform();
mycolor.color = (Math.random() * 0xFFFFFF);
parcare.transform.colorTranform = mycolor;
}
I get this error
: 4 Error: Access of undefined property parcare. parcare.transform.colorTranform = mycolor;
Do you know how can I solve it?
Thank you!
this is a scope problem, the random colour generating is fine
the onClick function is outside of the scope of the rest of your class. You need to find a way for the function to know what the variable parcare is.
There is no easy way of fixing it for you without seeing more code.
look a at this question for more info To pass a parameter to event listener in AS3 the simple way... does it exist?
To create a random hex color is:
'#'+Math.floor(Math.random()*16777215).toString(16);
And As A function is:
function random() {
var a = '#'+Math.floor(Math.random()*16777215).toString(16);
return a;
}

make SelectionColor transparent in mx:datagrid

I am using a mx:DataGrid component in my flex application. I showing a grid like this:
I want to change the selected Blue color to transparent. I am using following code:
<mx:DataGrid id="dg" dataProvider="{arrDg}" width="100%" height="100%"
draggableColumns="false" rowCount="{arrDg.length}" click="dgClickHandler(event)"
variableRowHeight="true" resizableColumns="false" sortableColumns="false"
selectionColor="#00000000" verticalScrollPolicy="off">
But this is changing the color to black instead of transparent.
Please help.
[Edit[
Oops, didn't notice you had 8 zero's in that color. Flash doesn't support RGBA (or color values with alpha/transparency).
[end edit]
If you do not want any highlight at all to be shown on selection, try setting the selectable property of the (mx) DataGrid to false. It seems you have a click handler, turning off selection is likely to prevent your click handler from doing it's job :(
If you do want some sort of selection indicator, but say, want to modify the alpha (transparency) of the selection color, that style setting does not exist (ie: there is no "selectionAlpha" style). You would have to create a custom data grid class to do that.
If you need the mx:DataGrid to be selectable but also want the selection color not to affect the color of your cells/rows you can use this approach:
Extend DataGrid an override the function drawSelectionIndicator and leave the content of the function empty (you may also be interested in overriding the drawHighlightIndicator function)
public class CustomRowColorDataGrid extends DataGrid
{
public function CustomRowColorDataGrid()
{
super();
}
override protected function drawSelectionIndicator(indicator:Sprite, x:Number, y:Number, w:Number, h:Number, color:uint, itemRenderer:IListItemRenderer):void
{
//If you want a rectangle arround the selected row use call this method
//Rectangle(indicator,x,y,0xFF9900, 2,h) ;
}
override protected function drawHighlightIndicator(indicator:Sprite, x:Number, y:Number, w:Number, h:Number, color:uint, itemRenderer:IListItemRenderer):void
{
Rectangle(indicator,x,y,highlight, 2,h) ;
}
private function Rectangle(indicator:Sprite, xx:int, yy:int,colorBorde:uint,grosor:int, h:int):void
{
var g:Graphics ;
var w:int ;
w = this.width - this.verticalScrollBar.width-1 ;
g = Sprite(indicator).graphics;
g.clear();
g.lineStyle(grosor,colorBorde) ;
g.drawRect(0,1,w-grosor,h-grosor) ;
indicator.x = xx;
indicator.y = yy;
}
}

AS3: Embedding characters

I having some trouble with TextFields and caracter embedding. As I have understood, the way to embed character in Flash, is to have a TextField in a movieclip that is exported to actionscript via some classname. Then have the TextField embed the characters.
But when i try to use that TextField in my project, I cannot auto resize the field any longer!? Is there a better way to embed charactes? or am I missing some unknow attribute? (and yes i have tried TextField.autoSize = "left" (or "center" or "right")).
The TextField is configured like this in Flash CS4:
Properties:
http://screencast.com/t/0VB6KnNO6G
Library implementation:
http://screencast.com/t/w3yQLqit0veI
And I embed the MovieClip containing the TextField like this:
protected var tabname:MovieClip = new Text(); // The property on the object
Adding the text and setting its Settings:
var txt:TextField = tabname.txt;
if( !contains(tabname) )
{
addChild(tabname);
var format:TextFormat = new TextFormat();
format.bold = true;
format.font = "Arial";
format.size = 12;
format.align = "left";
var dropShadow = new DropShadowFilter(0);
dropShadow.color = 0xFFFFFF;
dropShadow.strength = 2;
dropShadow.blurX = dropShadow.blurY = 5;
dropShadow.alpha = .7;
txt.type = TextFieldType.DYNAMIC;
txt.multiline = tabname.wordWrap = false;
txt.autoSize = TextFieldAutoSize.LEFT;
txt.defaultTextFormat = format;
txt.filters = [dropShadow];
txt.mouseEnabled = false;
txt.x = 10;
}
txt.text = value;
txt.y = Math.ceil((tabmask.height - txt.height) /2);
To embed fonts, don't rely on wrapping them in MovieClips in the library. They should be embedded correctly as Fonts. I have included some basic steps below for embedding fonts, then an example for your particular situation:
1 - Make the textfield Dynamic and click the Embed.. button
2 - Name the font with something meaningful (like the fonts name) and tick the character sets you will be using (usually I select caps, lowercase, numbers and punctuation). Also note the Style is 'Bold', you will need to embed a font set for each style. So if you want to use Bold and Regular, you need to embed 2 fonts.
3 - If you plan on adding textfields dynamically through ActionScript, goto the ActionScript tab and add a class for it (again, use a meaningful name)
4 - Finally click ok, and away you go. I have setup an example, using these steps, and the auto size method, you can see the results below
In Flash, you can click the [Embed...] button below the TextField's character properties. In the window that you get then, you can specify which characters you want embedded in your textfield.
There's a lot more to say about font embedding but this is the simple story. Flash CS5 added TLF TextFields but I don't think you were referring to those, right?
The autoSize property really has nothing to do with font embedding but I guess your TextField is not Dynamic when you cannot auto resize it?
Are you using CS5 or CS4 or earlier by the way?