Flex is the Flash SDK from Adobe, you can download a open source command line compiler from adobe (Flex open source page ). The Free SDK link is here.
Once you have downloaded the free SDK, you will need to place into the system. I have placed mine into /opt directory called flex_sdk_3, so something like (you will need to be root to do this!)
cd /opt
mkdir flex_sdk_3
cd flex_sdk_3
unzip <place where your free sdk is) |
cd /opt
mkdir flex_sdk_3
cd flex_sdk_3
unzip <place where your free sdk is)
and then create a place for where you are going to be creating your flex scripts, I placed mine into my home directory under my programming/flex sturcture, so this is where I am
mkdir -p ~/Programming/flex/ |
mkdir -p ~/Programming/flex/
The -p will create any directories that are not present there already, and also the ~ is for the home directory that user.
I then copied the flex-config.xml from the /opt/flex_sdk_3/frameworks directory into the Programming/flex directory and edited it to the new values.
The full differences between the standard flex-config.xml and the one that I have altered is below, but basically I have added into the xml elements values the new directory for where flex-sdk is (e.g. /opt/flex_sdk_3/) so that flex’s command line environment will know where to get the libraries to compile and create the swf (ShockWave Flash), but here are the elements that I have altered.
<external-library-path>
<path-element>/opt/flex_sdk_3/frameworks
<library-path>
all the <path-element's>/opt/flex_sdk_3/frameworks
<namespaces>
the <manifest>/opt/flex_sdk_3/frameworks path to the framework directory file. |
<external-library-path>
<path-element>/opt/flex_sdk_3/frameworks
<library-path>
all the <path-element's>/opt/flex_sdk_3/frameworks
<namespaces>
the <manifest>/opt/flex_sdk_3/frameworks path to the framework directory file.
Because the class structure follows the directory structure of the directories below the compiling environment, I created the following directory structure com/codingfriends/helloworld with
mkdir -p com/codingfriends/helloworld |
mkdir -p com/codingfriends/helloworld
which will create the whole structure if there is no directories there already.
Then you can place these two files into the helloworld directory
This is the class Greeter that is created and turned into a object within the swf file save this as Greeter.as
// the directory structure of where the class is.
package com.codingfriends.helloworld
{
public class Greeter
{
private var name:String;
// constructor
public function Greeter(initialName:String="Ian")
{
name = initialName;
}
// sayHello returns a String
public function sayHello():String
{
var result:String;
// if the class private name is present
if (name!=null && name.length >0)
{
result = "Hello there, "+name+".";
}
// should never reach here on a basic run.
else
{
result="Hello there.";
}
return result;
}
}
} |
// the directory structure of where the class is.
package com.codingfriends.helloworld
{
public class Greeter
{
private var name:String;
// constructor
public function Greeter(initialName:String="Ian")
{
name = initialName;
}
// sayHello returns a String
public function sayHello():String
{
var result:String;
// if the class private name is present
if (name!=null && name.length >0)
{
result = "Hello there, "+name+".";
}
// should never reach here on a basic run.
else
{
result="Hello there.";
}
return result;
}
}
}
var = variable and the type of variable or return variable is at the end of the definition e.g.
public var name:String; // this is a variable named "name" and it is of type String
public function sayHello():String // this is a function named "sayHello" and returns a String type |
public var name:String; // this is a variable named "name" and it is of type String
public function sayHello():String // this is a function named "sayHello" and returns a String type
save this as Greeter_mx.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="initApp();">
<mx:Script>
<![CDATA[
import com.codingfriends.helloworld.Greeter;
// the initialize application .. the start
private function initApp():void
{
// create a new Greeter object with no passing in name
var myGreeter:Greeter = new Greeter();
// and say hello world
output.text+=myGreeter.sayHello();
// create a new greeter with the name = genux instead
var newGreeter:Greeter = new Greeter("genux");
output.text+=newGreeter.sayHello();
}
]]>
</mx:Script>
<mx:Text id="output" width="100%" textAlign="center"/>
</mx:Application> |
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="initApp();">
<mx:Script>
<![CDATA[
import com.codingfriends.helloworld.Greeter;
// the initialize application .. the start
private function initApp():void
{
// create a new Greeter object with no passing in name
var myGreeter:Greeter = new Greeter();
// and say hello world
output.text+=myGreeter.sayHello();
// create a new greeter with the name = genux instead
var newGreeter:Greeter = new Greeter("genux");
output.text+=newGreeter.sayHello();
}
]]>
</mx:Script>
<mx:Text id="output" width="100%" textAlign="center"/>
</mx:Application>
The is the place where the code starts, the rest is just the adobe application settings, within the code there is a reference to “output”, which is defined in the PATH=$PATH:/opt/flex_sdk_3/bin |
PATH=$PATH:/opt/flex_sdk_3/bin
the -load-config will load the one in the present directory which I altered, and -source-path is there so that the base directory is used for compiling so the compiler will know where to get the com/codingfriends/helloworld/Greeter.as and Greeter_mx.mxml, and the -output is what the output flash file is/placed.
Here is the output of the Greeter.swf file.
Here is the difference (diff) between the flex-config.xml from the one that I edited and the one that was in the /opt/flex_sdk_3/frameworks directory.