Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I normally write the code in a <code>CDATA</code> block inside the <code>&lt;mx:script/&gt;</code> tag itself. </p> <pre><code>&lt;mx:Script source="code.as"&gt; &lt;![CDATA[ //code here ]]&gt; &lt;/mx:Script&gt; </code></pre> <p>This is the way it is done in most of the tutorials and Adobe code samples out there. An mxml file represents an ActionScript <code>class</code> (the file design.mxml creates a class named <code>design</code> in the root package) and hence I believe it makes sense to put the whole code in one place (file) - this is a matter of choice though. </p> <p>If user interface parts in a component are minimal, you can create that component using just an AS file. Checkout the code for a panel with a text input and a button in it:</p> <pre><code>//MyPanel.as inside /your/folder/structure package your.folder.structure { //import appropriate classes here public class MyPanel extends Panel { [Bindable] public var txt:TextInput; [Bindable] public var btn:Button; public function MyPanel(){} override protected function createChildren():void { txt = new TextInput(); txt.text = "Default text"; txt.setStyle("color", 0xffff00); addChild(txt); btn = new Button(); btn.label = "Click me!" btn.addEventListener(MouseEvent.CLICK, handleClick); addChild(btn); } private function handleClick(e:MouseEvent):void { trace("clicked"); } } } </code></pre> <p>And the (almost) equivalent mxml code would be:</p> <pre><code>&lt;!-- MyPanel.mxml inside /your/folder/structure --&gt; &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="your.folder.structure.*"&gt; &lt;mx:TextInput id="txt" text="Default text" color="#ffff00"/&gt; &lt;mx:Button id="btn" label="Click me!" click="handleClick(event);"/&gt; &lt;mx:Script source="code.as"&gt; &lt;![CDATA[ private function handleClick(e:MouseEvent):void { trace("clicked"); } ]]&gt; &lt;/mx:Script&gt; &lt;/mx:Panel&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload