You technically cannot do this, since callLater() actually takes a function as an argument and a setter is an accessor.
However, rules where meant to be broken (or at least worked around).
You can do it by creating a function inline which sets the property within your callLater invocation.
Here is a simple class with a method and a setter:
package
{
import mx.controls.Alert;
public class MyClass
{
public function set stuff(value:String):void{
Alert.show("setter for stuff: " + value);
}
public function junk(value:String):void{
Alert.show("function junk: " + value);
}
}
}
Here is a simple app demonstrating the workaround:
<?
xml version=
"1.0" encoding=
"utf-8"?>
<mx:Application xmlns:mx=
"http://www.adobe.com/2006/mxml" creationComplete=
"init()">
<mx:Script>
<!
[CDATA
[
public var foo:MyClass;
public function init
():
void{
foo=
new MyClass
();
}
]]>
</mx:Script>
<!–
The following will not compile with this error:
Error 1119: Access of possibly undefined property stuff through a reference with static type MyClass.
<mx:Button label="call setter within callLater"
click="callLater(foo.stuff,[’bunch of stuff’])"/>
–>
<mx:Button label="call function within callLater"
click="callLater(foo.junk,[’bunch of junk’])"/>
<mx:Button label="call setter within callLater"
click="callLater(function():void{foo.stuff=’bunch of stuff’})"/>
</mx:Application>
A complete Flex Builder 2.0.1 Project Archive (.zip) of this sample can be found here.
No Comments »
I wrote a simple little extension to ComboBox to allow the dropdown to expand to the width of the widest entry and the ComboBox input to expand/contract to the width of the selected item. This is based on the simple ComboBox sample from the docs found here.
Here is the source code for the component:
<?
xml version=
"1.0" encoding=
"utf-8"?>
<mx:ComboBox xmlns:mx=
"http://www.adobe.com/2006/mxml" creationComplete=
"init()" change=
"onChange(event);">
<mx:Script>
<!
[CDATA
[
import mx.
controls.
ComboBase;
import mx.
core.
EdgeMetrics;
private function init
():
void{
this.
dropdownWidth=calculatePreferredSizeFromData
(this.
dataProvider.
length).
width;
}
private function onChange
(e:Event
):
void{
var txt:
String = itemToLabel
(selectedItem
);
if(measureText
(txt
).
width >
0){
var buttonWidth:
Number =
getStyle("arrowButtonWidth");
// Text fields have 4 pixels of white space added to each side
// by the player, so fudge this amount.
// If we don’t have any data, measure a single space char for defaults
var bm:EdgeMetrics = borderMetrics;
var textWidth:Number = measureText(txt).width + bm.left + bm.right + 8;
this.width = textWidth + buttonWidth;
}
else {
this.width=DEFAULT_MEASURED_MIN_WIDTH
}
}
]]>
</mx:Script>
</mx:ComboBox>
Read the rest of this post»
No Comments »
I recently had a customer who was trying to debug his Flex components. Under certain circumstances the updateDisplayList() method of his component was getting called and it wasn’t obvious why.
In writing and debugging Flex components sometimes it would be useful to be able to see who called what method to figure out what is going on. Profilers are good for this, but unfortunately there is no Flex 2.0 (or 2.0.1) profiler. In speaking with Flex Engineer, Alex Harui (see his blog here), he suggested the strategy below:
- Subclass the component you are interested in (or if you are writing your own components just add the following to your component).
- Override the invalidateDislayList() method (or whatever method you are interested in).
- Create a new error object.
- Dump out the error objects getStackTrace() method.
This should get you the hierarchy of calls that caused the invalidateDisplayList call.
(And it is the calling of invalidateDisplayList that will flag the updateDisplayList() to be called.)
Read the rest of this post»
2 Comments »
This is installment 3 in the series.
The previous 2 related posts are:
Changing embedded True Type fonts at Runtime in Flex Applications
Using Modules to Change embedded True Type fonts at Runtime in Flex Applications
This approach is a little different.
The application is very similar, but instead of using a loader or module loading, I use the StyleManager to load a runtime CSS swf (which was compiled from a CSS file).
Here is one of the style sheets that is tuned into a CSS swf:
/* CSS file */
@font-face {
src:
url("assets/arial.ttf");
fontFamily: myFont;
}
@font-face {
/* Note the different filename for boldface. */
src:url("assets/arialbd.ttf");
fontFamily: myFont; /* Notice that this is the same alias. */
fontWeight: bold;
}
@font-face {
/* Note the different filename for italic face. */
src:url("assets/ariali.ttf");
fontFamily: myFont; /* Notice that this is the same alias. */
fontStyle: italic;
}
@font-face {
/* Note the different filename for bold-italic face. */
src:url("assets/arialbi.ttf");
fontFamily: myFont; /* Notice that this is the same alias. */
fontWeight: bold;
fontStyle: italic;
}
.myPlainStyle {
fontSize: 11;
fontFamily: myFont;
}
.myBoldStyle {
fontSize: 11;
fontFamily: myFont;
fontWeight: bold;
}
.myItalicStyle {
fontSize: 11;
fontFamily: myFont;
fontStyle: italic;
}
.myBoldItalicStyle {
fontSize: 11;
fontFamily: myFont;
fontWeight: bold;
fontStyle: italic;
}
Read the rest of this post»
6 Comments »
This is really part 2 in the series. Part 1 was Changing embedded True Type fonts at Runtime in Flex Applications
The interface that I have each font swf implementing is the same, so I have not posted the code.
The application has undergone some slight refactoring and I have added a few things.
I chose to use the ModuleManager.getModule(url) rather than using the mx:ModuleLoader tag.
The module Manager was more flexible and really should be used for loading non-visual modules.
Note the way that you get access to the loaded module after it has loaded:
var ml:IModuleInfo = e.target as IModuleInfo;
loadedFont = ml.factory.create()as IFontModule;
Read the rest of this post»
1 Comment »
[Bindable] actually generates getter/setters, which then show up as accessors during introspection. This actually confused myself and a customer for a little while until I realized that of course, the binding generates getters and setters which means that what where once normal properties now are more than just properties. Run the code with the [Bindable] tag commented and uncommented to see the difference.
MyClass.as:
package
{
import mx.collections.ArrayCollection;
// [Bindable]
public class MyClass {
public var someNumber:Number;
public var someString:String;
public var someInt:int;
private var _somePrivateString:String;
public function MyClass()
{
this.someNumber = -1;
this.someString = ‘’;
this.someInt = 1;
}
public function get somePrivateString():String{
return _somePrivateString;
}
public function set somePrivateString(s:String):void{
_somePrivateString=s;
}
}
}
main.mxml:
<?
xml version=
"1.0"?>
<!– usingas/IntrospectionAPI.
mxml –>
<mx:Application xmlns:mx=
"http://www.adobe.com/2006/mxml" xmlns=
"*" creationComplete=
"getDetails()">
<mx:Script><!
[CDATA
[
public function getDetails
():
void {
// Get the Button control’s E4X XML object description.
var mcl:MyClass=
new MyClass
()
var classInfo:
XML = describeType
(mcl
);
// Dump the entire E4X XML object into ta2.
ta2.text = classInfo.toString();
// List the class name.
ta1.text = "Class " + classInfo.@name.toString() + "\n";
// List the object’s variables, their values, and their types.
for each (var v:XML in classInfo..variable) {
ta1.text += "Variable " + v.@name + "=" + mcl[v.@name] +
" (" + v.@type + ")\n";
}
// List accessors as properties.
for each (var a:XML in classInfo..accessor) {
// Do not get the property value if it is write only.
if (a.@access == ‘writeonly’) {
ta1.text += "Property " + a.@name + " (" + a.@type +")\n";
}
else {
ta1.text += "Property " + a.@name + "=" +
mcl[a.@name] + " (" + a.@type +")\n";
}
}
// List the object’s methods.
for each (var m:XML in classInfo..method) {
ta1.text += "Method " + m.@name + "():" + m.@returnType + "\n";
}
}
]]></mx:Script>
<mx:Label text="Enumeration of MyClass.as’s properties, accessors and methods."/>
<mx:TextArea id="ta1" width="400" height="200"/>
<mx:Label text="E4X XML object description of MyClass.as"/>
<mx:TextArea id="ta2" width="400" height="600"/>
</mx:Application>
No Comments »
I’ve had a few customers ask me how they can switch out embedded fonts at runtime. This really isn’t too difficult and basically involves loading swfs at runtime that have the appropriate fonts embedded within.
You register the font after the “font swf” is loaded and apply the newly loaded font to whatever components you like. Check out the code and comments below.
First I define an interface that I want all my font swf classes to implement so I know how to determine what font(s) are loaded.
IFontModule.as:
package
{
public interface IFontModule
{
function get fontName_Normal():String;
function get fontName_Bold():String;
function get fontName_Italic():String;
function get fontName_BoldItalic():String;
}
}
Read the rest of this post»
1 Comment »
I’ve been asked this by at least 4 customers and provided them with simple samples that set the stage.quality = StageQuality.BEST. This provides smoother image scaling, but at potentially some performance cost. You also have to use the loaded image’s content property, casting it to a bitmap in order to scale it. I don’t recall exactly where I found the info on this, but I Googled and didn’t find a good reference for doing this, so I thought I’d post my own solution.
Check out the code snippet:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
applicationComplete="stage.quality = StageQuality.BEST">
<mx:Script>
<![CDATA[
private function handleImageComplete(event: Event): void {
var bitmap: Bitmap = ((event.target as Image).content as Bitmap);
if (bitmap != null) {
bitmap.smoothing = true;
}
}
]]>
</mx:Script>
<mx:HSlider id="zoomSlider"
height="150"
buttonMode="true" useHandCursor="true"
minimum="1" maximum="10"
snapInterval="1" tickInterval="1" value="1"/>
<mx:Image height="600" width="800"
scaleX="{zoomSlider.value}" scaleY="{zoomSlider.value}"
source="me.jpg" complete="handleImageComplete(event)" />
</mx:Application>
No Comments »
I had a customer request some help in modifying the default Flex ComboBox behavior such that when the dropdown list was open and a user was scrolling the mousewheel with the mouse outside the dropdown list, the dropdown list would scroll. The default behavior is to close the dropdown list as soon as the user scrolls the mousewheel outside the dropdown list.
Read the rest of this post»
No Comments »
Recent Comments