RSS
email

Detecting MouseWheel in Flash8



Some time we need to scroll MovieClip using MouseWheel , here it is feel free to use ..

file structure(same location)

-MouseWheel.as

-TestmouseWheel.fla


Step1: open New ActionScript class file
Paste the code below

//////////code starts
/////////////////////////////////////////////
// http://sara-intop.blogspot.com //
/////////////////////////////////////////////


//importing the required class
import mx.utils.Delegate;
class MouseWheel extends MovieClip {
/////////////////////////////////////////////
// private variables
//
private var __target:MovieClip;
private var _mouseLis:Object;
/////////////////////////////////////////////
// Constructor function
//
public function MouseWheel(target:MovieClip) {
this.__target = target;
this._mouseLis = new Object();
Initiate();
}
/////////////////////////////////////////////
// private function
//
private function Initiate():Void {
//Using Delegate Class to create listener function, to get class scope to Listener function
_mouseLis.onMouseWheel = Delegate.create(this, DetectMouse);
//adding Listener to mouse
Mouse.addListener(this._mouseLis);
}
//@Param delta contains +3 to -3 depends on the mouse wheel direction
private function DetectMouse(delta):Void {
//Detecting Mouse is over the target MovieClip
if (this.__target.hitTest(_root._xmouse, _root._ymouse, true)) {
this.__target._y += delta;
}
}
//DisposeListener Method to remove listener Objects
public function DisposeListener(_object:MouseWheel):Void{
Mouse.removeListener(_object._mouseLis);
delete _object._mouseLis;
}
}

//////////code ends


Note: save file as MouseWheel.as

Step2: Open New Flash document , Save as TestmouseWheel.fla

Step3: Draw any Rectangular shapes and conver them to MovieClip

Step4: Give any istance name to the MovieClip

Step5: Add the code below in first frame

//////////code starts
var testMouse:MouseWheel = new MouseWheel(_mc);
//////////code ends

Note: _mc is the instance name given in Step4



Code Expanation:


class MouseWheel extends MovieClip {

MouseWheel is the Class name should be same as file name that inherits properties and methods
from MovieClip Class


public function MouseWheel(target:MovieClip) {
this.__target = target;
this._mouseLis = new Object();
Initiate();
}


A Constructor function Will have same name as Class name and File name, will be initiated
when an Object is instatiated from the Class
Here we are passing MovieClip for which we need scroll function
Also we associate the values with the Object
Here this means respective Object of MouseWheel Class


private function Initiate():Void {
//Using Delegate Class to create listener function, to get class scope to Listener function
_mouseLis.onMouseWheel = Delegate.create(this, DetectMouse);
//adding Listener to mouse
Mouse.addListener(this._mouseLis);
}


In Initiate Method we are addListener to Mouse trough Delegate Class
We are using Delegate class to make listener function to class scope means if we dont use Delegate Class
then the this inside listener function denotes listener function itself not the MouseWheel Object


//@Param delta contains +3 to -3 depends on the mouse wheel direction
private function DetectMouse(delta):Void {
//Detecting Mouse is over the target MovieClip
if (this.__target.hitTest(_root._xmouse, _root._ymouse, true)) {
this.__target._y += delta;
}
}

We ar checking If Mouse position is over the MovieClip we are going to scroll


public function DisposeListener(_object:MouseWheel):Void{
Mouse.removeListener(_object._mouseLis);
delete _object._mouseLis;
}

We can Call this Method once we dont need the Listener to listen mouse wheel
in fla

var testMouse:MouseWheel = new MouseWheel(_mc2);
_btn.onRelease = function() {
testMouse.DisposeListener(testMouse);
};

Here we are disposing any listener

Bookmark and Share

0 comments:

 

Recent Posts

Recent Visitors

Donate Me

About Me

My photo
Chennai, Tamil nadu, India
Nothing more to say about me.. Just a Action Script programmer / Flex developer having 4.5 years of experience.