I believe this example is available in my earlier tutorials fro Actionscript 2, but in order to simplify and to make it easier to be found, I have made a new one in Actionscript 3.
package org.paazio {
import flash.display.*;
import flash.events.*;
import flash.ui.Keyboard;
public class Scrolling extends Sprite {
private var maskShape:Shape;
public var content:Sprite;
public var step:Number;
/**
*
* @param stuff The stuff to be scrolled
* @param step Amount of pixels for each step by keyboard or mousewheel
* @param w Width of the mask
* @param h Height of the mask
*/
public function Scrolling(stuff:DisplayObject, step:Number = 5, w:Number = 100, h:Number = 150) {
this.step = step;
maskShape = new Shape();
maskShape.graphics.beginFill(0x000000);
maskShape.graphics.drawRect(0, 0, w, h);
maskShape.graphics.endFill();
this.addChild(maskShape);
content = new Sprite();
content.name = "content";
content.addChild(stuff);
content.mask = maskShape;
this.addChild(content);
this.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
this.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
private function onMouseWheel(evt:MouseEvent):void {
content.y += evt.delta / 2 * step;
checkLimits();
}
private function onKeyUp(evt:KeyboardEvent):void {
if (evt.keyCode == Keyboard.UP) {
content.y -= step;
}
else if (evt.keyCode == Keyboard.DOWN) {
content.y += step;
}
checkLimits();
}
/**
* Checks for the assumed limits and resets to them if overlapped.
*/
private function checkLimits():void {
if (content.y > 0) {
content.y = 0;
}
else if (content.y < (maskShape.height - content.height)) {
content.y = maskShape.height - content.height;
}
stage.invalidate();
}
}
}
Please note that I have left one check out from the "checkLimits" function. It might sometimes happen that the mask is taller than the content...






















