PDA

View Full Version : creating custom mouse method on a movieclip


Vikana
12-09-2005, 02:36 PM
Hi

I wonder if you can help. I want to create a custom method for a mouse event handler attached to a movie clip on the stage,

Please look at sample code

ActionScript Code:


/** test class to show what I need to get custom
* mouse methods working
*/

class MouseListener {

// holder for the mouse object

private var mouse_mc:MovieClip;

// constructor

public function MouseListener(target:MovieClip, depth:Number){

mouse_mc = target.createEmptyMovieClip("mouse" + depth, depth);

drawGraphic();
createMouse();

}

// creates the event handler

public function createMouse():Void{

mouse_mc.onRelease = function(){
test1();
}
}

// custom method I want to run

public function test1():Void{
trace("test2");
}

// creates the image

public function drawGraphic():Void{
// clears the image first
mouse_mc.clear();
// use a 1-point line for outline
mouse_mc.lineStyle(1, 0xBAD9EF);
mouse_mc.moveTo(0,0);
// start fill
mouse_mc.beginFill(0xBAD9EF, 100);
// draw the border of the box
mouse_mc.lineTo(50, 0);
mouse_mc.lineTo(50,50);
mouse_mc.lineTo(0, 50);
mouse_mc.lineTo(0,0);
// end the fill
mouse_mc.endFill();
}
}


test1 method will not run, could anybody please give me some pointers on what I am doing wrong.

Thanks

Vikana

Xeef
12-10-2005, 05:07 AM
public function createMouse():Void{
mouse_mc.onRelease = function(){
//you are here in the scope of "mouse_mc" and not in the class
test1();
}
}



there are many ways to come around this (each has it back and forth)


public function MouseListener(target:MovieClip, depth:Number){
mouse_mc = target.createEmptyMovieClip("mouse" + depth, depth);
mouse_mc.CLASS=this
drawGraphic();
createMouse();
}
public function createMouse():Void{
mouse_mc.onRelease = function(){
this.CLASS.test1();
}
}




public function createMouse():Void{
mouse_mc.onRelease = function(){
MouseListener.test1();
}
}
static function test1():Void{
trace("test2");
}




public function MouseListener(target:MovieClip, depth:Number){
mouse_mc = target.createEmptyMovieClip("mouse" + depth, depth);
drawGraphic();
createMouse();
}
public function createMouse():Void{
mouse_mc.onRelease = mx.utils.Delegate.create(this, test1);
}
}

Vikana
12-10-2005, 10:58 AM
Once again, I get lost, I visit this forum and get found again

xeef, your a gentleman, and thank you for your assistance.

Vikana :cool: