|
-
FK founder & general loiterer
listening to mouseEvent.CLICK on an empty clip
is there a way to listen to a MouseEvent.CLICK event on an empty MovieClip...
essentially I want to listen to mouse click events on a container thats not the stage to then add objects to it...
basically I want this to work....
Code:
var clip = new Sprite()
addChild(clip)
var container = new Container()
clip.addEventListener(MouseEvent.CLICK, clickHandler)
function clickHandler(e:MouseEvent)
{
trace("CLICKED")
}
I can listen to the stage and get the same functionality... but that somehow feels dirty.
-
M.D.
from my understanding it has to be stage.
-
formerly hooligan2001 :)
Yeah I was playing around with this, I stick with stage. I'm not sure if it goes by the hit area of the clip?
-
I don't really understand the question... Why can't you do what you're asking about doing? It works fine. For example:
PHP Code:
var clip = new Sprite(); addChild(clip); clip.name = "clip";
clip.addEventListener(MouseEvent.CLICK, clickHandler);
for (var i=0;i<20;i++) { var circ = clip.addChild(new Sprite); circ.name = "circ"+i; circ.graphics.beginFill(Math.floor(0xFFFFFF*Math.random())); circ.graphics.drawCircle(0,0,Math.random()*20+2); circ.graphics.endFill(); circ.x = stage.stageWidth*Math.random(); circ.y = stage.stageHeight*Math.random(); }
function clickHandler(e:MouseEvent) { trace("CLICKED",e.target.name,e.currentTarget.name) }
If you click on any of the circles, it traces "CLICKED", the circle, and clip.
MouseEvent.CLICK only fires when you click on the thing that you have added the listener to. So if there's nothing in "clip", there's nothing to click on. If you put something in to clip, and then click on that, the event fires.
If you really want it to register clicks anywhere on stage, you can hax it pretty easy like this, but it feels dirtier than using the stage to me:
PHP Code:
var clip = new Sprite(); addChild(clip); clip.name = "clip";
clip.addEventListener(MouseEvent.CLICK, clickHandler);
var bg = clip.addChild(new Sprite); bg.name = "bg"; bg.graphics.beginFill(0); bg.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight); bg.graphics.endFill(); clip.hitArea = bg; bg.visible = false;
for (var i=0;i<20;i++) { var circ = clip.addChild(new Sprite); circ.name = "circ"+i; circ.graphics.beginFill(Math.floor(0xFFFFFF*Math.random())); circ.graphics.drawCircle(0,0,Math.random()*20+2); circ.graphics.endFill(); circ.x = stage.stageWidth*Math.random(); circ.y = stage.stageHeight*Math.random(); }
function clickHandler(e:MouseEvent) { trace("CLICKED",e.target.name,e.currentTarget.name) }
Is this what you were asking?
Last edited by WesIsGood; 09-25-2008 at 01:12 AM.
-
Senior Member
Add container to stage and fill it with color that has 0 alpha. It isnt visible but can detect mouse clicks.
-
formerly hooligan2001 :)
 Originally Posted by tonypa
Add container to stage and fill it with color that has 0 alpha. It isnt visible but can detect mouse clicks.
Would that be recommended over simply placing the listener on the stage ?
-
Senior Member
Well, I am not sure what Mark is trying to do, maybe Stage has already other click listener.
-
formerly hooligan2001 :)
Ow sorry I just meant in general terms would you advise putting the listener on the stage or on the invisible clip? Would having a clip with the fill colour alpha cause any performance issues? Does that still have to render to the screen?
-
Senior Member
I usually have main game mc and that where I place my listeners too so I dont need extra clip.
Invisible clip itself should not be much of perfomance issue, its only 1 clip with 1 rectangle drawn in it.
-
I think in terms of performance an invisible mc is better than an mc with alpha 0. Is this true?
-
Senior Member
But if mc is invisible then it does not detect mouse clicks. With alpha=0 it is still there.
-
But you can set the container MC's hitArea equal to the invisible MC, and it will detect mouse clicks even though it is invisible, like I did in my second code example.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|