マウスホイールの実験
マウスホイールでのページスクロールを無理やりとめる。
簡単で安易な実装
@author jc at bk-zen.com
/**
* Copyright bkzen ( http://wonderfl.net/user/bkzen )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/laf5
*/
package
{
import com.bit101.components.Label;
import com.bit101.components.Style;
import flash.display.Sprite;
import flash.events.Event;
/**
* マウスホイールでのページスクロールを無理やりとめる。
* 簡単で安易な実装。
*
* @author jc at bk-zen.com
*/
[SWF (backgroundColor = "0xFFFFFF", frameRate = "30", width = "465", height = "465")]
public class ScrollTest extends Sprite
{
public function ScrollTest()
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e: Event = null): void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
//
Style.fontName = "Verdana";
Style.embedFonts = false;
Style.fontSize = 12;
new Label(this, 50, 25, "普通の Sprite");
var normalSp: Sprite = new Sprite();
normalSp.graphics.beginFill(0xCCCCCC);
normalSp.graphics.drawRect(0, 0, 100, 200);
normalSp.graphics.endFill();
normalSp.x = 50, normalSp.y = 50;
new Label(this, 200, 25, "マウスホイールをロックした Sprite");
var lockSp: ScrollLockSprite = new ScrollLockSprite(100, 200);
lockSp.graphics.beginFill(0xAAAAAA);
lockSp.graphics.drawRect(0, 0, 100, 200);
lockSp.graphics.endFill();
lockSp.x = 200, lockSp.y = 50;
addChild(normalSp);
addChild(new DummyScrollBar(normalSp));
addChild(lockSp);
addChild(new DummyScrollBar(lockSp));
}
}
}
import flash.display.Graphics;
import flash.display.InteractiveObject;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
class DummyScrollBar extends Shape
{
private var target: InteractiveObject;
function DummyScrollBar(target: InteractiveObject)
{
this.target = target;
var g: Graphics = graphics;
g.beginFill(0);
g.drawRect(0, 0, 20, 30);
x = target.x + target.width;
y = target.y;
target.addEventListener(MouseEvent.MOUSE_WHEEL, onWheel);
}
private function onWheel(e: MouseEvent): void
{
var d: int = e.delta;
var t: int = this.y - d;
if (t < target.y) this.y = target.y;
else if (t > target.y + target.height - height) this.y = target.y + target.height - height;
else this.y = t;
}
}
class ScrollLockSprite extends Sprite
{
private var txt: TextField;
private var firstV: int;
function ScrollLockSprite(w: int, h: int)
{
addChild(txt = new TextField());
var i: int, n: int = h / 6;
for (i = 0; i < n; i ++ )
{
txt.appendText(i + "\n");
}
txt.width = w, txt.height = h;
firstV = txt.scrollV = h / 12;
txt.selectable = false;
txt.alpha = 0;
txt.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
}
private function onMouseWheel(e: MouseEvent): void
{
txt.scrollV = firstV;
}
}