Archived entries for Code

AS3 Sound Spectrum
Analyzer

A couple of weeks ago i wanted to try out working with sound. I done little more with sound other than loading mp3s and playing them. I wanted to make a sound visualizer…

..well I did! And id like to share it with you. Getting the actual snap shot data of the sound spectrum was the easy part. The tricky part was in fact making some cool graphics. Below you can see a screenshot of the visualizer, and here is the link to the actual visualizer.

Download source files here.

Popularity: 10% [?]

Scrollbar class for AS3

I have recently been working with a lot of display objects that needed scroll bars. Previously i have often used the scrollbar component in the Flash IDE, but i wanted a lightweight and slicker looking way to scroll through content. I made a class that is very simple to use, that i would like to share with you. This is the class in action:


You can download the source files here.

The class is very easy to use, below i will give two usage examples. One which is more of a Flash IDE approach where code is written in timeline, and the other where the object that is to be scrollable is created programatically. But first of all you need to add the Scroller class to you class path. In the Flash IDE, you can add the classes folder ( in the zip file ) to you list of classpaths in Preferences -> ActionScript -> ActionScript 3.0 Settings…



Now the code. First off, the timeline version. Lets say you have a MovieClip you need a scrollbar on, where the viewable area should be 250 pixels high, and has a red color. The content of this MovieClip can be anything, an image, graphic, text etc. We place it onto the stage where you want the scrolling MovieClip to be, and give it an instance name of “myClip” in the properties panel. Finally we write this code on the timeline:


      // Import the class.
      import com.perkstoveland.utils.Scroller;

      // Create the scrollbar
      var scroller:Scroller = new Scroller( myClip,  250, 0xFF0000 );

This will create a scrollbar and place it next to the MovieClip on the stage.



This class can used without using the timeline too, lets create a TextField that uses the Scroller all programatically. Like this:


      // Import the class.
      import com.perkstoveland.utils.Scroller;

      // Create the TextField, asume we have stored the text to
      // show in a variable "theTextToShow", which is a string
      // with a few paragraphs of text.
      var txt:TextField = new TextField();
      txt.multiline = true;
      txt.wordWrap = true;
      txt.autoSize = TextFieldAutoSize.LEFT;
      txt.width = 300;
      txt.text = theTextToShow;

      // Lastly we create the scroller, position it, and add it
      // to the display stack.
      var scroller:Scroller = new Scroller( txt, 350, 0x333333 );
      scroller.x = 20;
      scroller.y = 20;
      addChild( scroller );

Thats it, hope you use it, and like it. Enjoy!

Popularity: 20% [?]

Extending
EventDispatcher in
Flex Builder 3

Ok, so was working on an AS project in Flex Builder 3, and wanted to write a class that extended EventDispatcher. My class was going to receive an XML and dispatch an event when it had transformed the XML into an array of value objects. Since this class wasn’t going to be on the display list, i thought that extending the EventDispatcher class was a good idea.

I created a new actionScript class in Flex Builder and told it to extend the EventDispatcher. When I looked at the auto generated code in the file there, I scratched my head. This is what it looked like:


   package com.example {

      import flash.events.EventDispatcher;
      import flash.events.IEventDispatcher;

      public class Example extends EventDispatcher {

         public function Example(target:IEventDispatcher=null) {
            super(target);
         }

      }
   }

I was wondering what the target in the constructor was. After some quick research i learned that you only use the EventDispatcher constructor when you have to implement the IEventDispatcher, because you are already extending another class. Therefore i changed my class to look like this instead:


   package com.example {

      import flash.events.EventDispatcher;

      public class Example extends EventDispatcher {

         public function Example() {

         }

      }
   }

This wouldn’t have been an issue if i had just written it in TextMate, but because i used Flex Builders auto generating code, i had to investigate what was happening.

Popularity: 3% [?]