RE: [flexcoders] EventListener for canvas
-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
[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 * 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