PDA

View Full Version : AS3 Content Scroller


devonair
12-29-2006, 01:34 AM
Something to play with until some components are released:

package com.onebyonedesign.as3.utils.ui {

import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.geom.Rectangle;

/**
* @author Devon O. Wolfgang
* @date 29NOV06
*/

public class ContentScroller {

private var _mask:Sprite;
private var _content:Sprite;
private var _dragger:Sprite;
private var _easeRate:Number;
private var _direction:String;
private var _bounds:Rectangle;
private var _setSize:Boolean;

private var _scrollValue:Number;
private var _orgY:Number;
private var _orgX:Number;

/**
*
* @param content Sprite which will be scrolled by ContentScroller
* @param mask Sprite which will mask the scrolling content (mask should be set in another class)
* @param scrollbar Sprite which will be used to do the scrolling
* @param ease Number which controls amount of ease in scrolling. Higher the number, the more ease. 0 or 1 will have no ease.
* @param direction String specifying the direction of the scroll. Can either be "vertical" or "horizontal".
* @param setDraggerSize Boolean which decides whether to stretch the width or height of the scrollbar in proportion to the amount of content.
*
*/
public function ContentScroller(content:Sprite, mask:Sprite, scrollbar:Sprite, ease:Number = 0, direction:String = "vertical", setDraggerSize:Boolean = false) {
_setSize = setDraggerSize;
_content = content;
_mask = mask;
_dragger = scrollbar;
_easeRate = ease;
if (_easeRate < 1) _easeRate = 1;
_direction = direction;

switch(_direction) {
case "horizontal" :
initHorizontal();
break;
case "vertical" :
initVertical();
break;
default :
trace ("CONTENT SCROLLER ERROR: direction argument should be either \"vertical\" or \"horizontal\".");
break;
}
}

private function initHorizontal():void {
if (_mask.width < (_content.width - 1)) {
if (_setSize) _dragger.width = Math.ceil((_mask.width / _content.width) * _mask.width);
_scrollValue = _mask.width - _dragger.width;
_orgX = _dragger.x;
_bounds = new Rectangle(_orgX, _dragger.y, _scrollValue, 0);

addListeners();
}
}

private function initVertical():void {
if (_mask.height < (_content.height - 1)) {
if (_setSize) _dragger.height = Math.ceil((_mask.height / _content.height) * _mask.height);
_scrollValue = _mask.height - _dragger.height;
_orgY = _dragger.y;
_bounds = new Rectangle(_dragger.x, _orgY, 0, _scrollValue);

addListeners();
}
}

private function addListeners():void {
_dragger.addEventListener(MouseEvent.MOUSE_DOWN, onDraggerPress, false, 0, true);
_dragger.addEventListener(MouseEvent.MOUSE_UP, onDraggerUp, true, 0, true);
_dragger.stage.addEventListener(MouseEvent.MOUSE_U P, onDraggerUp, false, 0, true);
}

private function onDraggerPress(me:MouseEvent):void {
_ratio = (_dragger.y - _orgY) / _scrollValue;
_ty = _orgY - (_content.height - _mask.height) * _ratio;
_dragger.startDrag(false, _bounds);
if (_direction == "vertical") {
_dragger.addEventListener(MouseEvent.MOUSE_MOVE, onVerticalScroll, false, 0, true);
_dragger.stage.addEventListener(MouseEvent.MOUSE_M OVE, onVerticalScroll, false, 0, true);
} else if (_direction == "horizontal") {
_dragger.addEventListener(MouseEvent.MOUSE_MOVE, onHorizontalScroll, false, 0, true);
_dragger.stage.addEventListener(MouseEvent.MOUSE_M OVE, onHorizontalScroll, false, 0, true);
}
}

private function onDraggerUp(me:MouseEvent):void {
_dragger.stopDrag();

_dragger.removeEventListener(MouseEvent.MOUSE_MOVE , onVerticalScroll);
_dragger.stage.removeEventListener(MouseEvent.MOUS E_MOVE, onVerticalScroll);

_dragger.removeEventListener(MouseEvent.MOUSE_MOVE , onHorizontalScroll);
_dragger.stage.removeEventListener(MouseEvent.MOUS E_MOVE, onHorizontalScroll);
}

private function onVerticalScroll(me:MouseEvent):void {
me.updateAfterEvent();
if (!_content.willTrigger(Event.ENTER_FRAME)) _content.addEventListener(Event.ENTER_FRAME, moveVertical, false, 0, true);
}

private function onHorizontalScroll(me:MouseEvent):void {
me.updateAfterEvent();
if (!_content.willTrigger(Event.ENTER_FRAME)) _content.addEventListener(Event.ENTER_FRAME, moveHorizontal, false, 0, true);
}

private function moveVertical(e:Event):void {
var ratio:Number = (_dragger.y - _orgY) / _scrollValue;
var ty:Number = _orgY - (_content.height - _mask.height) * ratio;
var dist:Number = ty - _content.y;
var moveAmount:Number = dist / _easeRate;
_content.y += moveAmount;
if (Math.abs(_content.y - ty) < 1) {
_content.removeEventListener(Event.ENTER_FRAME, moveVertical);
_content.y = ty;
}
}

private function moveHorizontal(e:Event):void {
var ratio:Number = (_dragger.x - _orgX) / _scrollValue;
var tx:Number = _orgX - (_content.width - _mask.width) * ratio;
var dist:Number = tx - _content.x;
var moveAmount:Number = dist / _easeRate;
_content.x += moveAmount;
if (Math.abs(_content.x - tx) < 1) {
_content.removeEventListener(Event.ENTER_FRAME, moveHorizontal);
_content.x = tx;
}
}
}
}

Usage example (use this as document class):

package {

import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;

import com.onebyonedesign.as3.utils.ui.ContentScroller;

public class ScrollTest extends Sprite {

private var _m:Sprite;
private var _drag:Sprite;
private var _c:Sprite;
private var _gibberish:TextField;

private var _cs:ContentScroller;

public function ScrollTest() {

stage.frameRate = 31;

init();

}

private function init():void {
// create content sprite
_c = new Sprite();
_c.x = 100;
_c.y =100;

// create mask sprite
_m = drawRect(200, 200, 0x000000);
_m.x = 100;
_m.y = 100;

// create scrollbar sprite
_drag = drawRect(15, 15, 0x141414);
_drag.x = 300;
_drag.y = 100;


// create textfield
_gibberish = createText();
_gibberish.x = 5;

// add the textfield to the content sprite
_c.addChild(_gibberish);

// mask the content with the mask
_c.mask = _m;

// add everything to the display list
addChild(_c);
addChild(_m);
addChild(_drag);

// create the scroller
_cs = new ContentScroller(_c, _m, _drag, 5, "vertical", true);
}

private function createText():TextField {
var tf:TextField = new TextField();
var fmt:TextFormat = new TextFormat("_sans", 11, 0xCCCCCC);
tf.defaultTextFormat = fmt;
tf.background = true;
tf.backgroundColor = 0x141414;
tf.width = 190;
tf.height = 400;
tf.autoSize = TextFieldAutoSize.LEFT;
tf.multiline = true;
tf.wordWrap = true;
tf.selectable = false;
tf.mouseEnabled = false;
tf.text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas tempus arcu at erat. Nam accumsan ultrices augue. Morbi fermentum vestibulum tellus. Pellentesque et mauris vehicula sapien bibendum fermentum. Aliquam neque massa, elementum dapibus, ullamcorper sit amet, vulputate at, mauris. Nam imperdiet. Aliquam tempus nonummy felis. Nunc blandit dolor posuere enim. Nulla pretium purus ut orci. Praesent nibh lorem, egestas vitae, mattis quis, elementum et, erat. Etiam est sem, vestibulum sed, venenatis sit amet, placerat a, massa. Nulla ullamcorper ipsum ut massa. Morbi scelerisque tortor sit amet felis. Sed nec turpis. Fusce dui dolor, vestibulum nec, lacinia quis, venenatis non, est. Ut hendrerit, dui vel fermentum aliquam, lorem sem tempor purus, ut facilisis pede diam eu mauris. Nam ultricies. Aliquam id metus.\nCum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Phasellus dui tellus, condimentum pellentesque, venenatis nec, tincidunt ac, risus. Nam metus magna, lobortis vitae, molestie quis, laoreet eu, purus. Integer pulvinar, mauris et consequat pellentesque, felis purus porttitor nisl, nec faucibus tortor massa quis elit. Duis auctor. In erat tortor, molestie non, hendrerit quis, consequat vel, arcu. Sed enim. Praesent magna lectus, tincidunt eget, dignissim sed, commodo id, massa. In neque risus, gravida et, molestie sit amet, pellentesque in, massa. Curabitur elementum vehicula mi. In feugiat libero non metus. Duis feugiat, diam ac adipiscing tristique, velit ligula consequat purus, vel varius ligula mauris vitae velit. Nunc vehicula. Morbi luctus leo. Duis id enim varius mi ultricies convallis. Vestibulum egestas porttitor augue. ";

return tf;
}

private function drawRect(w:Number, h:Number, col:uint):Sprite {
var s:Sprite = new Sprite;
s.graphics.beginFill(col);
s.graphics.drawRect(0, 0, w, h);
s.graphics.endFill();

return s;
}

}
}

The above script makes this: http://www.onebyonedesign.com/flash/scrolltest/

A little word of "warning" - make sure all Sprite instances are added to the display list before passing them as arguments to the class constructor...

stringy
12-29-2006, 04:23 AM
Great work as always.
I had reason to make something similar a couple of weeks ago,took me ages and yours is far better.

devonair
12-29-2006, 09:35 AM
Thank you stringy - much appreciated.. I liked it 'cause an ap I was working on needed both horizontal and vertical scrollers, so that "saved" me a bit of work.. Now, I just gotta add mousewheel interaction...

mprzybylski
12-30-2006, 03:54 PM
very nice work. simpler than my AxisScroller for AS2 as well.

MichaelxxOA
12-30-2006, 07:47 PM
I moved this to here:
http://www.kirupa.com/forum/showthread.php?p=2032255#post2032255

Michael

MichaelxxOA
12-30-2006, 07:49 PM
BTW the main point of that last post was code design, not the efficiency, although our code is so flexible that we can accomodate many different scenarios so adjusting efficiency is left up as an exercise for whoever is using this.

Take care, again ;)
Michael

MichaelxxOA
12-30-2006, 08:28 PM
I'm going to re-organize this soon so it's a little more clear.

Take care.
Michael

devonair
12-30-2006, 11:25 PM
I'm going to re-organize this soon so it's a little more clear.

And perhaps give it its own thread maybe? lol..

Personally, I don't find what I made all that complex. If anything, it's overly simplified. Just seems like more than it is, as it can easily handle easing, horizontal, and vertical scrolling. For example, if you change the above document class just slightly to this (just expanded the text field, moved the slider bar, and changed the ContentScroller constructor basically):

package {

import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;

import com.onebyonedesign.as3.utils.ui.ContentScroller;

public class ScrollTest extends Sprite {

private var _m:Sprite;
private var _drag:Sprite;
private var _c:Sprite;
private var _gibberish:TextField;

private var _cs:ContentScroller;

public function ScrollTest() {

stage.frameRate = 31;

init();

}

private function init():void {
// create content sprite
_c = new Sprite();
_c.x = 50;
_c.y = 50;

// create mask sprite
_m = drawRect(400, 150, 0x000000);
_m.x = 50;
_m.y = 50;

// create scrollbar sprite
_drag = drawRect(15, 15, 0x141414);
_drag.x = 50;
_drag.y = 205;


// create textfield
_gibberish = createText();
//_gibberish.x = 5;

// add the textfield to the content sprite
_c.addChild(_gibberish);

// mask the content with the mask
_c.mask = _m;

// add everything to the display list
addChild(_c);
addChild(_m);
addChild(_drag);

// create the scroller
_cs = new ContentScroller(_c, _m, _drag, 0, "horizontal", false);
}

private function createText():TextField {
var tf:TextField = new TextField();
var fmt:TextFormat = new TextFormat("_sans", 11, 0xCCCCCC);
tf.defaultTextFormat = fmt;
tf.background = true;
tf.backgroundColor = 0x141414;
tf.width = 700;
tf.height = 150;
tf.autoSize = TextFieldAutoSize.NONE;
tf.multiline = true;
tf.wordWrap = true;
tf.selectable = false;
tf.mouseEnabled = false;
tf.text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas tempus arcu at erat. Nam accumsan ultrices augue. Morbi fermentum vestibulum tellus. Pellentesque et mauris vehicula sapien bibendum fermentum. Aliquam neque massa, elementum dapibus, ullamcorper sit amet, vulputate at, mauris. Nam imperdiet. Aliquam tempus nonummy felis. Nunc blandit dolor posuere enim. Nulla pretium purus ut orci. Praesent nibh lorem, egestas vitae, mattis quis, elementum et, erat. Etiam est sem, vestibulum sed, venenatis sit amet, placerat a, massa. Nulla ullamcorper ipsum ut massa. Morbi scelerisque tortor sit amet felis. Sed nec turpis. Fusce dui dolor, vestibulum nec, lacinia quis, venenatis non, est. Ut hendrerit, dui vel fermentum aliquam, lorem sem tempor purus, ut facilisis pede diam eu mauris. Nam ultricies. Aliquam id metus.\nCum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Phasellus dui tellus, condimentum pellentesque, venenatis nec, tincidunt ac, risus. Nam metus magna, lobortis vitae, molestie quis, laoreet eu, purus. Integer pulvinar, mauris et consequat pellentesque, felis purus porttitor nisl, nec faucibus tortor massa quis elit. Duis auctor. In erat tortor, molestie non, hendrerit quis, consequat vel, arcu. Sed enim. Praesent magna lectus, tincidunt eget, dignissim sed, commodo id, massa. In neque risus, gravida et, molestie sit amet, pellentesque in, massa. Curabitur elementum vehicula mi. In feugiat libero non metus. Duis feugiat, diam ac adipiscing tristique, velit ligula consequat purus, vel varius ligula mauris vitae velit. Nunc vehicula. Morbi luctus leo. Duis id enim varius mi ultricies convallis. Vestibulum egestas porttitor augue. ";

return tf;
}

private function drawRect(w:Number, h:Number, col:uint):Sprite {
var s:Sprite = new Sprite;
s.graphics.beginFill(col);
s.graphics.drawRect(0, 0, w, h);
s.graphics.endFill();

return s;
}

}
}

you can get this: http://www.onebyonedesign.com/flash/scrolltest/horz.html

Definitely wasn't meant to be the end all be all of scrollers. In fact, it was specifically designed for a particular ap I was working on, and I just thought I'd share..

I look forward to seeing your completed ideas on the subject.. It's looking good so far (aside from the hardcoded 150)..

MichaelxxOA
12-31-2006, 04:22 PM
Hey Devonair, yeah I apologize... like I said, looking over what you had made me think was all, I didn't mean it in anyway to knock what you were doing.

Moved the post here: http://www.kirupa.com/forum/showthread.php?p=2032255#post2032255

Michael

allnaturalscott
09-15-2007, 10:15 PM
How do I compile this into a working example?

Im sorry for the newb question, but I havent played with AS3 yet and I got put on a project that needs it.

Thanks.

Rawat
10-09-2007, 10:01 AM
hi devo,

i need your help. i am new to as3. can u plz send its source file. i donot know how to use classes and other things.

thanks in advance.

andreiashu
10-15-2007, 07:03 PM
devonair, 10x a lot man !! this rocks :)

dtrace
10-18-2007, 06:50 PM
Source file would make me so happy. This scrolling and easing is one of the best i've ever seen. Sharp.

devonair
10-18-2007, 07:07 PM
There really is no source file to post - it's literally just a copy and paste example..

bhf
01-31-2008, 09:46 AM
There really is no source file to post - it's literally just a copy and paste example..

Nice scroller :)

Do you have this one with working wheel function? And maybe autoresize :)

Thanks mate

daidai
03-07-2008, 06:50 AM
when i run the example, I get the following errors:
ContentScroller.as, Line 88 - 1120: Access of undefined property _ratio.
ContentScroller.as, Line 89 - 1120: Access of undefined property _ty.
ContentScroller.as, Line 89 - 1120: Access of undefined property _ratio.
any idea what im doing wrong?

dail
03-10-2008, 12:54 AM
just add in those two properties to the scrollers private vars

private var _ratio:Number;

daidai
03-11-2008, 11:43 AM
thanks
I have struck another error:Cannot access a property or method of a null object reference.at the lines where the Listeners are effecting the stage
_dragger.stage.addEventListener(MouseEvent.MOUSE_U P, onDraggerUp, false, 0, true);would this problem be related to where Im calling the ContentScroller.as? because it is within a child, how do I point it in the right direction?
Ive added an attached simplified version of the problem...
much obliged
dai2

daidai
03-11-2008, 02:29 PM
ive been having a play and got to here, but still no luck...

dail
03-11-2008, 03:56 PM
Basically it sounds like you have not added your child sprite to the display list. Flash is just complaining that it cannot access the stage.

daidai
03-11-2008, 08:14 PM
hmm, which Sprite? I've really tried finding the bug, but no luck :(
thanks for your time

rmoylan
03-19-2008, 12:46 PM
hmm, which Sprite? I've really tried finding the bug, but no luck :(
thanks for your time


I am having the same issue. My objects are displaying, which means that they are were added to the display list, right? I think the issue is the scope of the stage, but I do not know how to get that scope. Any thoughts?

dail
03-19-2008, 04:21 PM
This is the sprite that is used by the class to get stage access;

_dragger.stage.addEventListener(MouseEvent.MOUSE_U P,onDraggerUp,false,0,true);

Although, you could access the stage via any of the visual assets this class uses.

I see in your version linked in, you are attempting to pass in a reference to the stage. Is that tracing true in your version? (I'm at work, and don't have any Flash editing tools here to test)

dail
03-21-2008, 08:22 PM
Here is a 'butchered' version of the class, that internalises the widget drawing routines and now extends Sprite, and requires that it is added to the display list. The class then uses the ADDED_TO_STAGE event to set up its listeners. I added in a CONTENT_CHANGE event that the class listens out for, enabling it to turn off its scroll widgets etc if they are not needed. Its not as flexible as the original version, but suits the need I had for it.

/**
* @author Devon O. Wolfgang
* @date 29NOV06

Changed by noponies 2008
*/
/************************************************** ********************************
USEAGE
************************************************** *********************************
var cs:ContentScroller = new ContentScroller(yourContent:Sprite,maskHeight:int, maskWidth:int);

eg;
cs = new ContentScroller(container,400,200 );
addChild(cs);
************************************************** **********************************
EVENTS
************************************************** **********************************
And to dispatch an event to the scroller class

dispatchEvent(new Event(ContentScroller.CONTENT_CHANGE, true));
************************************************** *********************************/

package {

import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.geom.ColorTransform;
import flash.display.Stage;


public class ContentScroller extends Sprite {
//static const use for listening for content change events. On hearing this
//event, the class checks to see if its scroll bar widgets are needed, if not
//it turns them off
public static const CONTENT_CHANGE:String = "contentchange";

private var _mask:Sprite;
private var _content:Sprite;
private var _dragger:Sprite;
private var _scrollerTrack:Sprite
private var _easeRate:Number = 5
private var _direction:String = "vertical"
private var maskWidth:int
private var maskHeight:int
private var scrollerOffset:int = 10
private var _bounds:Rectangle;

//rollover colour changes for scroll widgets
private var colorInfo:ColorTransform
private var oldColor:ColorTransform
private var overTint:int = 0xCCCCCC

private var _scrollValue:Number;
private var _orgY:Number;
private var _orgX:Number;
private var _ratio:Number;
private var _ty:Number;

public function ContentScroller(content:Sprite, maskWidth:int, maskHeight:int) {

_content=content;

//create the mask for the passed in content
_mask = drawRect(1, 1, 0x000000);
_mask.x = _content.x;
_mask.y = _content.y;
_mask.width = maskWidth
_mask.height = maskHeight
_content.mask = _mask;

// create scrollbar sprite
_dragger = drawRect(15, 45, 0x141414);
_dragger.x = _mask.x+_mask.width+scrollerOffset;
_dragger.y = _content.y;

//create scroller track
_scrollerTrack = drawRect(15, _mask.height, 0xAAAAAA);
_scrollerTrack.x = _mask.x+_mask.width+scrollerOffset;
_scrollerTrack.y = _content.y;

//add the scroller widgets to the display list
addChild(_mask)
addChild(_scrollerTrack)
addChild(_dragger)

//access the color transform objects of the dragger
colorInfo = _dragger.transform.colorTransform
oldColor = _dragger.transform.colorTransform

switch (_direction) {
case "horizontal" :
initHorizontal();
break;
case "vertical" :
initVertical();
break;
}

//added to stage listener
addEventListener(Event.ADDED_TO_STAGE,addToStageHa ndler);
//call the handle content change handler as part of the constructor
//routine. This will test the need for scroll widgets when the scroller is
//first instantiated.
handleContentChange()

}

//add the listeners and part of adding this class to the display list
private function addToStageHandler(event:Event):void{
addListeners();
}

//Rectangle// widgets drawing function
private function drawRect(w:Number, h:Number, col:int):Sprite {
var s:Sprite = new Sprite;
s.graphics.beginFill(col);
s.graphics.drawRect(0, 0, w, h);
s.graphics.endFill();
return s;
}

//Test content dimensions against mask dimensions. If content height or width is less than the mask height or width
//then we don't need to scroll anything and so the scroller widgets can be hidden. You can of course do something else
//with the widgets, like disable them and change their alpha etc

private function handleContentChange(event:Event = null):void {
if (_direction == "vertical" && _content.height <= _mask.height+20) {
_dragger.visible=false;
_scrollerTrack.visible =false
} else if (_direction == "horizontal" && _content.width <= _mask.width) {
_dragger.visible=false;
_scrollerTrack.visible =false
} else {
_dragger.visible=true;
_scrollerTrack.visible = true
}

}

//horizontal scrolling
private function initHorizontal():void {
_scrollValue=_mask.width - _dragger.width;
_orgX=_dragger.x;
_bounds=new Rectangle(_orgX,_dragger.y,_scrollValue,0);
}

//vertical scrolling
private function initVertical():void {
_scrollValue=_mask.height - _dragger.height;
_orgY=_dragger.y;
_bounds=new Rectangle(_dragger.x,_orgY,0,_scrollValue);
}
//add listeners
private function addListeners():void {
_dragger.addEventListener(MouseEvent.MOUSE_DOWN,on DraggerPress,false,0,true);
_dragger.addEventListener(MouseEvent.MOUSE_UP,onDr aggerUp,true,0,true);
_dragger.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver)
_dragger.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut)
_dragger.stage.addEventListener(MouseEvent.MOUSE_U P,onDraggerUp,false,0,true);
_dragger.buttonMode = true
//listen for content changes
stage.addEventListener(CONTENT_CHANGE,handleConten tChange,false,0,true);
}

//colour tinting of drag widget
private function onMouseOver(event:MouseEvent):void{
colorInfo.color = overTint;
// apply the change to the display object
_dragger.transform.colorTransform = colorInfo

}
//reset colour change of drag widget
private function onMouseOut(event:MouseEvent):void{
_dragger.transform.colorTransform = oldColor
}

private function onDraggerPress(me:MouseEvent):void {
_ratio=_dragger.y - _orgY / _scrollValue;
_ty=_orgY - _content.height - _mask.height * _ratio;
_dragger.startDrag(false,_bounds);
if (_direction == "vertical") {
_dragger.addEventListener(MouseEvent.MOUSE_MOVE,on VerticalScroll,false,0,true);
_dragger.stage.addEventListener(MouseEvent.MOUSE_M OVE,onVerticalScroll,false,0,true);
} else if (_direction == "horizontal") {
_dragger.addEventListener(MouseEvent.MOUSE_MOVE,on HorizontalScroll,false,0,true);
_dragger.stage.addEventListener(MouseEvent.MOUSE_M OVE,onHorizontalScroll,false,0,true);
}
}
private function onDraggerUp(me:MouseEvent):void {
_dragger.stopDrag();
_dragger.removeEventListener(MouseEvent.MOUSE_MOVE ,onVerticalScroll);
_dragger.stage.removeEventListener(MouseEvent.MOUS E_MOVE,onVerticalScroll);
_dragger.removeEventListener(MouseEvent.MOUSE_MOVE ,onHorizontalScroll);
_dragger.stage.removeEventListener(MouseEvent.MOUS E_MOVE,onHorizontalScroll);
}
private function onVerticalScroll(me:MouseEvent):void {
me.updateAfterEvent();
if (! _content.willTrigger(Event.ENTER_FRAME)) {
_content.addEventListener(Event.ENTER_FRAME,moveVe rtical,false,0,true);
}
}
private function onHorizontalScroll(me:MouseEvent):void {
me.updateAfterEvent();
if (! _content.willTrigger(Event.ENTER_FRAME)) {
_content.addEventListener(Event.ENTER_FRAME,moveHo rizontal,false,0,true);
}
}
private function moveVertical(e:Event):void {
var ratio:Number = (_dragger.y - _orgY) / _scrollValue;
var ty:Number=_orgY - (_content.height - _mask.height) * ratio;
var dist:Number=ty - _content.y;
var moveAmount:Number=dist / _easeRate;
_content.y+= moveAmount;
if (Math.abs(_content.y - ty) < 1) {
_content.removeEventListener(Event.ENTER_FRAME,mov eVertical);
_content.y=ty;
}
}
private function moveHorizontal(e:Event):void {
var ratio:Number = (_dragger.x - _orgX) / _scrollValue;
var tx:Number = _orgX - (_content.width - _mask.width) * ratio;
var dist:Number=tx - _content.x;
var moveAmount:Number=dist / _easeRate;
_content.x+= moveAmount;
if (Math.abs(_content.x - tx) < 1) {
_content.removeEventListener(Event.ENTER_FRAME,mov eHorizontal);
_content.x=tx;
}
}
}
}

estroina
04-22-2008, 11:16 AM
I was unable to make this work. Can you post a zip file with a small as3&fla example please?

Thanx.

estroina
04-23-2008, 10:11 AM
Got it but had to put it has AS2 not AS3.

Thanx for the excelent work!

ajcates
04-23-2008, 08:48 PM
I don't like the scroll thingy, my scroll wheel doesn't work that great.

4lex
05-10-2008, 01:06 AM
Great code, thanks for providing it.

I have a problem. The swf file that has the ScrollTest Document Class assigned works fine, but when I load that swf from another swf file, I get this error:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at ScrollTest()

Did anyone else have this problem too? Why does this happen?

Thanks!

4lex
05-11-2008, 12:22 AM
I've found the solution for that problem here: http://www.actionscript.org/resources/articles/716/2/AS3-Classes-Using-Inheritance/Page2.html

Very good tutorial with cool examples and one of them addresses exactly the problem I had. Thanks Jody Hall!

Here's the code for someone else having the same problem:



public function ScrollTest() {
addEventListener(Event.ADDED_TO_STAGE, addedListener);
}
public function addedListener(event:Event) {
removeEventListener(Event.ADDED_TO_STAGE, addedListener);
stage.frameRate = 31;

init();
}

johnlouis
05-18-2008, 12:08 PM
how do I change the height of the scroller? could I change it dynamically through AS?