Hi,

Since you are listening to a container's mouseDownSomewhere or mouseDown
event, this would protect all children inside the container from receiving
mouse down event. That's why button's click event is not broadcast.

Well there are always way, eitehr you can redesign your app or do something
else. Following is one of the ways, I have modified your code to allow:

- having different way of event handling - by using handleEvent(..) function
- allowing turning off mouseDownSomewhere event - by doing bound checks and
dispatching button's click event.

I know, this is quite hacky, but I feel you can design your application in
someother way to avoid this.

##ModifiedCode##

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml"; 
width="100%" height="100%" creationComplete="onAppInit()" >
        <mx:Script>
        <![CDATA[
                //import mx.events.EventDispatcher;
                
                var xDef:Number;
                var yDef:Number;
                var i:Number=0;
                var eventListenerObject:Object;
                
                function onAppInit()
                {
                    eventListenerObject = new Object();
                    eventListenerObject.handleEvent =
mx.utils.Delegate.create(this, handleSubscribedEvents);
                    createListener();
                }
                
                function createListener()
                {
                    myCanvas.addEventListener("mouseOver",
eventListenerObject);
        
myCanvas.addEventListener("mouseDownSomewhere",eventListenerObject);
                }
                
                function handleSubscribedEvents(event)
                {
                    var eventName:String = event.type;
            
                    switch(eventName)
                    {
                        case "mouseOver":
                            handleMouseOver(event);
                        break;
                        case "mouseDownSomewhere":
                            handleMouseDown(event);
                    
                    }
                }
                
                function handleMouseDown(event:Object) {
                        var xLoc=event.target.mouseX;
                        var yLoc=event.target.mouseY;
                        var point = {x:xLoc, y:yLoc};
                        
                        myCanvas.localToGlobal(point);
                
                        xDef=xLoc;
                        yDef=yLoc;
                        
                        var l = remove_btn.x;
                        var r = remove_btn.x + remove_btn.width;
                        var t = remove_btn.y;
                        var b = remove_btn.y + remove_btn.height;
                        
                        
                        
                        if(point.x >= l && point.x <= r && point.y >= t &&
point.y <= b)
                        {
                            
                            remove_btn.dispatchEvent({type:"click"});
                            return;
                        }
                            
                            
                        
                        myCanvas.createChild(mx.controls.TextArea,
"newTxtFld"+i)
                        myCanvas["newTxtFld"+i].x=xLoc;
                        myCanvas["newTxtFld"+i].y=yLoc;
                        myCanvas["newTxtFld"+i].visible=true;
                        myCanvas["newTxtFld"+i].width=700-xLoc;
                        Selection.setFocus(myCanvas["newTxtFld"+i]);
                        
                        myCanvas.removeEventListener("mouseDownSomewhere",
handleMouseDown);
                        i++;
                }
                function handleMouseOver(event:Object){
                        var xLoc=event.target.mouseX;
                        var yLoc=event.target.mouseY;
                        var idx=event.target.index;
                        myCanvas[idx].x=xLoc;
                        myCanvas[idx].y=yLoc;
                        myCanvas[idx].width=700-xLoc;
                }
                
                function buttonPressed() {
                        myCanvas.addEventListener("mouseDownSomewhere",
eventListenerObject);
                        
                }
                function turnOffListener() {
                    
                        myCanvas.removeEventListener("mouseDownSomewhere",
eventListenerObject);
                }
                
        ]]>
        </mx:Script>
        <mx:Text width="100%" text="To start adding text, just click 
anywhere.  A new text area will be created to type into." />
        <mx:Button label="Add More Text" click="buttonPressed()" />
        <mx:Button label="Turn on Listener" click="createListener()" />
        <mx:Button id="remove_btn" label="Turn Off Listener"
click="turnOffListener()" />
        <mx:Canvas id="myCanvas" 
                width="800" height="600"
            >
</mx:Canvas>
</mx:Application>

-----Original Message-----
From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] 
Sent: Saturday, April 23, 2005 7:00 AM
To: flexcoders@yahoogroups.com
Subject: [flexcoders] EventListener for canvas



I have successfully set up some code that contains an event listener 
if I mouse down anywhere on a canvas.  From the handler, I can modify 
a text area and move it.  The code for this is shown below.  But what 
I would like to do is access the xy coordinates of the mouse and move 
the text area to those coordinates.  I am trying to access 
event.target.mouseX and event.target.mouseY.  Earlier I had some code 
that would do that successfully using <mx:canvas 
mouseDownSomewhere="runfunction()" />, but now that I have the event 
listener function set up for the mouseDownSomewhere, it won't work.

Any ideas on how to get the text area to move to where I want it to 
go?

Here is the code with the event Listener:


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml"; 
initialize="createListener();">
        <mx:Script>
        <![CDATA[
                function createListener() {
                        myCanvas.addEventListener
("mouseDownSomewhere", handleClickEvent);
                }
                
                function handleClickEvent(event:Object) {
                        myText.text="pressed";
                        myTA.text="";
                        myTA.x=100;
                        myTA.y=100;
                        myTA.visible=true;
                }
                
                function buttonPressed() {
                        var newTA=myCanvas.createChild
(mx.controls.TextArea, "", {text:"newtext",x:100,y:100});
                        Selection.setFocus(newTA);              
                }
        ]]>
        </mx:Script>
        
        <mx:Button label="Add More Text" click="buttonPressed()" />
        
        <mx:Canvas id="myCanvas">
                <mx:Text id="myText" text="Text" />
                <mx:TextArea id="myTA" visible="false" 
text="textarea" width="200"/>
        </mx:Canvas>
</mx:Application>







Here is some code that uses the mouseX and mouseY successfully, but 
doesn't include a way for me to turn off the event Listener.  
Essentially, what I want is for this code here to work while adding a 
way to turn the mousedown listener on and off via a button.
'


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml"; 
width="100%" height="100%" >
        <mx:Script>
        <![CDATA[
                //import mx.events.EventDispatcher;
                
                var xDef:Number;
                var yDef:Number;
                var i:Number=0;
                
                //private var dispatchEvent:Function;
                public var addEventListener:Function;
                public var removeEventListener:Function;

                //function myInit() {
                        //mx.events.EventDispatcher.initialize(this);
                //}

                function handleMouseDown(event:Object) {
                        var xLoc=event.target.mouseX;
                        var yLoc=event.target.mouseY;
                        xDef=xLoc;
                        yDef=yLoc;
                        
                        myCanvas.createChild
(mx.controls.TextArea, "newTxtFld"+i)
                        myCanvas["newTxtFld"+i].x=xLoc;
                        myCanvas["newTxtFld"+i].y=yLoc;
                        myCanvas["newTxtFld"+i].visible=true;
                        myCanvas["newTxtFld"+i].width=700-xLoc;
                        Selection.setFocus(myCanvas["newTxtFld"+i]);
                        
                        myCanvas.removeEventListener
("mouseDownSomewhere", handleMouseDown);
                        i++;
                }
                function handleMouseOver(event:Object){
                        var xLoc=event.target.mouseX;
                        var yLoc=event.target.mouseY;
                        var idx=event.target.index;
                        myCanvas[idx].x=xLoc;
                        myCanvas[idx].y=yLoc;
                        myCanvas[idx].width=700-xLoc;
                }
                
                function buttonPressed() {
                        myCanvas.addEventListener
("mouseDownSomewhere", handleMouseDown);
                        
                }
                function turnOffListener() {
                        myCanvas.removeEventListener
("mouseDownSomewhere", handleMouseDown);
                }
        ]]>
        </mx:Script>
        <mx:Text width="100%" text="To start adding text, just click 
anywhere.  A new text area will be created to type into." />
        <mx:Button label="Add More Text" click="buttonPressed()" />
        <mx:Button label="Turn Off Listener" click="turnOffListener
()" />
        <mx:Canvas id="myCanvas" 
                mouseOver="handleMouseOver(event)" 
                width="800" height="600">
        <!--mouseDownSomewhere="handleMouseDown(event)"-->
</mx:Canvas>
</mx:Application>


Thank you for you help!!!





 
Yahoo! Groups Links



 





 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/flexcoders/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply via email to