Flash: asfunction and Scope to Class instances
May 17th, 2005 by LuisI just started to use a lot the lessrain.lib.text.DynamicTextField to handle different types of textfields and textformatting.
There are some cases where I have to use asfunction together with the Proxy Class in order to make a call back to the class instance.
When a Class uses a TextField by composition the asfunction call is going to scope to the TextField's host MovieClip. With the help of the Proxy we can resolve this , calling the asfunction on the parent of the host mc.
I feel like posting an example of how to use the DynamicTextField Class, the Proxy Class and asfunction in one single sceneario.
Class Test (Click here to download the example files)
NOTES :
-
1) The url parameter for the TEXT_FORMAT is null instead of an empty String.
-
2) There is a new method in the DynamicTextField called fieldPath that returns the full path for the textField.
- import lessrain.lib.text.DynamicTextField;
- import lessrain.lib.utils.Proxy;
- class Test{
- private var testFontName:String;
- private var testFontColor:Number;
- private var testFontSize:Number;
- private var displayText:DynamicTextField;
- public var TEXT_FORMAT:TextFormat;
- function Test()
- {
- testFontName="Futura LT Condensed";
- testFontSize=14;
- testFontColor=0x000000;
- TEXT_FORMAT = new TextFormat( testFontName, testFontSize, testFontColor, null, null, null, null, "", "left", 0, 0, 0, 2)
- }
- public function init(_targetMc:MovieClip):Void
- {
- displayText=DynamicTextField(_targetMc.attachMovie("DynamicTextField","txt_mc",0));
- displayText.textFormat = TEXT_FORMAT;
- displayText.fieldWidth = 400;
- displayText.isFixedWidth=true;
- displayText.isMultiline=true;
- displayText.isAutosize='left';
- displayText.isSelectable =true;
- displayText.wordwrap=true;
- displayText.create();
- displayText.fieldPath._parent.callFunction=Proxy.create(this, executeFunction);
- var tempText:String ="Lorem Ipsum is simply dummy text of the printing and typesetting industry. ";
- tempText +="<a href='asfunction:callFunction,this argument 1,this argument 2'><u>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, </u></a>";
- tempText +="when an unknown printer took a galley of type and scrambled it to make a type specimen book";
- displayText.text=tempText;
- displayText._x = 0;
- displayText._y = 0;
- }
- private function executeFunction():Void
- {
- var argumentArray:Array = new Array();
- argumentArray = arguments[0].split(",");
- var l:Number=argumentArray.length
- for(var i:Number=0;i<l ;++i) trace('argument '+i+': '+argumentArray[i])
- }
- }



July 16th, 2005 at 1:32 am
hello,
i experiment a problem this days.
i don’t know if the solution is in your tutorial, but it’s the nearest i can find in the web.
i explain:
i’ve 2 classes called Article and Content.
in Article i use a TextField with html.
i display some news in this textfield with links in the text.
with those links i use asfunction to scope a function (which is the basic use of asfunction).
the problem is that i want to scope a method which is not in _root or _parent levels of the movieClip.
i want to scope a function called addComment in the Content class.
i try differents possibilities but nothing is working for the moment.
i try to use:
- asfunction:Content.addComment. call to other class has no effect.
- asfunction:this.addComment with a addComment function in Article and redirect to addComment in Content. this has no effect.
if you have an iodea to solve this problem, maybe with your Proxy class or by using Delegate.
thanks by advance.
Manuvers
October 18th, 2005 at 5:21 pm
This can also be done without the proxy class. You can instead create a proxy function that redirects to the class method. It’s basically the same thing, but uses existing techniques.
Thankfully, AS3 should do away with having to do this at all.
http://www.nathanderksen.com/blog/?p=20
August 4th, 2006 at 9:56 pm
[…] array of children. Same thing. Different phrasing. Here are two other ways to handle it: Luis of Lessrain and Nathan Derksen of nathanderksen.com Again, basically the same thing just different phra […]
August 21st, 2006 at 5:35 pm
omg!!!!! lolz u r teh uber tek