PDA

View Full Version : Event target?


panel
04-12-2007, 09:47 AM
Hi

I have created class ColorPicker. My problem is that I added 2 Sprites and all mouse events are dispatched from Sprites although i added eventlistener directly to class.
evt.target // [Object Sprite]
In class I have access to all methods, but when I am using listners outside class i can't get acces to class methods, becouse evt.target isSprite not ColorPicker


package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;

public class ColorPicker extends Sprite
{
private var _border:Sprite;
private var _colorFill:Sprite;
private var _color:uint;

public function ColorPicker(color:uint)
{
_color = color;

_border = new Sprite();
_border.graphics.beginFill(0x000000);
_border.graphics.drawRect(0,0,15,15);
_border.graphics.endFill();

_colorFill = new Sprite();
_colorFill.graphics.beginFill(color)
_colorFill.graphics.drawRect(1,1,13,13)
_colorFill.graphics.endFill();

addChild(_border);
addChild(_colorFill);

addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
}

private function onMouseOver(evt:MouseEvent):void
{
trace(evt.target) // [Object Sprite]
}


public function getcolor():String
{
return _color;
}

}
}



package
{
import flash.display.Sprite;
import ColorPicker;
import flash.events.MouseEvent;

public class Test extends Sprite
{
privare var _colorPicker:ColorPicker;

public function Test()
{
_colorPicker:ColorPicker = newColorPicker(0xFF0000);
_colorPicker.addEventListener(MouseEvent.MOUSE_OVE R, onMouseOver);
}
addChild(_colorPicker);
}

private function onMouseOver(evt:MouseEvent):void
{
trace(evt.target) // [Object Sprite]
//also Sprite not ColorPicker so can't access getColor() function
}

}
}


I tryied drawing objects directly in class wihhout using sprites. I works (trace(evt.target) // [Object ColorPicker]), but I don't have any controll over objects in class.

dr_zeus
04-12-2007, 04:36 PM
You can set the mouseChildren property to false if you want to ignore mouse events from your Sprites.

panel
04-13-2007, 08:42 AM
You can set the mouseChildren property to false if you want to ignore mouse events from your Sprites.

Didn't work

senocular
04-13-2007, 03:44 PM
use ROLL_OVER instead of MOUSE_OVER
http://www.kirupa.com/forum/showthread.php?p=1948052#post1948052

dr_zeus
04-13-2007, 05:26 PM
Didn't work

Go with senocular's solution. I forgot that mouseChildren causes Flash to completely ignore them rather than making the target their parent.

panel
04-16-2007, 09:46 AM
ROLL_OVER did the trick, but i wonder how can I do the same think for MOUSE_DOWN?

Tink
04-16-2007, 10:03 AM
use event.currentTarget

panel
04-16-2007, 10:53 AM
Thanks a lot for help :D