1

I need to create a video player using OSMF . I want to seperate our mxml file from actionscript files . How can I do that ? I have a actionscript class file and I want to execute its constructor when mxml is loaded .

I have added creationComplete="initApp()" and on initApp I call var p = new myclass(); . Now in myclass() I am trying to add label programmatically

my_player.mxml

<?xml version="1.0" encoding="utf-8"?>
<!-- controls\videoplayer\VideoPlayerSimple.mxml-->
<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:Application>

my_player.as

   package 
{
    import mx.controls.Label;
    import mx.core.Application;
    import mx.events.FlexEvent;
    import spark.components.Application;
    public class my_player extends spark.components.Application
    {
        public function convey_player(a:String){
            var label:Label = new Label();
            label.text = "Testxxx";
            addElement(label);
                    Alert.show("Hello");

        }
    }
}

But nothing is added to flash . Am I missing something ?

Pit Digger
  • 9,618
  • 23
  • 78
  • 122

1 Answers1

2

A constructor will only run when an instance of an object js created. If you want to run constructor code you will have to create an instance of it. In your MXML file add an event listener for the creationComplete event and create an instance of your ActionScript object there.

this will effectively execute the ActionScript class constructor code after the MXML code is finished its creation process as part of it's component lifecycle.

JeffryHouser
  • 39,401
  • 4
  • 38
  • 59
  • I have edited as per your guide and added changes in question can you please check ? thanks ! – Pit Digger Feb 26 '13 at 20:31
  • I see Alert Hello but label is not added in flash . – Pit Digger Feb 26 '13 at 21:23
  • 2
    You don't usually have two applications in a Flex app. I suspect that may cause issues. The label you add will not be displayed because you add it to a component that is never added to the displaylist. In your initApp() you need an addElement(p). You'll benefit from reading up on the Flex Component Lifecycle. http://help.adobe.com/en_US/flex/using/WS460ee381960520ad-2811830c121e9107ecb-7fff.html components should be created in the createchildren() method; not in a constructor. – JeffryHouser Feb 26 '13 at 21:32