Jan 25 2009

AS3 Info Box Class

Category: BoxHadi Tavakoli @ 12:41 am

If you have read the previous posts, you know that we are trying to build a drop down menu and till here we have the scrollbar ready. and as you know we need a container for our items which are going to be placed under the drop down menu. so we here we create a Box class which enables us input three different parameters in it. Of course we are going to make the box in a way which the parameter are optional so in result we can have different kinds of Box instances which all may look different when we change the input.

we need a class which itself can be used as a MovieClip so we will make sure to extend the MovieClip class. I thought that it's not always a good idea to create dynamic classes through library and reference them via new class()  as little by little they can make our code dirty, so wherever we can create classes which they can act as a movieclip or something, it would be better to just extend it.

Alright, for our box to appear on the stage, we will use the follwoing.


		  
		  
		  
		  
		  
		  
		

myBox.floorWidth > will be used to set our box's width
myBox.floorHeight > will be used to set our box's height
myBox.font > will be used as our font but remember to create an empty dynamic text object and place it somewhere off the stage and embed the characters you would need in it. You need to do this because in the class later you will see that we will embed the fonts. (Some where I read that it is possible to embed fonts dynamically into classes which was not possible in SA2. I will look into that later as it will be another nice improvements in AS3)
myBox.fontSize > This will be the size of our fonts in the Box
myBox.fontColor > This will be the color of our fonts
myBox.addInfo({}); > I like this part a lot! as we can call the addInfo method within the Box class and add our information to it easily as you can see the sample above. Note that we want our Box class to accept optional features so if we insert "--" for the object values, it will simply ignore it which in result you can have a Box with thumbnails and label and details all available or just a label :) you can find the final look of our box below.

Notice that we have added no mouse events because OOP is telling us not to make the classes very complicated. so we will just have a box which shows the input. later if we needed a Box which should do something on a click or something, then we can nicely extend our current Box and add listeners to the new sub class.

And here is the Box class below.


		  
		  
		  
		  
		  
		  
		

As you see, in this class I have learned how to deal with a couple of important functionalities like TextField and Loader which I try to give some brief explanation on them.

 

How to use the text class

first you need to import the necessary classes.

import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.AntiAliasType;

then, introduce your variable in the class block  which will later hold your string.

protected var myLable:TextField;
protected var myFormat:TextFormat = new TextFormat();

Note that in this example, "myLable" will hold our text string later and myFormat will hold all the necessary properties for the style of our text.

After that you should start the text field class by calling new TextField(); and set any different settings you want for the text field (This is NOT the style of your text but the properties of the text field itself)

myLable = new TextField();
myLable.antiAliasType = AntiAliasType.ADVANCED;
myLable.autoSize = TextFieldAutoSize.LEFT;
myLable.embedFonts = true;
myLable.mouseEnabled = false;

There are a lot more properties for text fields that I'm sure you can easily find the list in flash's help documents, but here I have mentioned just the properties that I needed to set in my Box class. so feel free to play around with different properties, I assure you that it's will be fun :)

OK, after you have your text field set, you need to set the format (or the styling of your text) ready.

myFormat.font = _font; // "Tahoma"
myFormat.size = _fontSize; // 11
myFormat.color = _fontColor; // 0x000000

After you set the text field and the format ready, it will be the time to link these two objects together like this.

myLable.defaultTextFormat = myFormat;

You're almost done, now insert your string in plain string or html like this.

myLable.text = "my first text!";
myLable.htmlText = "my <b>first</b> text";

 

That's it, I remember a couple of years ago when I was learning AS2 and found about how text fields are organized, I used to say why we should do a lot of work to display a simple text?! well, these things are necessary, every piece of them are necessary. Computers are dumb, we need to tell them every details :)

And by the way, please note how differently AS3 treats the auto size thing... I'm not going to talk about the details here, I'm sure you will find a lot of detailed articles talking about other small things here and there, here you just understood how to make a text field appear on your flash movie. If you found something nice with it, make sure to let me know though! :)

Anyway, there will be a lot of talks about text fields later as in our application we are going to do a lot of crazy things with the text fields.

Regards,
Hadi

Tags: , , , , , , , , , ,


Jan 23 2009

AS3 Creating Custom Event Listeners

Category: ClassesHadi Tavakoli @ 8:50 pm

Maybe you have heard a lot about creating custom listeners, well, doing this is very easy in AS3 now! At least it will look simple when you figure out how! But from my personal experience, I don't think it would be a good idea on some occasions. first let's see how the custom listeners will be created using dispatchEvent Here is what I was going to do initially (I changed my mind later!). I was trying to build a class for loading external files into our movie so it will make the job easier without forcing me to deal with the Loader and URLRequest classes each time. I was hoping on creating a class where I can simply load something like this:

var myImageLoader:ImageLoader = new ImageLoader("00.jpg", this); 

so I wrote the class like below considering all the different listeners that you may want to deal with when loading an external file.


		  
		  
		  
		  
		  
		  
		

As you see, we get the input as one url which will be the address of the file to be loaded and a movieclip target where we want to place the loaded content to. I have used the good dispatchEvent event to create custom listeners with a new name so it enables me to create new listeners. and I could use the new listeners like below


		  
		  
		  
		  
		  
		  
		

Very nice and clean idea you might think, yeah? OK, I used to agree with this too, but nope, forget about it! :( let me get you to the point quickly, Your custom ProgressEvent will NOT work! I'm not sure if it's right to say this for sure, but I tested this in different ways for a thousand times, but it simply won't work and will just send it's properties, bytesTotal and bytesLoaded always zero "0"! which makes it impossible to build any preloader using your custom listener! I also tried digging in the web but I didn't manage to find any article or anything talking about this little issue. Maybe it's a possible bug in AS3 and how dispatchEvent works? I really don't know. I hope someone can give some idea about this. but anyway, in my case, I don't have time to spend more time on this! So I decided to create a function for this reason and pass parameters of whatever I wish to load into the function directly and use the listeners directly. Oh, don't take this wrong, other custom listeners will work just fine and the problem seems to be with the ProgressEvent only. so if you are not going to use the progress event, you will still be able to create your own custom listeners in a separated class. But I personally rather create a function now as it will help me out of the headache at least. if anyone has a better experience with this, please do let me know. I'm going to get back to my Box class and clean it up with an almost stand alone function for taking care of the loading process of external files. hmm, I don't feel satisfied with having to do it with a function in every class I may need something to load... but I have to do this until I find a work around the problem.  

Problem Solved!

January 25th, 2009
ok, I need to edit my post and correct the ImageLoader class we had built before. Below will be the correct class as in my last post I didn't sent the necessary information via dispatchEvent! So there's NO bug with ProgressEvent :) I was just an idiot not to fining the right answer.


		  
		  
		  
		  
		  
		  
		

The only unsolved question in my mind are bubbles and cancelable which I see them a lot of places but can't understand what they mean exactly yet. I will post an article when I did. Anyhow, I think it's a good idea now to use the ImageLoader class because soon we need to create some extended classes from this ImageLoader to load images in Bitmaps... Regards, Hadi

post updated on http://www.emstris.com/2009/02/custom-event-listener-final/

Tags: , , , , , , ,


Jan 20 2009

Encapsulation

Category: ClassesHadi Tavakoli @ 2:46 pm

The real fun is just getting started, building classes needs more concentration before you start. As I had mentioned in my last post, we were trying to build a special DropDownMenu for our needs in the application, and what I thought initially was that I will be able to use the Scrollbar we had built into the drop down menu and that's all.

But when I was building the drop down menu, I suddenly understood another class to be used inside our menu, though I named the new class ItemList Class! So, now we should first build an ItemList class which its job would be to create the necessary boxes for our items which later will be placed under the Scrollbar and the Scrollbar will the go under the menu.

But again, I thought again and found out that we again need another class named Box so it will first create us the boxes with information in it and then we can insert the boxes to the ItemList and then put the ItemList under the Scrollbar and finally the Scrollbar under the drop down menu!!!

I was building the Box class that I suddenly understood that again we need another class inside of it!!! OOHHHH :) HaHaHa

Don't get crazy, that how it is, it's inevitable, that's just how it is, we needed a ExternalLoader class which could load any given external file in it.

Let me tell you something, we may had created your drop down menu without breaking it into smaller classes, but that's totally a wrong idea because you will in any case have to write some algorithms so why you shouldn't do them all in separated classes which each one will do only its work independently?

How we have divided our menu class into a lot of smaller classes is a very good idea because you may somewhere else need to use that ItemList to list some objects without using it under a menu and now that it's built in a class you will be able to do that.

So it's always a good idea to create as many classes as you can, do the classes and break them down to create new classes, do it until you really come to a point that there's no more need to cut a class to any smaller pieces. That's when you are really writing classes with respect to OOP :)

OK, today I will post two new classes, the ExternalLoader and the Box class... Keep checking back...

Regards,
Hadi

Tags: , ,


Jan 18 2009

AS3 Drop Down Menu Class

Category: DropDownMenu,DropDownMenu ClassHadi Tavakoli @ 3:28 pm

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)

var myCombo:DropDownMenu = new DropDownMenu(myCombo_mc);
myCombo.width = 100;
myCombo.floors = 5;
myCombo.floorHeight = 50;
trace(myCombo.position);
myCombo.buttonMode = false;
myCombo.font = "Arial";
myCombo.fontSize = 12;
myCombo.fontColor = "#000000";
myCombo.align = "center";
myCombo.defaultItem.index = 0;
trace(myCombo.selectedItem.data);
trace(myCombo.selectedItem.index);
myCombo.add({lable:"", detail:"", data:"", thumb:""});
 

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.

Regards,
Hadi

Tags: , , , , , , ,


Jan 17 2009

Final Scrollbar class V 2.0

Category: Scrollbar,Scrollbar ClassHadi Tavakoli @ 8:23 pm

this post is out of date! check the new scrollbar here: http://www.emstris.com/2009/10/my-super-as3-scrollbar/

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 :)

 

Here you may download the final class for our scrollbar. (Saved in CS4)

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.

Regards,
Hadi

Tags: , , , , , , , , ,


« Previous PageNext Page »