A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: draw position is not where the mouse is

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    13

    draw position is not where the mouse is

    where is my mistake? Everything works fine but when I want to draw
    the line is not where the mouse is...its like 20px away from it...and i cant draw in the hole area
    can someone help me?

    Code:
    package 
    {
    	import PNGEncoder;
    	import flash.display.MovieClip;
    	import flash.display.Shape;
    	import flash.display.DisplayObject;
    	import flash.text.TextField;
    	import flash.text.TextFormat;
    	import flash.text.TextFieldType;
    	import flash.text.TextFieldAutoSize;
    	import flash.display.BitmapData;
    	import flash.geom.ColorTransform;
    	import flash.events.MouseEvent;
    	import flash.events.Event;
    	import flash.utils.ByteArray;
    	import flash.net.FileReference;
    
    	public class Main extends MovieClip
    	{
    
    		/* Variables */
    
    		/* Pencil Tool shape, everything drawed with this tool and eraser is stored inside board.pencilDraw */
    
    		var pencilDraw:Shape = new Shape();
    
    		/* Text format */
    
    		var textformat:TextFormat = new TextFormat();
    
    		/* Colors */
    
    		var colorsBmd:BitmapData;
    		var pixelValue:uint;
    		var activeColor:uint = 0x000000;
    
    		/* Save dialog instance */
    
    		var saveDialog:SaveDialog;
    
    		/* Active var, to check wich tool is active */
    
    		var active:String;
    
    		/* Shape size color */
    
    		var ct:ColorTransform = new ColorTransform();
    		
    		
    
    		 public function Main():void
    		{
    			textformat.font = "Quicksand Bold Regular";
    			textformat.bold = true;
    			textformat.size = 16;
    
    			convertToBMD();
    
    			addListeners();
    
    			/* Hide tools highlights */
    
    			pencil.visible = false;
    			hideTools(eraser, txt);
    		}
    
    		/* Pencil Tool */
    
    
    
    
    
    
    
    
    
    		 function PencilTool(e:MouseEvent):void
    		{
    			/* Quit active tool */
    
    			quitActiveTool();
    
    			/* Set to Active */
    
    			active = "Pencil";
    
    			/* Listeners */
    
    			board.addEventListener(MouseEvent.MOUSE_DOWN, startPencilTool);
    			board.addEventListener(MouseEvent.MOUSE_UP, stopPencilTool);
    
    			/* Highlight */
    
    			highlightTool(pencil);
    			hideTools(eraser, txt);
    
    			ct.color = activeColor;
    			shapeSize.transform.colorTransform = ct;
    		}
    
    		function startPencilTool(e:MouseEvent):void
    		{
    			pencilDraw = new Shape();
    			board.addChild(pencilDraw);
    
    			pencilDraw.graphics.moveTo(mouseX, mouseY);
    			pencilDraw.graphics.lineStyle(shapeSize.width, activeColor);
    
    			board.addEventListener(MouseEvent.MOUSE_MOVE, drawPencilTool);
    		}
    
    		 function drawPencilTool(e:MouseEvent):void
    		{
    			pencilDraw.graphics.lineTo(mouseX, mouseY);
    		}
    
    		 function stopPencilTool(e:MouseEvent):void
    		{
    			board.removeEventListener(MouseEvent.MOUSE_MOVE, drawPencilTool);
    		}
    
    		/* Eraser Tool */
    
    		 function EraserTool(e:MouseEvent):void
    		{
    			/* Quit active tool */
    
    			quitActiveTool();
    
    			/* Set to Active */
    
    			active = "Eraser";
    
    			/* Listeners */
    
    			board.addEventListener(MouseEvent.MOUSE_DOWN, startEraserTool);
    			board.addEventListener(MouseEvent.MOUSE_UP, stopEraserTool);
    
    			/* Highlight */
    
    			highlightTool(eraser);
    			hideTools(pencil, txt);
    
    			ct.color = 0x000000;
    			shapeSize.transform.colorTransform = ct;
    		}
    
    		 function startEraserTool(e:MouseEvent):void
    		{
    			pencilDraw = new Shape();
    			board.addChild(pencilDraw);
    
    			pencilDraw.graphics.moveTo(mouseX, mouseY);
    			pencilDraw.graphics.lineStyle(shapeSize.width, 0xFFFFFF);
    
    			board.addEventListener(MouseEvent.MOUSE_MOVE, drawEraserTool);
    		}
    
    		 function drawEraserTool(e:MouseEvent):void
    		{
    			pencilDraw.graphics.lineTo(mouseX, mouseY);
    		}
    
    		function stopEraserTool(e:MouseEvent):void
    		{
    			board.removeEventListener(MouseEvent.MOUSE_MOVE, drawEraserTool);
    
    		}
    
    		/* Text Tool */
    
    		 function TextTool(e:MouseEvent):void
    		{
    			/* Quit active tool */
    
    			quitActiveTool();
    
    			/* Set to Active */
    
    			active = "Text";
    
    			/* Listener */
    
    			board.addEventListener(MouseEvent.MOUSE_UP, writeText);
    
    			/* Highlight */
    
    			highlightTool(txt);
    			hideTools(pencil, eraser);
    		}
    
    		 function writeText(e:MouseEvent):void
    		{
    			var textfield = new TextField();
    
    			textfield.type = TextFieldType.INPUT;
    			textfield.autoSize = TextFieldAutoSize.LEFT;
    			textfield.selectable = false;
    			textfield.defaultTextFormat = textformat;
    			textfield.textColor = activeColor;
    			textfield.x = mouseX;
    			textfield.y = mouseY;
    			stage.focus = textfield;
    
    			board.addChild(textfield);
    		}
    
    		/* Save */
    
    		 function export():void
    		{
    			var bmd:BitmapData = new BitmapData(835, 263);
    
    			bmd.draw(board);
    
    			var ba:ByteArray = PNGEncoder.encode(bmd);
    
    			var file:FileReference = new FileReference();
    
    			file.addEventListener(Event.COMPLETE, saveSuccessful);
    
    			file.save(ba, "MyDrawing.png");
    		}
    
    		 function saveSuccessful(e:Event):void
    		{
    			saveDialog = new SaveDialog();
    
    			addChild(saveDialog);
    
    			saveDialog.closeBtn.addEventListener(MouseEvent.MOUSE_UP, closeSaveDialog);
    		}
    
    		 function closeSaveDialog(e:MouseEvent):void
    		{
    			removeChild(saveDialog);
    		}
    
    		 function save(e:MouseEvent):void
    		{
    			export();
    		}
    
    		/* Clear Tool */
    
    		 function clearBoard(e:MouseEvent):void
    		{
    			/* Create a blank rectangle on top of everything but board */
    //, 
    			var blank:Shape = new Shape();
    
    			blank.graphics.beginFill(0xFFFFFF);
    			blank.graphics.drawRect(0, 0, 826, 263);
    			blank.graphics.endFill();
    
    			board.addChild(blank);
    		}
    
    		/* Default colors function */
    
    		 function convertToBMD():void
    		{
    			colorsBmd = new BitmapData(colors.width,colors.height);
    			colorsBmd.draw(colors);
    		}
    
    		 function chooseColor(e:MouseEvent):void
    		{
    			pixelValue = colorsBmd.getPixel(colors.mouseX,colors.mouseY);
    			activeColor = pixelValue;//uint can be RGB!
    
    			ct.color = activeColor;
    			shapeSize.transform.colorTransform = ct;
    		}
    
    		/* Quit active function */
    
    		 function quitActiveTool():void
    		{
    			switch (active)
    			{
    				case "Pencil" :
    					board.removeEventListener(MouseEvent.MOUSE_DOWN, startPencilTool);
    					board.removeEventListener(MouseEvent.MOUSE_UP, stopPencilTool);
    				case "Eraser" :
    					board.removeEventListener(MouseEvent.MOUSE_DOWN, startEraserTool);
    					board.removeEventListener(MouseEvent.MOUSE_UP, stopEraserTool);
    				case "Text" :
    					board.removeEventListener(MouseEvent.MOUSE_UP, writeText);
    				default :
    			}
    		}
    		
    		/* Highlight active Tool */
    
    		 function highlightTool(tool:DisplayObject):void
    		{
    			tool.visible=true;
    		}
    
    		 function hideTools(tool1:DisplayObject, tool2:DisplayObject):void
    		{
    			tool1.visible=false;
    			tool2.visible=false;
    		}
    
    		/* Change shape size */
    
    		 function changeShapeSize(e:MouseEvent):void
    		{
    			if (shapeSize.width >= 20)
    			{
    				shapeSize.width = 1;
    				shapeSize.height = 1;
    
    				/* TextFormat */
    
    				textformat.size = 18;
    			}
    			else
    			{
    				shapeSize.width += 5;
    				shapeSize.height=shapeSize.width;
    
    				/* TextFormat */
    
    				textformat.size+=5;
    			}
    		}
    
    		 function addListeners():void
    		{
    			pencilTool.addEventListener(MouseEvent.MOUSE_UP, PencilTool);
    			eraserTool.addEventListener(MouseEvent.MOUSE_UP, EraserTool);
    			textTool.addEventListener(MouseEvent.MOUSE_UP, TextTool);
    			saveButton.addEventListener(MouseEvent.MOUSE_UP, save);
    			clearTool.addEventListener(MouseEvent.MOUSE_UP, clearBoard);
    			colors.addEventListener(MouseEvent.MOUSE_UP, chooseColor);
    			sizePanel.addEventListener(MouseEvent.MOUSE_UP, changeShapeSize);
    			shapeSize.addEventListener(MouseEvent.MOUSE_UP, changeShapeSize);
    		}
    	}
    }
    Attached Files Attached Files

  2. #2
    Junior Member
    Join Date
    Oct 2013
    Posts
    13
    no ideas????

  3. #3
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,145
    Are you using the mouseX and mouseY of a clip when you want to use the mouseX and mouseY of the stage?

    If you need mouse position or touch position in your flash, always use the stage x and y to keep things clear.

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