View Full Version : [AS3] scrolling and maths :(

01-11-2008, 01:56 PM
ok - the following seems to be a popularly-used bit of code for scrolling a container, where the speed + direction of the scroll changes according to the mouse position.

There's a sort of working version here (http://www.bencasey.co.uk/flash/)

I need to tweak it, but am suffering a severe mental block. The code below scrolls the container according to the bounds of the stage width - I need to scroll it to the bounds of a masking object that overlays the actual scrolling object. Using my vast Photoshop skills, I have illustrated this in the following diagram:


- where the mask is, in fact, a viewport through which you see the scroll bit. Any bits of the scrolling thing that are outside of the mask area are hidden.

The current code:

stage.addEventListener(Event.ENTER_FRAME, scroller);

function scroller(evt:Event):void{
var percentage:Number = mouseX/(stage.stageWidth);
var mouseSpeed:Number = -(percentage);
var newX:Number = Math.round((container.width-stage.stageWidth) * mouseSpeed);
container.x += Math.ceil((newX-container.x)*(speed/100));

Can anyone see how to adjust this so that the container scrolls not to the stage edges (which leaves parts of it hidden outside of the mask/viewport), but to the edges of the mask itself?