arpit.net : Technology and other shots...

Sunday, August 29, 2004

Simple Stepper Class in AS 2

Recently, we had the Delhi Flash UserGroup meet in which I took a session on "Introduction to AS2.0" . I demonstrated a very simple version of a Stepper class in AS2. It covered new features of AS 2.0 like:
  • New keywords: Class, extends, Public/Private
  • DataTyping
  • Getter / Setter functions
  • Linking a library symbol directly with a class
  • and adding custom events with the new EventDispatcher class

Click here to download the source files and the presentation.

Along with me, there were Simon Horwith from Team Macromedia, who took an excellent session on Flex, and Owas Bhat who demonstrated various ways for skinning the Flash 2004 UI components.

Here is the code for the Stepper class:


import mx.events.EventDispatcher


class Stepper extends MovieClip {
// class definitions
private var next_mc:MovieClip;
private var back_mc:MovieClip;
private var label_txt:TextField;

private var _counter:Number = 0;

// event dispatcher props
public var addEventListener:Function;
public var removeEventListener:Function;
private var dispatchEvent:Function;

// constructor function
function Stepper () {
EventDispatcher.initialize(this);
init();
}

// will be called by the constructor function
private function init() {
next_mc.onRelease = nextHandler;
back_mc.onRelease = backHandler;
label_txt.autoSize = 'center';
label_txt.text = _counter.toString()
}

private function nextHandler():Void {
_parent.index++ ;
//trace ('next clicked '+_parent._counter);
}

public function backHandler():Void {
_parent.index-- ;
//trace ('back clicked '+ _parent._counter);
}

public function set index (val:Number) {
_counter = val;
label_txt.text = _counter.toString();
//dispath event
dispatchEvent({type:"change"});
}
public function get index ():Number {
return _counter;
}
}


Monday, August 09, 2004

DataProvider in Flash 2004

I have been trying to use a Data Model in Flash 2004 and as I see, the implementation of DataProvider is different in Flash 2004 than Flash MX

The way in Flash MX was pretty straight forward:

// In Flash MX
myDP = new DataProviderClass();


This creates an array called items within the instance created.

Though this approach seems ok, similar approach does not work with Flash 2004:

// In Flash 2004
import mx.controls.listclasses.DataProvider
myDP:DataProvider = new DataProvider();


Now, I have come across a couple of ways to go about it.

  1. As described by Jesse: Using the initialize method over an Array.

    mx.controls.listclasses.DataProvider.Initialize(Array);

    This gets the job done, by adding all the methods of the DataProvider Class to the object prototype. However, what I dont like about it is, that it uses mixins.
  2. I have come across a Vector class, made by 123Webwizard, similar to the one used in Java. This creates an array called data within the object it instantiates and works similarly as the DataProviderClass in FlashMX.


Now I am really dont know which is the better approach and why.
I personally would like to use the one which can be instantiated rather than using mixin simply as its easier to read and understand.

Would any one like to comment on the pros and cons....?
Also... will both the approaches work fine if the target player is Flash Player 6?