How do I manipulate layout of spark/mx components via actionscript? - actionscript-3

I know I can create an actionscript loop in a script block that can create an array of objects. The problem is when doing that I don't know how to have those objects interact with the overall layout structure of the mxml document itself. They just default to 0,0.
What If I wanted to arrange a loop of dynamically generated objects inside of a tilegroup and under another dynamically generated item using actionscript for instance? (without individually specifyin x&y)
I need to know how to give them an automatic layout (like horizontal) and then determine where they fall in the order of other objects declared via mxml?

You can just add them as elements to the container group of your choice, just use:
myVGroup.addElement( myComponent );
Inside your loop. Where myVGroup is a VGroup that already exists in your layout.
You may also find this of interest. http://evtimmy.com/2009/06/flowlayout-a-spark-custom-layout-example/

Related

clone Xaml FramworkElement in WinRT

i need to clone a FrameworkELement in my CodeBehind in WinRT...
I did found a solution in the internet, though this workaround doesn't work in WinRT because the XamlWriter is NOT available in winRT! Is there an easy/built-in way to get an exact copy (clone) of a XAML element?
is there any other way to get another instance of my FrameworkElement?
I don't think there is an easy way to clone an element exactly - I don't know for example if there is a way to figure out the arbitrary attached properties set on one or figure out if properties are set by style, animation, template, explicit value etc.
There is one way that would possibly be a solution for your scenario if you have a specific element tree you want cloned - simply put it in a DataTemplate in XAML and then retrieve that template by name or resource key in code behind and call LoadContent() to generate an instance from the template.
If you have your original one in your XAML already that you don't want to put in resources and generate or lay out from code behind again - simply wrap it inside ContentControl/ContentTemplate/DataTemplate.

Getting a list of the rendered items

I have my own custom component. This component extends from a basic container. I want to be able to access the itemRenderer instances that are being visualized. I know that the component mx:list has an internal getter that provides an array of Arrays containing the itemRenderer instances that render each data provider item. I want the same thing. Any idea how of how to do that?
To be more specific: I am setting the selected property of my dataProvider items to true or false. From the updateDisplayList funcion of my ItemRenderer I check for changes of the property and correct the border color of the selected ones. Unfortunately I have to force the updateDisplayList function. I already did this once on a ItemRenderer from a list. Only with the list it was practical because by making my own list I was able to get the list of items being rendered and therefore visualized (cannot be many). It was no overhead to go trough the rendered Items and updateDisplayList. But in this case I can have 100 items. Imagine checking and changing styles on so many items. Thanks
The Flex architects intentionally made this difficult to do, because they are properly encapsulating the component. In short, to even try to do this is a violation of good OOP principles.
That said, about 90% of the things you are probably trying to do can be done by manipulating the data item, and the remaining 10% can be done by using a ClassFactory for your itemRenderer that sets a custom property on your itemRenderer to a callback where you can look at the data available to the containing context and provide back a value based on that.
If you elaborate a bit more on your end goal, I can give you more specifics.
Edit in light of clarification:
You need to make your data object class dispatch an event when it changes (one way is to make it bindable, or just make the selected property bindable). Then, in your renderer, listen for the change event and take the appropriate action.
A second way to handle this would just be to refresh() the collection, storing the selectedItem first (if you care about that) and resetting it once the refresh has finished.
I believe you can access the itemRenderer instances through getChildAt method. Flex 3's container overrides "getChildAt", "numChildren", given that some children are logical children, while some are decorative children such as background, border and scrollbars.
Keep in mind that itemRenderer may not right away become available upon dataProvider assignment, as they may be created during the next component lifecycle. Check with the underlying container's documentation and find out which event to be listened when the renderers are surely created, so you can reliably access them.

Correct use of addChild

I'm new to coding and AS3. I was reading about adding things to the stage using AS3 and learned about the addChild method. Reading more I found that there are different ways to use it. I also read that some ways are better than others, and that some ways are not good at all and better avoided.
I don't trust those sources though. As a coding newbie I come asking for help, StackOverflow. I'm about to start an addChild heavy project and I want to start it with the right implementations. I trust you, so let me ask you this: What's the correct use of addChild?
I just want to add things to the stage, but I read that it's not good to add them directly to the stage (without further argument though).
stage.addChild()
this.addChild()
addChild()
Are there other ways? Which one should I use?
Thanks for your time. :)
As the top-level container for all display objects in the display list hierarchy, there is only one Stage no matter how many SWF files are loaded into the runtime. So, generally, objects should not be added to the Stage, directly, at all. The only object the Stage should contain is the root object.
Generally, you should not use: stage.addChild()
Adding a DisplayObject to the display list should be performed within the scope of a DisplayObjectContainer.
Each SWF file has an associated ActionScript class, known as the main class of the SWF file which extends a display object. From this class or any child within the hierarchy you may call addChild().
The following are equal, and would add a child within the scope of the current display object container.
this.addChild()
addChild()
The this keyword explicity defines scope; however, is generally implicit when left off.
While a display object added via addChild() is added to the front (top) of all other children, to add a child to a specific index position, use the addChildAt() method.
References:
Display Programming
Basics of display programming
Adding display objects to the display list

Dynamic Tree Generation in Flex Actionscript

I am using the URL logic for creating a dynamic Tree in Flex using action script. However the output is not properly shown (Object name is shown instead of Label).
Code is available in above mentioned URL.
Please help.
Write a correct toString() implementation of your DataTreeNode, so it would have a proper display in this tree.
An example: Provided the class DataTreeNode has a data:Object field, and this object has a urlToDisplay:String property that you want displayed. Do like this:
override public function toString():String {
if (!data) return '[null]';
return data.urlToDisplay;
}
If you only rely on simple Objects or data classes, you can use the tree's labelField or labelFunction in order to read and/or format data, which is passed to the renderer. There are no new item renderer classes needed.
New renderer should be compatible with these functions!
On a site note: item renderer are not "mostly just simple MXML classes", they are component instances. It doesn't matter how there are implemented. There are best practises like avoiding data bindings in item renderers, that's why it is common to use the markup for drawing, but implementing the views behaviour according to the Flex component live cycle. you might want to read about it in the documentation, because it is a necessary read for a Flex developer.
You will have to write an ItemRenderer that tells your tree how it should display the components. ItemRenderers are mostly just simple MXML classes that access one item each and display the data in any way you want. You will have to assign the ItemRenderer to your component.
See this article:
http://help.adobe.com/en_US/flex/using/WS03d33b8076db57b9-23c04461124bbeca597-8000.html

Arbitrary dynamic properties in ActionScript 3

In JavaScript I can take anyOldObject and add anyOldObject["anyOldProperty"]="Test". I could do this in ActionScript 2 as well. Can I do it in ActionScript 3?
For example, I'm creating SoundChannel instances and I want to, say, assign IDs to them (e.g. soundChannel["id"]="123") or assign states. Will this still work, or will I have to create a separate associative array for each property (or create some new enclosing SoundChannelHolder class)?
You can do it on every class created as dynamic.
MovieClip is one of them.