Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 12-18-2008, 02:16 AM   #1
krayzeebean
Senior Member
 
Join Date: Feb 2006
Posts: 682
Default get scale value from mouse position?

I'm trying to make an interactive scale tool, but having some difficulty getting the proper scale values based on the mouse position.

The first problem is if the scale starts at 100%, then the scale values properly follow the mouse movement. But if it's smaller than 100%, the scale changes faster than the mouse moves, and if it's larger than 100% the scale changes slower than the mouse movement.

The other problem I have is if I let go of the mouse button while the scale is a negative value, and then try to scale again, the scale changes are the inverse of the mouse position.

Here is the code:

Code:
package {
	
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import fl.motion.MatrixTransformer;
	import flash.geom.Rectangle;
	
	public class Scale extends Sprite {
		var xScale:Number;
		var yScale:Number;
		var xDif1:Number;
		var xDif2:Number;
		var yDif1:Number;
		var yDif2:Number;
		var scaleStartX:Number;
		var scaleStartY:Number;
		var rect:Sprite;

		
		public function Scale(){
			rect = new Sprite();
			rect.graphics.beginFill(0x990000);
			rect.graphics.drawRect(-50, -50, 100, 100);
			rect.graphics.endFill();
			rect.graphics.beginFill(0x000000);
			rect.graphics.drawRect(-45, -45, 10, 10);
			rect.graphics.endFill();
			rect.x = 275;
			rect.y = 200;
			addChild(rect);
			
			stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
		}
		
		private function mouseDownHandler(event:MouseEvent):void {
			scaleStartX = rect.scaleX;
			scaleStartY = rect.scaleY;
			xDif1 = event.stageX - rect.x;
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
		}
		
		private function mouseMoveHandler(event:MouseEvent):void {
			xDif2 = event.stageX - rect.x;
			xScale = scaleStartX + ((xDif2 / xDif1) - 1);
			yScale = 1;
			scaleItem(rect, xScale, yScale);
		}
		
		private function mouseUpHandler(event:MouseEvent):void {
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
		}
		
		private function scaleItem(item:Sprite, xscale:Number, yscale:Number):void {
			item.scaleX = xscale;
			item.scaleY = yscale;
		}
	}
}
krayzeebean is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to cause a mouse trail to change scale on mouse down only ginob ActionScript 3.0 3 04-03-2008 08:59 PM
Using Mouse Position to Control Speed acornFlyer ActionScript 2.0 2 02-15-2006 02:44 PM
_xmouse - difference in current and initial mouse position blondeh ActionScript 2.0 2 01-30-2006 02:51 PM
Stage Movement based on mouse position PM Hopper ActionScript 1.0 (and below) 4 05-06-2005 08:58 PM
Relative Mouse Position with Easing jjmancini ActionScript 1.0 (and below) 0 07-20-2004 06:38 PM


All times are GMT. The time now is 02:10 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.