<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Swedish fika &#187; ActionScript 3.0</title>
	<atom:link href="http://www.swedishfika.com/category/actionscript-30/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.swedishfika.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Sun, 25 Jul 2010 21:57:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Custom Cursor in AS3</title>
		<link>http://www.swedishfika.com/2009/08/29/custom-cursor-in-as3/</link>
		<comments>http://www.swedishfika.com/2009/08/29/custom-cursor-in-as3/#comments</comments>
		<pubDate>Sat, 29 Aug 2009 18:46:07 +0000</pubDate>
		<dc:creator>Johan</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>

		<guid isPermaLink="false">http://swedishfika.com/?p=560</guid>
		<description><![CDATA[Creating a custom mouse pointer in AS3 seems like a basic task, and it is, kind of. There are some pitfalls that developers seem to fall into more often than one would think. In this post I’m going to walk through the whole process of making a custom cursor.

This is the end result:
Tools used in [...]]]></description>
			<content:encoded><![CDATA[<p>Creating a custom mouse pointer in AS3 seems like a basic task, and it is, kind of. There are some pitfalls that developers seem to fall into more often than one would think. In this post I’m going to walk through the whole process of making a custom cursor.</p>
<p><span id="more-560"></span></p>
<p>This is the end result:<br />

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_0" width="446" height="170">
      <param name="movie" value="http://swedishfika.com/wp-content/uploads/custom-cursor-final.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://swedishfika.com/wp-content/uploads/custom-cursor-final.swf" width="446" height="170">
      <!--<![endif]-->
        <p>The Flash plugin is required to view this object.</p>
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
</p>
<h3>Tools used in this post</h3>
<ul>
<li>Flash CS3</li>
<li>Illustrator</li>
<li>Eclipse</li>
</ul>
<p><em>These tools are all optional and not needed to follow the post</em></p>
<h3>The pointer</h3>
<p>First we need graphics for the pointer. If you import graphics from Illustrator there are a couple of things that are good to know.</p>
<ul>
<li>Changing all the lines to fills will prevent the image to look funky when resizing it. (Object, Path, Outline Stroke in Illustrator)</li>
<li>If you&#8217;ve got text in the graphics you&#8217;ll want to outline that. Right click (ctrl click on the mac) and choose &#8220;Create Outlines&#8221;.</li>
<li>If you import the graphics instead of just dragging it in to flash you&#8217;ll get a &#8220;incompatibility report&#8221; if something is wrong with the illustration.(File, Import, Import to Library&#8230;).</li>
</ul>
<p>When we have our graphics, link it to a class! While we&#8217;re at it, let&#8217;s create a document class for the main time line as well. Now we don&#8217;t have to use the flash code editor at all and can use our favourite IDE instead (Eclipse in my case).</p>
<h3>Let&#8217;s code</h3>
<p>In our document class we first have to instantiate the cursor class and add it to the scene.<br />
<code>// in a good IDE, these two lines add themselves<br />
import flash.display.Sprite;<br />
import src.Cursor;<br />
// our document class pedagogically named "DocumentClass" <img src='http://www.swedishfika.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /><br />
public class DocumentClass extends Sprite<br />
{<br />
	// Declarations go here<br />
	public var cursor:Cursor;<br />
	// And Constructor here<br />
	public function DocumentClass():void<br />
	{<br />
		// instantiate the cursor<br />
		// and add it to the scene<br />
		cursor = new Cursor();<br />
		addChild(cursor);<br />
	}<br />
}</code><br />
This is how it looks if we run the application now.<br />

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_1" width="446" height="170">
      <param name="movie" value="http://swedishfika.com/wp-content/uploads/custom-cursor-step1.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://swedishfika.com/wp-content/uploads/custom-cursor-step1.swf" width="446" height="170">
      <!--<![endif]-->
        &lt;p&gt;Custom cursor flash. First compile&lt;/p&gt;
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
 </p>
<p>As seen above the cursor graphics is visible at top left even though we do not need it. Right now it is just littering the scene. Let&#8217;s make it go away.<br />
<code>cursor.visible = false;</code><br />
And let&#8217;s hide the mouse cursor as well.<br />
<code>// We need this import<br />
import flash.ui.Mouse;<br />
// then hide it like this.<br />
Mouse.hide();</code></p>
<p>As soon as the mouse enter the scene we want the cursor to be visible again. And when it leaves we have to make invisible. Otherwise it will just lie and clutter the scene at its last registration point. So, two new events:<br />
<code>// needed imports<br />
import flash.events.Event;<br />
import flash.events.MouseEvent;<br />
// The events<br />
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);<br />
stage.addEventListener(Event.MOUSE_LEAVE, mouseLeave);<br />
// The first function<br />
private function mouseMove(e:MouseEvent):void<br />
{<br />
	// Since there is no MOUSE_ENTER event<br />
	// we'll have to make the cursor visible here,<br />
	// hence the if statement.<br />
	if (!cursor.visible) cursor.visible = true;<br />
	// And pin it to the mouse position.<br />
	cursor.x = e.stageX;<br />
	cursor.y = e.stageY;<br />
}<br />
// second function<br />
private function mouseLeave(e:Event):void<br />
{<br />
	// The anti littering function<br />
	// Besides making our cursor invisible<br />
	// we may also want to remove eventlisteners<br />
	// and stuff we don't need to free up resourcers<br />
	// for the user, just remember to turn them on<br />
	// again in the mouseMove function.<br />
	cursor.visible = false;<br />
}</code><br />
We&#8217;re almost done.<br />

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_2" width="446" height="170">
      <param name="movie" value="http://swedishfika.com/wp-content/uploads/custom-cursor-step2.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://swedishfika.com/wp-content/uploads/custom-cursor-step2.swf" width="446" height="170">
      <!--<![endif]-->
        &lt;p&gt;Custom cursor. second compile.&lt;/p&gt;
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
 </p>
<h3>Final touches</h3>
<p>As new stuff gets added to the scene we don&#8217;t want our nice cursor to get concealed by it. To prevent that from happening we can add these lines.<br />
<code>// check if there are more objects on the scene<br />
if(numChildren > 1)<br />
{<br />
	// set the cursor index so the cursor has the<br />
	// highest index the other objects on the<br />
	// scene will get new indexes automatically<br />
	setChildIndex(cursor, (numChildren - 1));<br />
}</code></p>
<p>depending on the project we need the cursor in, we may need to turn these lines into a little function and call it from more places in the code. Ie, the click event, the enter frame event or whenever a new object gets added, because right now the index will only be updated when you move the cursor.<br />

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_3" width="446" height="170">
      <param name="movie" value="http://swedishfika.com/wp-content/uploads/custom-cursor-step2.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://swedishfika.com/wp-content/uploads/custom-cursor-step2.swf" width="446" height="170">
      <!--<![endif]-->
        &lt;p&gt;Custom cursor. second compile.&lt;/p&gt;
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
 </p>
<p>If we move the cursor around the scene now we&#8217;ll notice some stuttering. To get rid of this we need to call the updateAfterEvent(). This will make flash rerender as soon as the event completes. So, at the bottom of our mouseMove function we add this line.<br />
<code>e.updateAfterEvent.updateAfterEvent();</code><br />
This is the final result.<br />

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_4" width="446" height="170">
      <param name="movie" value="http://swedishfika.com/wp-content/uploads/custom-cursor-final.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://swedishfika.com/wp-content/uploads/custom-cursor-final.swf" width="446" height="170">
      <!--<![endif]-->
        <p>The Flash plugin is required to view this object.</p>
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
 </p>
<h3>Conclusion</h3>
<p>There really is no perfect way to do this. If you have an application that is heavy on the cpu, the cursor can get laggy even though you did everything by the book. Maybe one day Adobe will improve the mouse ui but until then, make sure to remove the cursor on mouse out, use the updateAfterEvent() and set the right index of the cursor when needed.</p>
<p>If you want you can <a href='http://swedishfika.com/wp-content/uploads/Custom-cursor.zip'>download this little project</a>.</p>
<p>/ Johan</p>
]]></content:encoded>
			<wfw:commentRss>http://www.swedishfika.com/2009/08/29/custom-cursor-in-as3/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Use your own ActionScript 3.0 classes and custom events in Adobe Flash (Part 2)</title>
		<link>http://www.swedishfika.com/2009/07/17/use-your-own-actionscript-30-classes-and-custom-events-in-adobe-flash-part-2/</link>
		<comments>http://www.swedishfika.com/2009/07/17/use-your-own-actionscript-30-classes-and-custom-events-in-adobe-flash-part-2/#comments</comments>
		<pubDate>Fri, 17 Jul 2009 22:18:57 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>

		<guid isPermaLink="false">http://swedishfika.com/?p=450</guid>
		<description><![CDATA[This is the last part in this short serie of two where we use real world examples to show you how you can create and use your own custom event classes to write customizable and reusable code that you can use in your ActionScript 3.0 projects from time to time.
In this second part we&#8217;ll take [...]]]></description>
			<content:encoded><![CDATA[<p>This is the last part in this short serie of two where we use real world examples to show you how you can create and use your own custom event classes to write customizable and reusable code that you can use in your ActionScript 3.0 projects from time to time.</p>
<p>In this second part we&#8217;ll take part one a step further and write classes to load images using the XML-file we loaded in part one. </p>
<p><span id="more-450"></span></p>
<p>If you don&#8217;t remember anything about part one or never read it, you can find it <a href="http://swedishfika.com/2009/05/02/use-your-own-actionscript-30-classes-and-custom-events-in-adobe-flash-part-1/">here</a>.</p>
<p>So what we&#8217;re going to do is to take the contents of the XML-file we loaded in part one and extract each of the image-url:s in that file and use them to load the corresponding images. To do this we just need to create two classes and update our document class a bit. The first class that we&#8217;ll create will be called ImageLoader and the other  will be called ImageLoaderEvent. The ImageLoader-class will be used to actually load the images and the ImageLoaderEvent-class will be used to handle the events thats used when loading the images. </p>
<h3>Create the ImageLoader-class</h3>
<p>In Adobe Flash, choose File->New->ActionScript file and add the following code to the file that you save as ImageLoader.as in the com-folder.<br />
<code>package com<br />
{<br />
  import flash.display.Sprite;<br />
  import flash.events.Event;<br />
  import flash.events.IOErrorEvent;<br />
  import flash.events.ProgressEvent;<br />
  import flash.display.Loader;<br />
  import flash.net.URLRequest;<br />
  import flash.display.Bitmap;<br />
  public class ImageLoader extends Sprite<br />
  {<br />
    private var index:int;<br />
    public function ImageLoader(url:String, index:int)<br />
    {<br />
      // This value will be used to know which image<br />
     // in the xml-file that's actually been loaded.<br />
      this.index = index;<br />
      // Load the image<br />
      var loader:Loader = new Loader();<br />
      loader.load(new URLRequest(url));<br />
      // Complete listener<br />
      loader.contentLoaderInfo.addEventListener(Event.COMPLETE,<br />
        onComplete);<br />
    }<br />
    public function onComplete(e:Event):void<br />
    {<br />
      // Dispatch the complete event when the image<br />
      // has been loaded<br />
      this.dispatchEvent(new ImageLoaderEvent(<br />
        ImageLoaderEvent.COMPLETE, false,<br />
        false, e.target.content, this.index));<br />
    }<br />
  }<br />
}</code></p>
<h3>Create the ImageLoaderEvent-class</h3>
<p>Now we need to create our custom event class that we&#8217;ll use to handle the events that we use when we load the images.</p>
<p>In Adobe Flash, choose File->New->ActionScript file and add the following code to the file that you save as ImageLoaderEvent.as in the com-folder.<br />
<code>package com<br />
{<br />
  import flash.events.Event;<br />
  import flash.display.Bitmap;<br />
  public class ImageLoaderEvent extends Event<br />
  {<br />
    public static const COMPLETE:String = "complete";<br />
    public var image:Bitmap;<br />
    public var index:int;<br />
    public function ImageLoaderEvent(type:String,<br />
      bubbles:Boolean = false, cancelable:Boolean = false,<br />
      image:Bitmap = null, index:int = -1)<br />
    {<br />
      super(type, bubbles, cancelable);<br />
      this.image = image;<br />
      this.index = index;<br />
     }<br />
  }<br />
}</code></p>
<h3>Last step is to update our document class</h3>
<p>So now we have the classes we need to be able to load the images. As you can see we&#8217;ve added an index-value that the ImageLoader-class takes as an argument and that it&#8217;s passed to the event-class when the image has been loaded. We&#8217;ll explain what this value will be used for in just a moment. First off we need to update our document class (Main.as) so it looks like this.<br />
<code>package<br />
{<br />
  import flash.display.MovieClip;<br />
  import com.XMLLoader;<br />
  import com.XMLLoaderEvent;<br />
  import com.ImageLoader;<br />
  import com.ImageLoaderEvent;<br />
  public class Main extends MovieClip<br />
  {<br />
    public function Main()<br />
    {<br />
      var url:String = "xml/images.xml";<br />
      var xl:XMLLoader = new XMLLoader(url);<br />
      xl.addEventListener(XMLLoaderEvent.COMPLETE,<br />
       onXMLLoaded);<br />
    }<br />
    public function onXMLLoaded(e:XMLLoaderEvent):void<br />
    {<br />
      var imageXML:XML = e.xml;<br />
      // Get a list of all the image-nodes<br />
      var images:XMLList = imageXML..image;<br />
      var numImages:int = images.length();<br />
      var imageLoader:ImageLoader;<br />
      // Loop through each image and load it<br />
      for(var i:int = 0; i < numImages; i++)<br />
      {<br />
        // Get the url from the current image-node<br />
        // in our XMLList<br />
        var urlStr:String = images[i].url;<br />
        imageLoader = new ImageLoader(urlStr, i);<br />
        imageLoader.addEventListener(ImageLoaderEvent.COMPLETE,<br />
          onImageLoaded);<br />
      }<br />
    }<br />
    public function onImageLoaded(e:ImageLoaderEvent):void<br />
    {<br />
      // The image<br />
      trace(e.image);<br />
      // With this value we know which image<br />
      // in the XML-list that's actually been loaded<br />
      trace(e.index);<br />
    }<br />
  }<br />
}</code><br />
As we've said we've added the index-value. This index-value is just there so we can know which one of the images in the XML-file that's been loaded. It's just a nice feature that can come in handy if you need to know this. You might have some information to your image that's listed in the image-node and if you have this index-value you can easily extract that information from the XML-file and present it along with the correct image. </p>
<p>Before you run your code you need to create a folder in your project folder (CustomEvents) that you call images. In this folder you need to add three jpg-images called image1.jpg, image2.jpg and image3.jpg. </p>
<p>The output from executing the code should look something like this:<br />
<img src="http://swedishfika.com/wp-content/uploads/custom_events_2_output.jpg" alt="The output from the code" /></p>
<p>So what's just happened? What we now do in our document class is that we load the XML. When the XML has been loaded we load each image that's listed in that file and when each image has been loaded we just trace the contents of what's been loaded. We could just as easily have added theese images to be displayed on the stage but for the simplicity of this article we've just traced the contents.</p>
<p>When you get the hang of using your own custom event classes you can get more in control of your code and write larger and more powerful applications. You'll also get a collection of classes that you can reuse in many projects and this will most probably save you precious development-time.</p>
<p>That's it, if you have any ideas about how you could improve this code please write a comment!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.swedishfika.com/2009/07/17/use-your-own-actionscript-30-classes-and-custom-events-in-adobe-flash-part-2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Use your own ActionScript 3.0 classes and custom events in Adobe Flash (Part 1)</title>
		<link>http://www.swedishfika.com/2009/05/02/use-your-own-actionscript-30-classes-and-custom-events-in-adobe-flash-part-1/</link>
		<comments>http://www.swedishfika.com/2009/05/02/use-your-own-actionscript-30-classes-and-custom-events-in-adobe-flash-part-1/#comments</comments>
		<pubDate>Sat, 02 May 2009 12:37:20 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>

		<guid isPermaLink="false">http://swedishfika.com/?p=295</guid>
		<description><![CDATA[If you get the hang of using your own custom events in ActionScript 3.0 you can streamline your development and get more control of your code. This is the first part in a serie of two where we&#8217;re going to show you how you can create and use your own custom events. 
In this first [...]]]></description>
			<content:encoded><![CDATA[<p>If you get the hang of using your own custom events in ActionScript 3.0 you can streamline your development and get more control of your code. This is the first part in a serie of two where we&#8217;re going to show you how you can create and use your own custom events. </p>
<p>In this first part we&#8217;re going to use a real-world example and use a custom event-class to load <a href="http://www.w3schools.com/xml/default.asp">XML</a> more smoothly. We&#8217;ll also go through how you create a document class and how you link it to your flash document. </p>
<p><span id="more-295"></span></p>
<h3>1. Everything starts somewhere</h3>
<p>First off, create a folder on your desktop that you call CustomEvents where you can put all the files corresponding to this tutorial. This will be our project directory.</p>
<h3>2. Create the flash document:</h3>
<p>Open up Adobe Flash (in this case we use Adobe Flash CS4 but the approach is similar if you use another version such as Adobe Flash CS3), choose File->New->Flash File (ActionScript 3.0) and save it as Main.fla in your current project directory.</p>
<h3>3. Create the XML-file:</h3>
<p>Create a folder in your project directory that you call xml. Add the following code to a file called images.xml and save it in the xml-folder in your project directory. This is the file that we are going to load with the help of our custom event class.<br />
<code>&lt;?xml version="1.0" encoding="utf-8"?&gt;<br />
&lt;images&gt;<br />
  &lt;image&gt;<br />
    &lt;url&gt;images/image1.jpg&lt;/url&gt;<br />
  &lt;/image&gt;<br />
  &lt;image&gt;<br />
    &lt;url&gt;images/image2.jpg&lt;/url&gt;<br />
  &lt;/image&gt;<br />
  &lt;image&gt;<br />
    &lt;url&gt;images/image3.jpg&lt;/url&gt;<br />
  &lt;/image&gt;<br />
&lt;/images&gt;</code><br />
This file is pretty straight-forward. It contains three image-nodes that each of them contain a url-node that specifies the location of each image.</p>
<h3>4. Create the custom event class</h3>
<p>To be able to streamline and make it easy to use our ActionScript-classes it&#8217;s nice (so far it&#8217;s possible) to make general classes that you can reuse from project to project. So create a new folder on your desktop that you call AS3Classes and create another folder in that directory that you call com. We&#8217;ll get back to how you customize your flash document so it can use the classes you put in this folder. But first off, let&#8217;s create the classes. </p>
<p>In Adobe Flash, choose File->New->ActionScript file and add the following code to the file that you save as XMLoaderEvent.as in the com-folder.<br />
<code>package com<br />
{<br />
  import flash.events.Event;<br />
  public class XMLLoaderEvent extends Event<br />
  {<br />
    public var xml:XML;<br />
    public static const COMPLETE:String = "complete";<br />
    public function XMLLoaderEvent(type:String,<br />
      bubbles:Boolean = false, cancelable:Boolean = false,<br />
      xml:XML = null)<br />
    {<br />
      super(type, bubbles, cancelable);<br />
      this.xml = xml;<br />
    }<br />
  }<br />
}</code><br />
You don&#8217;t need to create your own event class to be able to listen to all the events you can listen to when you load files but it&#8217;s a nice thing to do because you get more control of your code. As you can see the event class doesn&#8217;t actually load a file, it just has a few properties that you can use when you load a file. We&#8217;ll create  a specific class later on that actually do all the loading but we&#8217;ll get back to that soon.</p>
<h3>5. Create a document class</h3>
<p>You can start your code by adding ActionScript directly on the first frame in your main timeline but since we write all our code in classes it&#8217;s nice to keep on doing this and that&#8217;s why we now need to create our document class. </p>
<p>Choose File->New->ActionScript file, add the following code to it and save it as Main.as in your current project directory.<br />
<code>package<br />
{<br />
  import flash.display.MovieClip;<br />
  public class Main extends MovieClip<br />
  {<br />
    public function Main()<br />
    {<br />
      // This is the constructor of our document class. This<br />
      //is where our code will start.<br />
      trace("Hello world!");<br />
    }<br />
  }<br />
}</code><br />
Now we need to tell our flash document that we want this class to be the document class. Choose File->Publish settings->Flash (the tab)->Settings. At the top of that popup-window you can tell your flash document the name of your document class that in this case is Main. So just write Main in this input and save all your changes.</p>
<p>If you now press CTRL+Enter you will publish your flash document and if you don&#8217;t get any error and in the output-window can read the text (&#8220;Hello world!&#8221;) that we trace in the constructor in our document class everything is set up correctly.</p>
<h3>6. Create our XMLLoader class</h3>
<p>We want to load XML and we want to write reusable code that we might want to use on other projects so let&#8217;s create a general class that we can use each time we want to load XML.</p>
<p>Choose File->New->ActionScript file, add the following code to the class and save it as XMLLoader.as in your com-folder:<br />
<code>package com<br />
{<br />
  import flash.display.Sprite;<br />
  import flash.events.Event;<br />
  import flash.events.IOErrorEvent;<br />
  import flash.events.ProgressEvent;<br />
  import flash.net.URLLoader;<br />
  import flash.net.URLRequest;<br />
  public class XMLLoader extends Sprite<br />
  {<br />
    public function XMLLoader(url:String)<br />
    {<br />
      // Load the XML<br />
      var urlR:URLRequest = new URLRequest(url);<br />
      var l:URLLoader = new URLLoader(urlR);<br />
      // Add listeners<br />
      l.addEventListener(Event.COMPLETE, onComplete);<br />
    }<br />
    public function onComplete(e:Event):void<br />
    {<br />
      // Dispatch the complete event when the XML is loaded<br />
      this.dispatchEvent(new XMLLoaderEvent(<br />
       XMLLoaderEvent.COMPLETE, false, false,<br />
       XML(e.target.data)));<br />
    }<br />
  }<br />
}</code></p>
<p>This class takes a url to the xml-file as argument and starts to load the XML and dispatches our own XMLLoaderEvent.COMPLETE-event when the XML has been loaded.</p>
<h3>7. Tell our flash document where it can find our custom classes that we save in our AS3Classes-folder</h3>
<p>To be able to use our classes that we put in our AS3Classes-folder we need to tell our document that this folder exists. You do that by choosing File->Publish settings->Flash (the tab)->Settings. There are a couple of new tabs and you should add the source path to your AS3Classes-folder here. Do that and save your changes.</p>
<h3>8. Wrap it all together</h3>
<p>Our next and final step is to instanciate our XMLLoader-class and trace the XML when it&#8217;s been loaded. So add some code to our document class so it&#8217;ll look like this:<br />
<code>package<br />
{<br />
  import flash.display.MovieClip;<br />
  import com.XMLLoader;<br />
  import com.XMLLoaderEvent;<br />
  public class Main extends MovieClip<br />
  {<br />
    public function Main()<br />
    {<br />
      trace("Hello world!");<br />
      var url:String = "xml/images.xml";<br />
      var xl:XMLLoader = new XMLLoader(url);<br />
      xl.addEventListener(XMLLoaderEvent.COMPLETE,<br />
       onXMLLoaded);<br />
    }<br />
    public function onXMLLoaded(e:XMLLoaderEvent):void<br />
    {<br />
     trace(e.xml);<br />
    }<br />
  }<br />
}</code></p>
<p>Now the code is ready to go so press CTRL+Enter and if everything is set up correctly you will now be able to read the contents of the XML-file in your output window.</p>
<p>This was more of an introduction to how you can write and use your custom events. In part 2 we’re going to take this code a step further and write code to load images more smoothly and more in-depth descripe how event classes work and how you can customize them.</p>
<p>/Simon</p>
]]></content:encoded>
			<wfw:commentRss>http://www.swedishfika.com/2009/05/02/use-your-own-actionscript-30-classes-and-custom-events-in-adobe-flash-part-1/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>How to listen to events from links within TextFields in ActionScript 3</title>
		<link>http://www.swedishfika.com/2008/06/27/how-to-listen-to-events-from-links-within-textfields-in-actionscript-3/</link>
		<comments>http://www.swedishfika.com/2008/06/27/how-to-listen-to-events-from-links-within-textfields-in-actionscript-3/#comments</comments>
		<pubDate>Fri, 27 Jun 2008 18:43:04 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>

		<guid isPermaLink="false">http://swedishfika.com/?p=84</guid>
		<description><![CDATA[There are many times when you want to listen to link events from TextFields in ActionScript 3. You might want to perform a certain task when the user clicks on a link and if so the new feature in ActionScript 3 that&#8217;s called &#8220;link events&#8221; come in handy. 

This class describes how you can use [...]]]></description>
			<content:encoded><![CDATA[<p>There are many times when you want to listen to link events from TextFields in ActionScript 3. You might want to perform a certain task when the user clicks on a link and if so the new feature in ActionScript 3 that&#8217;s called &#8220;link events&#8221; come in handy. </p>
<p><span id="more-84"></span></p>
<h3>This class describes how you can use link events:</h3>
<p><code>package {<br />
  import flash.display.Sprite;<br />
  import flash.text.TextField;<br />
  import flash.text.TextFormat;<br />
  import flash.events.TextEvent;<br />
  public class LinkEventTest extends Sprite<br />
  {<br />
    public function LinkEventTest()<br />
    {<br />
      // Create a TextField<br />
      var txt:TextField = new TextField();<br />
      /* Due to the width of our column on swedishfika.com<br />
      we save our link in a separate variable. */<br />
      var ourLink:String = "&lt;a href=\"event:Hello World!\"&gt;";<br />
      ourLink += "Trace \"Hello world!\"&lt;/a&gt;";<br />
      // Add the link<br />
      txt.htmlText = ourLink;<br />
      // Add the listener<br />
      txt.addEventListener(TextEvent.LINK, onClick);<br />
      // Add the textfield to the display list<br />
      addChild(txt);<br />
    }<br />
    private function onClick(e:TextEvent):void<br />
    {<br />
      /* Is triggered when the user clicks on the link<br />
      we declared above. */<br />
      // Traces "Hello World!"<br />
      trace(e.text);<br />
    }<br />
  }<br />
}</code></p>
<p>When the user clicks on the link the TextEvent.LINK event is triggered and the onClick-handler is executed and traces the string &#8220;Hello world!&#8221;. </p>
<p>This is a really neat new feature that solves a lot of problems! </p>
<p>/Simon Kjellberg</p>
]]></content:encoded>
			<wfw:commentRss>http://www.swedishfika.com/2008/06/27/how-to-listen-to-events-from-links-within-textfields-in-actionscript-3/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
