A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: listening to mouseEvent.CLICK on an empty clip

  1. #1
    FK founder & general loiterer Flashkit's Avatar
    Join Date
    Feb 2000
    Location
    Sydney
    Posts
    1,149

    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.
    Regards Mark Fennell - Flash Kit Founder, general loiterer
    -------------------------------
    I Hate Zombies - iPhone Game | markfennell.com

  2. #2
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    from my understanding it has to be stage.
    lather yourself up with soap - soap arcade

  3. #3
    formerly hooligan2001 :) .hooligan's Avatar
    Join Date
    Mar 2008
    Posts
    405
    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?

  4. #4
    Senior Member
    Join Date
    Mar 2008
    Posts
    301
    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.CLICKclickHandler);

    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.stage.stageWidth*Math.random();
        
    circ.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.CLICKclickHandler);

    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.stage.stageWidth*Math.random();
        
    circ.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.

  5. #5
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Add container to stage and fill it with color that has 0 alpha. It isnt visible but can detect mouse clicks.

  6. #6
    formerly hooligan2001 :) .hooligan's Avatar
    Join Date
    Mar 2008
    Posts
    405
    Quote 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 ?

  7. #7
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Well, I am not sure what Mark is trying to do, maybe Stage has already other click listener.

  8. #8
    formerly hooligan2001 :) .hooligan's Avatar
    Join Date
    Mar 2008
    Posts
    405
    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?

  9. #9
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    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.

  10. #10
    Senior Member
    Join Date
    Mar 2008
    Posts
    301
    I think in terms of performance an invisible mc is better than an mc with alpha 0. Is this true?

  11. #11
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    But if mc is invisible then it does not detect mouse clicks. With alpha=0 it is still there.

  12. #12
    Senior Member
    Join Date
    Mar 2008
    Posts
    301
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center