I was in the process of completing my DropDownMenu class and I needed a way to save the information of the selected item in the list and the best thing would be to use the getter functions. In my later posts you'll see the dropDownMenu but for right now to give you an idea, we would need the following details to be accessible when an item is selected:
lable, detail, thumb, data and index.
So, if we used the getter functions normally, I had to create something like below 5 times!
public function get index():int { return _index; }
It may work, but we can do something better, to save the data all in an object and use only one getter function and extract the kind of details we'd like to see.
In order to do so, you should do the following in your Class file. First introduce your object with all of the information you want to save in it, easily like this:
private var _selectedItem:Object = {lable:"EMPTY", detail:"EMPTY", thumb:"EMPTY", data:"EMPTY", index:0};
it will be very easy to overwrite the values of an object, as an example, you are able to set values to such an object like this:
_selectedItem.data = "some data here!";
ok, and after you have the object ready you should write your getter function like this:
public function get selectedItem():Object { return _selectedItem; }
easy right? you don't have to write several getters now and you will call the getter function from outside of the class like this:
Some call it, combo box but I like to call it a drop down menu! Well, it's my creation and I can name it whatever I like, right?! HaHaHa No matter its name, this is going to be a very interesting class and a lot more complicate than the scrollbar we finished yesterday.
I'm not going to build some silly drop down menu which you just can insert some text in it and do some actions when an item is selected! We are going to build a very nice drop list menu which can have different items shown in it! it can load images, text descriptions, etc, etc, because we are going to need these things later in our application.
So, before we get our hands dirty in writting any code, let's spend a couple of minutes and think about the input parameters that we may need for our drop down menu. Here is what I came up with. (Maybe I add some more items, but right now, I can think of these options right now)
I think this is good enough to get started with. Maybe you have just guessed rgar the very nice thing we are going to do is to import our Scrollbar class into this drop down menu class! The Scrollbar class took a lot of time to be completed, but I think I am much more comfortable with this, so let's get down to work and see how far we can get with this drop down menu class today.
Ok, that was a great weekend, I found some time to get back to some old books and once more read about extensions of classes and I founded out that if there's no reason to extend something, why you should force yourself to extend it?! In our last version of scrollbar we extended the ScrollbarBase class with no actuall reason! maybe it was just an excitement to extend something! HeHeHe
But in this new version which I can call it the final version of a scrollbar, I didn't extend it BUT I did the class in a way that it can be easilly extended later. I thought of some future functionaleties that we may need in our software and it will be nicely possible to extend this current scrollbar and add or modify functionaleties to it, so this version can really be called the final version, I'm quite sure that I would never again go to the trouble of creating a new Scrollbar class
I have also learned a couple of new things which I'm going to share with you.
The first thing that I really liked is the getter and setter functions, so with the help of them, I don't have to pass parameters within the class instance anymore and instead, I set them using the setter function. And another advantage of the setter function is that you are actually using them as private and the setter function will actually write them.
This way will also enable you not to start any action in the constructor function (unless you really need to) with which I mean, you will start the class by calling the "new ..." and set the variables, etc, etc and then after you are ready to use the class, you can manually call a function from outside and it will run the script.
Below you can see how the new class will except the variables. This way it looks much better, don't you think so?
Now let's have a look at how you use the getter and setter functions:
Ok, the first step is to know that you are using the getter and setter functions for a better encapsulation which enables you to set the variables in your class in private. It's better to set these variables in private because you surely do NOT want other extended classes to modify them, right? you only want them to be modified with the setter function. So, in our example we have the three variables set in the class like this.
private var _width:Number = 0;
private var _height:Number = 0;
private var _position:String = "V";
and then you set the functions, in public. take below as an example of a setter function
public function set width(newWidth:Number):void
{
_width = newWidth;
}
take note of the public access method and the keyword "set". that's it! So, as the setter function is public you can easilly access it from outside as in our example like this.
myScroll.width = myScroll_mc.bg.width;
The getter function is also very similar to the setter and it will be used from the outside to read the current value of that property.
public function get width():Number
{
return _width;
}
Take note that getter functions must always have a return value and you will simply call them from the outside like this:
trace(myScroll.width);
And that's all there is to it about the setter and getter
And here is how it works. As you see, it works just like the last version, but this one is really orgenized! At least I think so right now!!! if you are coming up with a better version, please feel free to let me know.
And here is the whole code class for your reference.
Oh, and I almost forgot, I also found something very interesting on how you can handle something like onDragOver, onDragOut, and onReleaseOutside in ActionScript 3.0 (AS3)! As you know already, these events are no more explicitly available in AS3 but it surely does not mean that you can't use them!
I found an interesting article here which really helped me get my code a lot cleaner by using these nice mouse event properties like buttonDown, target and currentTarget!
Now, I'm going to create a drop down menu which is going to be used in our applet later. remember that it must be open for later extensions as we are going to use them in several diffrent ways... let's see what we can come up with in our drop down menu, keep reading my posts.