4 Replies Latest reply on Jun 27, 2011 3:40 AM by lightmar

    Pass variable to AS file through MXMLC compiler


      Hey all,


      I am trying to do something for which I haven't found answer anywhere.


      I want to dynamically compile SWF file on the server, and whould like to pass a parameter through PHP in order to customize the SWF file.


      Currently I have a PHP file with a constant variable, and it works. Here it is:


      exec('mxmlc compile.as -output compile.swf');


      The AS file contains:


      public function sa()


                  var loader:Loader=  new Loader()
                  loader.load(new URLRequest('player.swf'+'?'+'movieId='+"317") );


      I want to pass a variable to the AS file, in order to customize the currently hardcoded "317".


      Something like this: 'mxmlc compile.as -output compile.swf -variable ID=317'


      Does anyone knows how to do that? I will be happy to hear!



      Or M

        • 1. Re: Pass variable to AS file through MXMLC compiler
          UbuntuPenguin Level 4

          If you are compiling it dynamically I assume that you have access to your source code.  In that case, you can copy the source code to a temp directory and replace the desired text with the incoming data.  The steps would be something like this.


          1.  Copy source code from main code to a temp directory

          2.  Find file //temp//myproject//src//main//services//SuperService.as

          3.  Read file into string

          4.  Replace a special pattern that you have in the main

               project file with your input.  In this case the special pattern

               is "Killer Clowns" and we want to replace it with "42"

               the input variable.

               fileString.replace( "Killer Clowns" , "42" );

          5.  Write string out to the temp file SuperService.as

          6. Compile.

          • 2. Re: Pass variable to AS file through MXMLC compiler
            lightmar Level 1

            Thank you for your answer, but I don't have any idea how to write the AS file on runtime through PHP.


            Do you have a full example?

            • 3. Re: Pass variable to AS file through MXMLC compiler
              bre_an Level 1

              well, you can write an .as-file using PHP, something like:

              $myFile = "Data.as"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "package {\n";
              $stringData += "    public class Data {\n";
              $stringData += "        public static const val=" + 135; $stringData += "    }"; $stringData += "}"; fwrite($fh, $stringData); fclose($fh);


              so you can use Data.val in your file, but that is of course a quite ugly solution... also mxmlc needs a lot of system resources


              so what about using FlashVars? with the FlashVars-parameter you can pass values to the embedded, compiled swf. See http://kb2.adobe.com/cps/164/tn_16417.html

              • 4. Re: Pass variable to AS file through MXMLC compiler
                lightmar Level 1

                Thank you, you have pointed me to the right direction and I extended it.


                This whole solution is something very unique which I haven't seen anywhere online.

                What it does it takes a query string parameter (can also be POST instead of GET if it is not from quesry string), inserts it to an AS file (by replacing a string in the raw AS file), and compiles the customized AS file with MXMLC.


                So if someone else wants to do something similar, here is the PHP code I use:




                $bnrId = $_GET['bnrId'];
                $file_name= "compile" . $bnrId . ".as";
                $compileName = "compile" . $bnrId . ".swf";
                $refFile = "compile.as";


                     $handle = fopen($refFile, 'r+');


                     die("couldn't open file <i>$file_name</i>");


                    $data = fread($handle, filesize($refFile));


                    if(preg_match("[X]", $data))
                         $newStr = str_replace("class compile", "class compile" . $bnrId, $data);
                        $newStr = str_replace("[X]", $bnrId, $newStr);
                        $newStr = str_replace("function compile", "function compile" . $bnrId, $newStr);
                        $newStr = $data;
                    $new = fopen($file_name, 'x+');


                    if (fwrite_stream($new, $newStr))


                        exec("mxmlc " . $file_name . " -output " . $compileName);
                        echo "success writing to file: " . $compileName;


                        echo "error writing stream";
                     echo "file <i>$file_name</i> doesn't exists";


                function fwrite_stream($fp, $string) {
                    for ($written = 0; $written < strlen($string); $written += $fwrite) {
                        $fwrite = fwrite($fp, substr($string, $written));
                        if ($fwrite === false) {
                            return $written;
                    return $written;