A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: add a MOUSE_UP event listener to the stage from a class??

  1. #1
    Senior Member
    Join Date
    Apr 2000
    Location
    Minneapolis
    Posts
    2,127

    add a MOUSE_UP event listener to the stage from a class??

    Hi,
    in my class I am trying to add a MOUSE_UP event listener to the stage.
    How can I do this?

    Thanks, Mark

    PHP Code:
    //when I do this in the main constructor of my class
    Stage.addEventListener(MouseEvent.MOUSE_UPh_draggable_mouseUp);

    //I get the error
    1061Call to a possibly undefined method addEventListener through a reference with static type Class.
    Stage.addEventListener(MouseEvent.MOUSE_UPh_draggable_mouseUp); 

  2. #2
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    in AS3 it's "stage" not "Stage", and it's a property - so it won't exist unless it's on the display list. If that class is a document class, which has access to it's stage property, you're OK as it is - if it's not, and you will be instantiating this class, you'll need to do that in an addedToStage event listener, e.g.,
    PHP Code:
    private function setupStageListener(event:Event):void{
    stage.addEventListener("mouseUp",h_draggable_mouseUp);
    removeEventListener("addedToStage",arguments.callee);
    }
    addEventListener("addedToStage",setupStageListener); 

  3. #3
    Senior Member
    Join Date
    Apr 2000
    Location
    Minneapolis
    Posts
    2,127
    Hi,
    thanks for the reply.
    I may need a bit more help. A bit out of my actionscript depth.
    The class is not the document class.
    I added this to the class constructor
    PHP Code:
     addEventListener("addedToStage",setupStageListener); 
    then I added this function to the class
    PHP Code:
        private function setupStageListener(event:Event):void
            
    stage.addEventListener("mouseUp",h_draggable_mouseUp); 
            
    removeEventListener("addedToStage",arguments.called);
        } 
    in my mouse up function I put a trace
    PHP Code:
        private function h_draggable_mouseUp(evt:Event):void {
        
    trace("MOUSE UP");
          
    dragTarget null;
        } 
    I do not see the trace.
    Thanks again
    Mark

  4. #4
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    is the class a display object, and did you add it to the stage? the addedToStage event won't fire until the object is in the display list. if it's not a display object, then it won't have a stage property, so you'll have to pass it that property somehow...

  5. #5
    Senior Member
    Join Date
    Apr 2000
    Location
    Minneapolis
    Posts
    2,127
    I have 3 classes.
    PpGraphics
    PpObject
    and Main which is my document class.

    In main I have
    PHP Code:
    public class Main extends PpGraphics {
        
        private var 
    A:PpObject,B:PpObject,C:PpObject,D:PpObject,E:PpObject
    I am guessing, sorry I am out of my depth, that PpGraphics is not a display object, but PpObject is.
    You can see all the code I started with here
    http://www.kynd.info/library/mathand...dotProduct_01/

    I am trying to add the event listener for mouse up from "protected function enableDrag" to the stage so that it works outside the dot mc's, as I have added several more and restricted their movement.

    mark

  6. #6
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    ppObject extends Point - not a display object
    ppGraphic extends Sprite - which is a display object
    Main extends ppGraphic - which is a display object

    Since main is your document class, you can access it's stage property directly and immediately:
    PHP Code:
    // add this to the constructor
    stage.addEventListener("mouseUp",h_draggable_mouseUp); 

  7. #7
    Senior Member
    Join Date
    Apr 2000
    Location
    Minneapolis
    Posts
    2,127
    I added that to the main class
    last thing in the constructor.
    Was that all I had to add? No other code anywhere?

    My constructor in main now looks like this
    PHP Code:
        public function Main() {

          
    origin(stage.stageWidth 2,stage.stageHeight 2);
          
    = new PpObject(100,0);
          
    = new PpObject(0,150);
          
    = new PpObject(-200,0);
          
    = new PpObject(0,-150);
          
    = new PpObject(0,0);

          
    circle(A,8,C_DRAGGABLE"A");//draw circle -- (point var, radius, C_DRAGGABLE is a color, label text);
          
    circle(B,8,C_DRAGGABLE"B");
          
    circle(C,8,C_DRAGGABLE"C");
          
    circle(D,8,C_DRAGGABLE"D");
          
    circle(E,8,C_DRAGGABLE"E");
          
          
    path(A,BC_MOVABLE);//draw line -- path(point 1, point 2, C_MOVABLE is a color);
          
    path(B,CC_MOVABLE);
          
    path(C,DC_MOVABLE);
          
    path(D,AC_MOVABLE);
          
    path(A,CC_MOVABLE);
          
    path(B,DC_MOVABLE);

          
    enableDrag(A,B,C,D,E);

         
    // add this to the constructor
        
    stage.addEventListener("mouseUp",h_draggable_mouseUp);
        } 
    it throws this error in reference to that line in main.as
    1178: Attempted access of inaccessible property h_draggable_mouseUp through a reference with static type Main.

  8. #8
    Senior Member Awoogamuffin's Avatar
    Join Date
    Nov 2008
    Posts
    208
    That last error means that you haven't defined a function called h_draggable_mouseUp(). Where is that function?
    Check out my blog showing the development of my flash game, the Dregs of War

  9. #9
    Senior Member
    Join Date
    Apr 2000
    Location
    Minneapolis
    Posts
    2,127
    Hi,
    that function is in a class called PpGraphics.
    My document class Main.as extends PpGraphics which extends Sprite.

    your question gave me the clue
    changed h_draggable_mouseUp() from private function to protected function.
    works now

    thanks everyone.
    Mark

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