0
votes

I have a mxml application as below . How do I change text of label from actionscript file ?

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="initApp()">
    <fx:Script>
        <![CDATA[
            public function initApp(){
                var p = new my_player("a");
            }
        ]]>
    </fx:Script>
    <s:Label x="700" y="409" text="Label" id="lble" width="131" height="41"/>
</s:Application>

my_player.as code

package 
{
    import spark.components.Label;
    public class my_player
    {
        public var lble:Label;
        public function my_player(a:String)
        {
            lble.text="hello";
        }
    }
}
2
This code breaks the basic principle of OOP - encapsulation. Or that I do not understand the problem. - Ilya Zaytsev
@IlyaZ how come it breaks the encapsulation principles? - sybear
You have two Application instances there, which doesn't make much sense. If it is separating ActionScript from MXML your after, you should do some research on the Flex 4 (Spark) skinning architecture. - RIAstar
Please take a look at livedocs.adobe.com/flex/3/html/help.html?content=usingas_6.html before you continue with this mxml and as3 separation. - bug-a-lot
@Jari p object (first class) want link to label and change label in main application (second class) -> generation of new relations that make difficult the modification code in the future, it is bad practice. - Ilya Zaytsev

2 Answers

0
votes

Inject label from main app to convey_player with constructor (another way - binding)

public function initApp(){
    var p = new my_player("a", lble);
}

public function my_player (a:String, targetLabel:Label)
{
    lble = targetLabel;
    lble.text="hello";
}
0
votes

You do not need the second class :(

<?xml version="1.0" encoding="utf-8"?>
<s:Application 
    xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx" 
    creationComplete="initApp()">
    <fx:Script>
        <![CDATA[
        public function initApp(){

            // access your components by id
            lble.text  = "My new text for the label"
        }
        ]]>
    </fx:Script>
    <s:Label x="700" y="409" text="Label" id="lble" width="131" height="41"/>
</s:Application>