   Flash Map Scaling Constraints, please help.
  Hi all, i'm new to the forum and hoping to find an answer to my flash map dilemma. I've searched and searched and not found a solution for it and no one seems to be able to help on the various flash forums i'm a member of. Here's hoping someone on here can help :)Basically I am creating a flash map which a user can move around by dragging and also zoom in and out on. The dragging and zooming isn't the problem, keeping the map within it's boundaries on zoom out however is.I have created a map class which I have applied to a map movieclip in my library, the class contains all the functionality for the map (dragging, zooming etc), when a zoom out button is pressed my getBoundariesAfterScale() function is called and this returns an object with the predicted boundaries of the map after the scale. I now have the information i need to check whether the map has left the boundaries of the mask. This is where I am having trouble though, if I find that the leftExtents of the map is greater than the leftExtents of the mask I should offset the map when I scale, but then i have to check the rightExtents aswell, I've tried various combinations but can't quite get it right, could someone please show me what the best method is for this or where i am going wrong?Here is my code:Map.as:import flash.geom.Rectangle;class Map extends MovieClip {/* //////////////////////////////////////////// *//* //////    Define Private Variables   /////// *//* //////////////////////////////////////////// */private var scaleFactor:Number;private var boundaryRect:Rectangle;private var newBoundariesMask:MovieClip;/* //////////////////////////////////////////// *//* //////    Define Public Functions    /////// *//* //////////////////////////////////////////// */public function Map() {}public function init(boundaryMc:MovieClip,scaleFactor:Number) {// Initialise variablesthis.scaleFactor = scaleFactor;// Set offsets so we can scale from the center of the mapvar xOffset:Number = this._width / 2;var yOffset:Number = this._height / 2;// Set the area in which the map can be movedvar leftExtents:Number = boundaryMc._x; // - (this._width /2) + boundaryMc._width;var topExtents:Number = boundaryMc._y; // - (this._height /2) + boundaryMc._height;var rightExtents:Number = boundaryMc._x + boundaryMc._width;var bttmExtents:Number = boundaryMc._y + boundaryMc._height;// Setup boundary rectangleboundaryRect = new Rectangle(leftExtents,topExtents,rightExtents,bttmExtents);trace("Initialising map...");trace("Map boundaries:")trace("x: "+boundaryRect.x);trace("y: "+boundaryRect.y);trace("width: "+boundaryRect.width);trace("height: "+boundaryRect.height);trace("Scale Factor: "+scaleFactor);trace("");// Center map within boundariescenter();}public function onMouseDown() {this.startDrag(false,boundaryRect.x,boundaryRect.y,boundaryRect.width,boundaryRect.height);}public function onMouseUp() {this.stopDrag();}public function zoomIn() {}public function zoomOut() {trace("Zooming out...");// Get new boundaries so we can check and make adjustments to positionvar newBoundaries:MovieClip = getBoundariesAfterScale(-10);trace("Map boundaries:")trace("x: "+boundaryRect.x);trace("y: "+boundaryRect.y);trace("width: "+boundaryRect.width);trace("height: "+boundaryRect.height);trace("");trace("New map boundaries:")trace("x: "+newBoundaries.x);trace("y: "+newBoundaries.y);trace("width: "+newBoundaries.width);trace("height: "+newBoundaries.height);trace("Scale Factor: "+scaleFactor);trace("");var xDiff:Number = 0;var yDiff:Number = 0;var xWidthDiff:Number = 0;var yHeightDiff:Number = 0;var scale:Boolean = false;// Check boundaries and create differences// Check if left and right border are within the mask boundariesif(newBoundaries.x > boundaryRect.x && newBoundaries.x + (newBoundaries.width / 2) < boundaryRect.width) {trace("Left and right borders of map will not be within the mask boundary limits, do not scale.");scale = false;}else if(newBoundaries.x > boundaryRect.x && newBoundaries.x + (newBoundaries.width / 2) > boundaryRect.width) {trace("Left border will not be within the mask boundaries limits, right border will be, creating offset.");xDiff = boundaryRect.x - newBoundaries.x;trace("xOffset = "+xDiff);// Check right side after x offset.if(newBoundaries.x + newBoundaries.width + xDiff > boundaryRect.width) {scale = true;}else {scale = false;}}else if(newBoundaries.x < boundaryRect.x && newBoundaries.x + newBoundaries.width > boundaryRect.width) {trace("Both borders will be within boundary limits, ok to scale.");scale = true;}/*if(newBoundaries.x + (newBoundaries.width / 2) > boundaryRect.width) {xWidthDiff = boundaryRect.width - (newBoundaries.x + newBoundaries.width);}if(newBoundaries.y < boundaryRect.y) {yDiff = boundaryRect.y - newBoundaries.y;}if(newBoundaries.y + (newBoundaries.height / 2) > boundaryRect.height) {yHeightDiff = boundaryRect.height - (newBoundaries.y + newBoundaries.height);}*/if(scale) { // Scalethis._width = scaleByPercent(this._width,-10);this._height = scaleByPercent(this._height,-10);// Move map based on offsetsthis._x = this._x - xWidthDiff;this._y = this._y - yHeightDiff;}}public function mapZoomOut() {this._xscale = this._xscale - scaleFactor;this._yscale = this._yscale - scaleFactor;}/* //////////////////////////////////////////// *//* //////    Define Private Functions   /////// *//* //////////////////////////////////////////// */private function center() {this._x = (boundaryRect.x + boundaryRect.width) / 2;this._y = (boundaryRect.y + boundaryRect.height) / 2;}private function getBoundariesAfterScale(pScaleFactor:Number) {trace("Generating new boundaries after scale with scale factor "+pScaleFactor);var maskBoundaries:Rectangle = new Rectangle();maskBoundaries.x = (this._width/2)*-1;maskBoundaries.y = (this._height/2)*-1;maskBoundaries.width = scaleByPercent(this._width/2,pScaleFactor);maskBoundaries.height = scaleByPercent(this._height/2,pScaleFactor);// Get Offsetsvar xOffset:Number = (this._width - scaleByPercent(this._width,pScaleFactor)) / 4;var yOffset:Number = (this._height - scaleByPercent(this._height,pScaleFactor)) / 4;// Create background inside new boundary mc (important so we have something to scale)var predictionMask:MovieClip = _root.createEmptyMovieClip("predictionMask",10,{_x:0,_y:0});predictionMask.beginFill(0x0000FF);predictionMask.linestyle(0, 0x0000FF, 100, true, "none", "round", "miter", 1);predictionMask.moveTo(maskBoundaries.x, maskBoundaries.y);predictionMask.lineTo(maskBoundaries.width, maskBoundaries.y);predictionMask.lineTo(maskBoundaries.width, maskBoundaries.height);predictionMask.lineTo(maskBoundaries.x, maskBoundaries.height);predictionMask.moveTo(maskBoundaries.x, maskBoundaries.y);predictionMask.endFill();predictionMask._alpha = 50;predictionMask._x = this._x + xOffset;predictionMask._y = this._y + yOffset;// Create rectangle instance of new boundaries (relative to root)var newBoundaries:Rectangle = new Rectangle();newBoundaries.x = predictionMask._x - (predictionMask._width / 2);newBoundaries.y = predictionMask._y - (predictionMask._height / 2);newBoundaries.width = predictionMask._width;newBoundaries.height = predictionMask._height;/*New map boundaries:x: -621y: -932width: 558.9height: 838.8Scale Factor: 10*/return newBoundaries;}private function scale(value:Number,pScaleFactor:Number) {return (value * scaleFactor);}private function scaleByPercent(sValue:Number,pScaleFactor:Number) {sValue = sValue + ((sValue / 100) * pScaleFactor);trace("Scaled value: "+((sValue / 100) * pScaleFactor));return sValue;}private function clamp(low:Number,value:Number,high:Number) {// Clamp a value between a low and high valuereturn Math.min(Math.max(value, low), high);}private function clampPosition() {// Clamp position within boundariesthis._x = clamp(boundaryRect.x,this._x,boundaryRect.width);this._y = clamp(boundaryRect.y,this._y,boundaryRect.height);}}Map.fla// Add mapthis.attachMovie("boundary","boundary",2,{_x:0,_y:0});this.attachMovie("map","map_mc",1,{_x:0,_y:0}).init(this.boundary,10);this.attachMovie("controls","controls",3,{_x:10,_y:(this.boundary._height+10)}).init(this.boundary,10);// Add button behavioursthis.controls.zoom_in_btn.btn.onPress = function() {this._parent._parent._parent.map_mc.zoomIn();}this.controls.zoom_out_btn.btn.onPress = function() {this._parent._parent._parent.map_mc.zoomOut();}this.controls.map_zoom_out_btn.btn.onPress = function() {this._parent._parent._parent.map_mc.mapZoomOut();}Thanks in advance,eb_dev[Edited by - eb_dev on April 28, 2009 6:50:39 AM]