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