3 Replies - 682 Views - Last Post: 14 April 2011 - 07:12 AM

#1 Chopster  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 134
  • Joined: 29-March 08

Stage area seems to jump when drawRect() onto map.

Posted 14 April 2011 - 06:35 AM

Hi Guys/gals,

I have a strange problem with my flash program concerning the stage/background image. Let me shed some light on the project first.

The project is simply mapping and drawing rectangles onto a map using the Mercator Projection. All the plotting points is accurate, so the math involved is correct. But come to run time and something quite strange happens. I have setup a timer to run the plotting after 5 seconds of runtime.

When this 5 seconds is up it draws the (in this example) 2 rectangles onto the stage (over my background map image). But the whole background image (along with the points) seems to move nearly completely off the stage itself. It sort of jumps to the right. Please see before and after screenshots. This happens when the plotting drawRect() is run.

Please find my complete code below.

package {
	
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.text.*;
	import flash.utils.*;
	//import LoadText;

	
	public dynamic class MercatorProjection extends MovieClip {
		
		var dot_size = 4;
		var longitude_shift = 0;   // number of pixels your map's prime meridian is off-center.
		var x_pos = 54;
		var y_pos = 0;
		var map_width = 774;
		var map_height = 600;
		var half_dot = dot_size / 2
		
		public function MercatorProjection() {
			
			var timer:Timer = new Timer(5000, 1); // 5 seconds
			timer.addEventListener(TimerEvent.TIMER, onTimer);
			timer.start()
		}
		
		public function onTimer(evt:TimerEvent) :void {
			
			trace("Timer Triggered"); //output a message so you know its working
			plot_point(54, -2, 1);
			plot_point(37, 127.5, 2);
		}
			
		public function draw_point(xp, yp, id) {
			
			var dot:String = id;
			this[dot] = new Sprite;
			this[dot].graphics.clear();
			this[dot].graphics.beginFill(0xFF0000);
			this[dot].graphics.drawRect(0, 0, dot_size, dot_size);
			this[dot].x = xp;
			this[dot].y = yp;		
			this[dot].graphics.endFill();
			addChild(this[dot]);
		}
			
		public function plot_point(lat, lng, id) {
   			
			// Mercator projection
		   	// longitude: just scale and shift
    		x = (map_width * (180 + lng) / 360 + longitude_shift) % map_width;
		    // latitude: using the Mercator projection
    		lat = lat * Math.PI / 180;  // convert from degrees to radians
    		y = Math.log(Math.tan((lat/2) + (Math.PI/4)));  // do the Mercator projection (w/ equator of 2pi units)
    		y = (map_height / 2) - (map_width * y / (2 * Math.PI)) + y_pos;   // fit it to our map
			trace(lat, lng, id, x, y);
		   	draw_point(x - half_dot, y - half_dot, id);
		}
	}
}


Please let me know if you have any questions

Many Thanks

Jase

Attached image(s)

  • Attached Image
  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Stage area seems to jump when drawRect() onto map.

#2 lordofduct  Icon User is online

  • I'm a cheeseburger
  • member icon


Reputation: 2078
  • View blog
  • Posts: 4,081
  • Joined: 24-September 10

Re: Stage area seems to jump when drawRect() onto map.

Posted 14 April 2011 - 07:03 AM

first, you could have asked this in your other thread related to the same problem to an extent... just as a continuation.


As for your problem, look at this part inside your plot_point method:

    		x = (map_width * (180 + lng) / 360 + longitude_shift) % map_width;

...

    		y = Math.log(Math.tan((lat/2) + (Math.PI/4)));
    		y = (map_height / 2) - (map_width * y / (2 * Math.PI)) + y_pos;



What are these 'x' and 'y' variables? Where is their scope? It looks to be 'this.x' and 'this.y' as you don't define any variables of those names in the function. Well these 'x' and 'y' variables are the translation properties of the 'root' (your code is in your DocumentClass, your DocumentClass is the type your root will take), so yeah... you moved the 'root' (or what you referred to as the 'stage').
Was This Post Helpful? 1
  • +
  • -

#3 lordofduct  Icon User is online

  • I'm a cheeseburger
  • member icon


Reputation: 2078
  • View blog
  • Posts: 4,081
  • Joined: 24-September 10

Re: Stage area seems to jump when drawRect() onto map.

Posted 14 April 2011 - 07:09 AM

let's separate the utility of Mercator conversion from the actual positioning. Something like:

	public function mercatorConversion(lat:Number, lng:Number):Point
	{
		//longitude: just scale and shift
		var dx:Number = (map_width * (180 + lng) / 360 + longitude_shift) % map_width;

		//latitude: using the Mercator projection
		lat *= Math.PI / 180; //convert from degrees to radians
		var dy:Number = Math.log(Math.tan((lat/2) + (Math.PI/4)));  // do the Mercator projection (w/ equator of 2pi units)

		dy = (map_height / 2) - (map_width * dy / (2 * Math.PI)) + y_pos;   // fit it to our map

		return new Point(dx, dy);
	}



then you could just call that, and then pass the returned values to the draw method.
Was This Post Helpful? 0
  • +
  • -

#4 Chopster  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 134
  • Joined: 29-March 08

Re: Stage area seems to jump when drawRect() onto map.

Posted 14 April 2011 - 07:12 AM

Hi lordofduct,

I wasn't sure how well i explained my first post, so i thought i might have more success starting fresh, but, point taken. I apologize.

Also your idea's worked like a charm, i have it working and i feel slightly stupid, thank you very much.

Jase
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1