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


Jan 15 2009

Scrollbar class V 1.2

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

We are almost done with our Scrollbar class. slider and up and down buttons and mouse wheel all working fine now. check below. (Note that you must first click on the flash object to get the focus to the flash object.)

And you can download the files from here.

Although we are having all we need with this scrollbar now but it still needs to be tuned a bit more. I don't like how the codes are orgenized. in the next version I try to use some more rules from AS3 to make a clean and better code.

For now, let's see how we have managed the action on the up and down buttons. It's clear that we would need a MOUSE_DOWN listener to find out when the button is pressed so we use the follwoing on the cunstrctor function

_up.addEventListener(MouseEvent.MOUSE_DOWN, down);

this event will call a function called "down" and in that function we have:


		  
		  
		  
		  
		  
		  
		

There are some helpful notes to know about the Mouse events and that's the point that when a function is called via any mouse events, a parameter is also passed to the function automatically which holds a couple of values and one of these values is the target which is actually the movieclip which the event listener has been registered to.

If you are wondering what other MOUSE events are available in AS3, Here is a list of all the mouse events in AS3:

CLICK : String = “click” MouseEvent
Used to detect mouse clicks.

DOUBLE_CLICK : String = “doubleClick” MouseEvent
Used to detect double clicks.

MOUSE_DOWN : String = “mouseDown” MouseEvent
Checks when mouse is pressed down.

MOUSE_LEAVE : String = “mouseLeave” Event
Monitors when the mouse leaves the stage.

MOUSE_MOVE : String = “mouseMove”
Monitors when the mouse moves.

MOUSE_OUT : String = “mouseOut”
Monitors when the mouse moves out of the object that the event has been attached to.

MOUSE_OVER : String = “mouseOver”
Monitors when the mouse moves over the object that the event has been attached to.

MOUSE_UP : String = “mouseUp”
Monitors when the mouse moves up the attached to object of the event from a click.

MOUSE_WHEEL : String = “mouseWheel”
Monitors when the mouse wheel moves, detect the positive or negative delta property for distance and direction moved.

There's nothing you can't do with your mouse the help of these mouse events. I like this much more than how they where in AS2!

ok, back to our script, when the mouse is down and the event.target detects that the object is the up button, it will start the timer class and  the timer class will call "timerHandlerUp" every 10 milliseconds. the same thing happens if the _do buttons is clicked but this time the "timerHandlerDo" function will be called. It's clear that we must remove the timer listener as soon as the mouse is up.

You know what, there are a lot of parts in AS3 that surely makes our job easier but still there are some of them who are hard to be understood and one of them is the Timer class! Although I have been able to use it successfully, but I'm not clear about some of its features. in some other topics I will try to get very detailed on this Timer class and see how it works exactly.

Anyway, below is the modified file for the Scrollbar class.


		  
		  
		  
		  
		  
		  
		

It really needs to get clean! I'll take acre of that in my next post.

Regards,
Hadi

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


Jan 15 2009

Scrollbar class V 1.1

Category: Scrollbar,Scrollbar ClassHadi Tavakoli @ 2:38 pm

In this article we are trying to make our scrollbar to work horizontally also. Download the files from here and then read below. (FLA is saved with CS4)

I haven't yet added any more functionalety to it and we have just made it to work horizontal also, here is how it looks now:

The changes in the FLA are not very complicated and if you have read my last posts you will find your way fast in the new FLA.

but as far as the .as files, I have now devided the classes into 3 and each one now actually does a seperated work.

ScrollbarBase.as   >  This class will introduce all the variables and sets the positions of all diffrent elelemts by initializing the class "ScrollbarSetter"

If you have noticed I have changed "private" to "protected" for all of the variables in "ScrollbarBase.as" I did this so that I can have access to them easilly from "Scrollbar.as" later which will extends the base class.

I still don't feel comfortable on when to use private and when protected and which methods will make the codes more capsulated, but I think doing some more work will help me understand which one is a better solution. But for now, protected looks just fine as it gives access to the base variables in the subclass file.

anyway, as you can see I have moved the whole responsibility of organizing of the scrollbar according to it Vertical or Horizontal status to a new class called "ScrollbarSetter.as" and in the constructor function of the base class we will call the new class like this:

setpositions = new ScrollbarSetter(pos, w, h, _controlers, _up, _do, _slider, _sliderBg);

well, I tried to use the "internal" accessory for those variables that "ScrollbarSetter" wanted to use but I just couldn't figure out how to make that work! or anyway, would it be a good idea to use the "internal" at all? anyhow, I just passed the parameters and below is the code for how we have set the position of scrollbar according to the H or V status.


		  
		  
		  
		  
		  
		  
		

and here is the base class.


		  
		  
		  
		  
		  
		  
		

The important point is that when you want to put a content under the scrollbar you will actually call the Scrollbar class (This is NOT the same file as my last post, don't mix things up! I'm learning things just like you right now, so although it's said that when a class is written, you don't have to modify it anymore, but that can only happen when you have enough experience on how you should organize the base classes, so I hope soon we will reach that level of professionalism! :) but for now, please bear with me.)

as I was saying, you'll call the Scrollbar class from the FLA like this:

var scroll1:Scrollbar = new Scrollbar(myScroll_mc, //instance of the scrollbar on stage
 myScroll_mc.bg.width,
 myScroll_mc.bg.height,
 sampleContentV_mc, //content to be scrolled
 "V"); 

And this is the Scrollbar class which extends the base class. here is the Scrollbar class now:


		  
		  
		  
		  
		  
		  
		

OK, this should do it for now and the scrollbar now works horizontally also but we need to get our data a bit more organized I think as there are still a lot of dark points in the basic facts of AS3 in my mind.

Maybe it would be worthy if I talk a bit about how super(); works as I had some problem with it when working with it for the first time.

when you are extending another class, you cannot have a complete new constructor function for your subclass and it's a must to use the super() which actually means you are importing the base class constructor function and as you see you need to send the parameters through it like below in our sample:

super(scroller, w, h, theContent, pos);

and after that you can add your other lines of codes... But still knowing this after receiving lots of error messages, I don't know how it worked in the tutorials when I extended a Movieclip and I wrote the constoctur function without using the super(); ! I will try to find an answer for that soon though!

in the next version of the class, I try to get the _up and _do buttons to work also.

Regards,
Hadi

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


Jan 14 2009

Scrollbar class V 1.0

Category: Scrollbar,Scrollbar ClassHadi Tavakoli @ 5:06 pm

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

Here we are trying to build our first class which is a Scrollbar, a scrollbar which can smoothly move the content up and down when you drag the slider.

Before anything, you must think about how you want to call your scrollbar in your project. in my case, I'd like to be able to call it like this:

 


		  
		  
		  
		  
		  
		  
		

So we first create a new movieclip and orgenize all the graphics in it. Download the files from here and open the FLA for better understanding

We will have a transparent "bg" movieclip. the reason that it's transparent is that we don't want anybody to actually see it but we will be able to modify its width and height and everything else will set itself according to the size of this movieclip. This will enable us to have diffrent width and height for our scrollbar on different ocasions.

We will also have another movieclip "theContent" which is empty and as its name shows, our content movieclip will be added into this one by calling addChild. will get to that later.

We also need a mask to hide the unwanted parts of our content! For those who are comming from AS2 maybe it's a pain to find the right syntax for masking! well, at least it took me half an hour to find out! :)

content.mask = mask_mc; // put "content" under the mask graphic

And finally we need a movieclip to put all our scrollbar control buttons inside it.

 

This is all easy, as it's just a matter of creating some movieclips and putting them inside the library. Now before we get to the .as file, let's see final results.

 

ok, as far as the class file, we first write the skeleton of our class


		  
		  
		  
		  
		  
		  
		

If  you can test your movie and see the output "Working fine till here!" then it means that we are on the correct track and the connection between the class file and fla is successful.

The next thing we would do is to import all the classes that we are going to use in our file so we will import them into the package block


		  
		  
		  
		  
		  
		  
		

And in the class block we introduce all of the private variables that we are going to use later.


		  
		  
		  
		  
		  
		  
		

 

Beleive me, things are very easier than what you could imagine! after we have the variables all ready all we have to do is to use the constructor function to set the positions of all elements. All positions will be set according to the width and height that we sent from our FLA using the bg... remember?

Here is the constructor function. as you see we just set the position of all elements, then we add the content movieclip into the holder movieclip and then we set the mask and finally we add the mouse events to do some actions when the user drags the _slider.

in the next version we will add some more functionaleties to the _up and _do buttons also and maybe we also add some mouse wheel functionaleties also! I hope working with the mouse wheel and setting the foucuses are easy to do in SA3!!!
 
below is the full completed class for your reference:


		  
		  
		  
		  
		  
		  
		

The little trick we've done about the ENTER_FRAME listener is that we are not just initializing it once and leave it because it will eat a lot of CPU so we must make sure to run the listener only when we need it and stop it when we don't.

Considering this, we add the listener as soon as MOUSE_DOWN and remove the listener only when: (Math.ceil(newY) == Math.ceil(-_theContentHolder_mc.y) && dragStatus == false)

OK, that's it, we now have a good scrollbar that can be in different sizes and designs and can scroll any movie clip that you put into it. But still there are a lot of enhancements to be made like disabling the scrollbar if the content movieclip is smaller than the mask, or the buttons up and down to function and if the scrollbar is to be used vertically or horizontally... and lots of other enhancements...

Remember that OOP is telling us not to include everything in one class! so we'll lesson to this important note and try to clean our class a bit more. I'm now going to do some "extend" work and see how we can do those things. as this is the first package we are creating, let's make sure it's working perfect in any possible way, so let's put every possible functionality that a scroller should have and also make sure to divide the details into different classes...

Keep checking for my next post, I'm right now trying to enhance the current class and divide it into more specific classification of functionalities.

Regards,
Hadi

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


Jan 12 2009

Create animated gif files in flash AS3

Category: SlushhHadi Tavakoli @ 12:21 am

OK, we are trying to create a flash tool which users can play with to publish their own designed animated gif files and save them locally on their computer or publish it on the web.

Let's call this software "Slushh"! and we will let users choose different graphic designs in their canvas, let them draw things around, let them upload their images or animated gif files, let them type notes and choose their favorite font, size, etc, etc

As it all appears, this gonna be a very huge piece of software, but it will be a great fun I'm sure and I hope I can finish it before March. it maybe a bit challenging though as I'll be working on it alone as the rest of the team is still on AS2! So if I'm going wrong anywhere or you might have any better idea on how things are getting done, please do advice me.

Alright, before anything, we need to have a complete list of all is going to happen in the software. no matter how complicated the work is, we can create an outline from what will be the output and what are the inputs. and narrow down the requirements little by little until we really feel that a task cannot be no more divided into any smaller sections.

OK, here is the outline of all is going to happen in the software in general. We need to make sure that it's covering all the main functions. Here I have divided the whole thing into three steps, first input will gather all of the information the software may need to server the user start working on his graphics. Second, user will play around with the different tools available in the software to actually create his frames of animations and third, user will be able to preview and save what he has created. Although the classification below is not in great details but it does help us find our way around. I don't know about you but to me, starting a project is always the biggest step to take and after you have the details listed and find the starting point, then everything else will be easy to achieve.

  • Inputs
    • flashVars //different settings to be inserted. We need a class to grab all the flashVars and save them for later uses in the software.
    • Template //A graphic chosen by user to act like a frame for the final output, it can be gif or png, animated or static, let's call it the template. We need a class to test the imported template (it will be loaded via flashVars) and decide on its type and introduce it to the software.
    • Stamps //These will be some animated or static graphics which user can add to their stage at run time. We need a class to load the stamps which are categorised in XML.
    • Effects //We will have some Photoshop like effects which can be applied to any selected inanimate graphics. We need a class for the reading effects' names, thumbnail and function from XML. Effects will be actually produced with ImageMagick on the server.
    • Fonts // I'm not sure if we can produce and embed fonts at run time, but if we can, we need a class to grab the fonts from XML and embed them for later use in the software.
  • Processes
    • Timeline //Frames for the animation can be added, removed or duplicated
    • Text Tool //User can select his favorite text style and write anywhere on the stage
    • Doodle Tool //Doodle can draw with different brushes and styles in the stage
    • Effects Tool //in-animated graphics will be due to effects, multi-effects available and they can be removed from a graphic
    • Upload Tool //User can upload his own graphics or animated gif files
    • Image Insertion Tool //the software will provide a basket full of different graphics that user may insert
    • Stamp Insertion Tool //effects can be applied on in-animated stamps only
    • run time PNG conversion //Every frame of the movie will be converted to PNG in run time. (While ago I read an article that was talking about a class that can do such on the fly conversion in flash player 10 so if we do this without the need of sending pixel information to server and having PHP to generate the file then it directly means that we are saving a lot of time and bandwidth) I feel like we need flash CS4 for this... hmm, not sure yet, but we'll find a solution for that when the right time comes though.
  • Outputs
    • gif file //Would it be possible to create animated gif files in run time also?!! I guess I'm expecting too much from flash! Am I getting too greedy on this? HeHeHeHe! we'll come to this later anyway, but if it's not possible which I think it won't be, then we need to put all the frame information in an XML object and send it to PHP and PHP will be able to put the frames together using the ming library and get us the final gif file to perform in the preview window and even give users to save the file on their local computer if they are logged in!

 

Alright, let's get to work now. But before we start, keep in mind that the main intention of creating classes is to for you to save time in future. it's a kinda time investment! as it will require you to think more and devote more time for creating classes that later you will reuse them over and over again so easily. So it will defenetily worth it.

I remember someone told me that if you are not going to use classes with AS3, then even don't bother buying the expensive CS4 (or CS3) ! because the whole idea behind AS3 is to code in OOP.

OK, now I'm going to create some classes which are going to be used in our project :-SS Let's start with a scroller, then a preloader... make sure to read my next posts...


« Previous PageNext Page »