Syncronising ScrollPane Components in ActionScript 2.

This is perhaps an unusual one – and one I haven’t seen anyone else make much fuss over, so here goes.

Using the standard Version 2 components (Flash MX2004+) I’ve been trying to link the movement of three components together. “No biggie” you’d think. You can after all, set listeners on the .scroll property of the component instance, and then use that to set the .scroll properties of the other instances. Well yes, that’s true – but looking closely at the movement of the ScrollPanes, you can see a slight lag in their movement. A bit of frantic scrolling can throw the whole thing out of place.

var ScrollPaneListener:Object = new Object();
//
ScrollPaneListener.scroll = function(){
scrollpaneinstance2.vPosition = scrollpaneinstance1.vPosition;
scrollpaneinstance3.hPosition = scrollpaneinstance1.hPosition;
}
scrollpaneinstance1.addEventListener("scroll", ScrollPaneListener);

You should see in this example, that the ScrollPanes can go out of sync, particulary while dragging the scrollbar. – And the effect is even more pronounced with really complex contents.

After a bit of digging, I found out that the ‘x’ and ‘y’ scroll properties (not the same as ‘_x’ and ‘_y’) are updated at longer intervals, so can miss out on movements and give inaccurate positions. The solution? Thankfully we can dig into the components themselves with ActionScript, and pull out the properties we want. So, in place of our inital code, we can use;

var ScrollPaneListener:Object = new Object();
//
ScrollPaneListener.scroll = function(){
scrollpaneinstance2.vPosition = scrollpaneinstance1.spContentHolder._y;
scrollpaneinstance3.hPosition = scrollpaneinstance1.spContentHolder._x;
}
scrollpaneinstance1.addEventListener("scroll", ScrollPaneListener);

And in this example, the ScrollPanes should always be at identical scroll positions, even with some pretty intensive contents.

The code is now far more accurate, because it always gets the same position as the actual position of the components’ on stage, although it’s slightly more resource-intensive. (Only slightly)

, ,

About James

James is a Senior New Media Developer at MMT Digital, and has BA(Hons) in Design for Interactive Media from the University of Gloucestershire. He loves designing and producing all sorts of website and Flash-related things, as well as prattling on about technologies.Day-to-day he works with Flash, Dreamweaver, Director, Microsoft Office Sharepoint Server 2007 (MOSS) and in his spare time he mucks about in Flex and Wordpress.Follow James on Twitter.
No comments yet.

Leave a Reply

Note: This post is over 4 years old. You may want to check later in this blog to see if there is new information relevant to your comment.