PDA

View Full Version : Need help with this array/position problem



rondog
February 26th, 2009, 08:35 PM
I have a scroll pane component with SWFs loaded in them vertically like a PDF viewer. I need to be able to change the page indicator when the user scrolls.

I have an event listener on the scroll pane listening for scroll.

I have an array of the Y values of each page; looks something like this:
0,800,1600,2400,3200 etc

What kind of equation would I do to determine the correct number.

My scroll event looks like this:


private function checkYValue(e:ScrollEvent):void
{
var currentY:Number = (e.position + PAGE_SPACING) / PAGE_SPACING;
/* traces out these kind of values
1.05
1.055
1.06
1.065
1.07
*/
}


currentY is obviously the Y position of the scroller in pixels starting from 0.

So with that said, how could I change the page number based off of currentY?

MurtenSaerbi
February 26th, 2009, 08:54 PM
I would divide my current Y position with 800 and the number you will get (you need to round it off) will be the page number.

Let's say y = 1648. Then you should do: 1648 / 800 which equals to 2.06 so the user is at page 2.

rondog
February 26th, 2009, 09:15 PM
Yup that'll work. I did Math.floor because I would be halfway down page 1 and it would say page 2.

rondog
February 26th, 2009, 09:19 PM
Wait I noticed a problem. If I use the mouse wheel to scroll it stays page 1. If I trace out:

var currentY:Number = Math.floor((e.position + PAGE_SPACING) / PAGE_SPACING);

it traces out two number:

the actual y
1

1 must be the mouse wheel..whats that about?

cbeech
February 27th, 2009, 12:59 AM
seems that there are two outputs, one for each, x / y position value, but i am not finding a way to 'access' which value is being specified using the 'position' property alone. it appears as though you may be better off calling the value in this manner:

var currentY = Math.ceil((e.currentTarget.verticalScrollPosition+ e.currentTarget.height) / PAGE_SPACING);

although using the calc without the height being deduced works just fine, it will be a matter of 'when' you would like the page number to change - when the page 'touches' the bottom of the pane, or when the page touches the top of the pane - course you could split the height value and call it in the middle LOL!

i also would go with Math.ceil here as floor will take you down below 50% of the value - in other words it wont read as page 1 until you've covered half the initial distance - and one can't scroll 'above' the maximum value so rounding up may be more conveinient.

rondog
February 27th, 2009, 01:22 PM
Hey cool man that seems to work the way it should now. I was looking at the verticalScrollPosition and didnt even think of using that rather than e.position. Thanks a lot man

cbeech
February 27th, 2009, 04:57 PM
np - dude, you're welcome