Archive for the ‘Actionscript’ Category

AS 2: Custom Scrollbar Position Formula

Saturday, April 21st, 2007

This is a formula I am using (and blogging to keep a record of it) to set the position of a target item for a custom scrollbar. Basically, there are a few components to a scrollbar. I won’t get into the core of it but for the most part you have thumb (the item you drag), track (basically the bar or box the thumb drags within), and the target clip.

To understand, a maxScroll variable is needed. This can be static or determined by a mask height. Again, I’m going to get right to the formula. I just wanted to give a quick background to it.

[Code]

target._y = Math.ceil(maxScroll*((thumb_mc._y+(thumb_mc._height/2))/maxPos))

[/Code]

You could add tweening or whatever you want but this will give you the exact position based on the percent of scroll the thumb has been dragged. Hope it helps.

AS2 to AS3 Quick Tip: Where is attachMovie?

Thursday, March 29th, 2007

IT’S GONE! (gasp) :-) Ok a little dramatic but here’s the thing. attachMovie doesn’t exist in AS3, and for good reason.

In Flash CS3, draw a box on the stage then convert that box to a MovieClip named Box. In the Linkage dialog (or Advanced section of the Properties dialog), set the linkage to Export for Actionscript. Notice the base class is set to flash.display.MovieClip, which is perfectly fine for this example. Click OK.

We now have a dialog box that basically tells you the class Box doesn’t exist so it will be created for you in the SWF “upon export” (basically…just click OK and move one). :-) This is a nice feature. What it allows you to do is now create instances of this class. So, I return to the question. Where is attachMovie?

Take the following partial code snippet. It basically loops over some dummy data, creates a new Box() instance, sets properties on it, and adds it to the “_root” child list. The boxes will be aligned one after another in a diagonal line.

var data:Object = new Object();
data.rows = 10;
data.columns = 6;

var __x:int = 0;
var __y:int = 0;
var boxTemp:Box;

for(var i:int = 0; i < data.rows; i++){
 boxTemp = new Box();
 boxTemp.x = __x;
 boxTemp.y = __y;
 addChild(boxTemp);

 __y+= 20;
 __x+= 20;
}

That’s it for this quick tip. I was working on some AS3 in Flash CS3 and my mind kept reverting to AS2. I wrote the entire example above with attachMovie and variables x and y (conflicts with the x and y properties of the root display object). :-) My mind had to revert to AS3 in the Flash IDE, which was a bit weird but no biggie.

Look for more of these quick tips. I’ll post’em whenever I get a chance.