MovieClip._location property

| 4 Comments | 1 TrackBack

It has been said that necessity is the mother of invention. It has been argued that annoyance, not necessity, fuels invention.

I'd like to start the year off with a little script brought to life by annoyance. To set the position of a MovieClip, you need two lines of code: one to set the _x position, and another to set the _y position. Many times I found myself wishing for a way to set a MovieClip's coordinates in one line. Enter the "_location" property. Use the code below to add the _location property to all of your MovieClips.

// pt is an object with an x and a y property
function $setLocation(pt) {
	this._x = pt.x;
	this._y = pt.y;
}
function $getLocation() {
	return {x:this._x, y:this._y};
}
MovieClip.prototype.addProperty("_location", $getLocation, $setLocation);

// constants for ASSetPropFlags
var HIDDEN:Number = 1;
var PROTECT_DELETE:Number = 2;
var PROTECT_OVERWRITE:Number = 4;

ASSetPropFlags(MovieClip.prototype,"_location", 
			HIDDEN | PROTECT_DELETE | PROTECT_OVERWRITE, 0);

Usage is simple - you can just write..

my_mc._location = {x:20, y:40};

.. and the MovieClip will adjust it's x and y positions.

Happy New Year Everyone!

1 TrackBack

TrackBack URL: http://www.darronschall.com/mt/mt-tb.cgi/1

MovieClip._grandpa from Jesse Warden dot Kizz-ohm - Multimedia Engineer & Flash Developer on January 5, 2004 2:40 PM

And to think, all you have to do nowadays to obtain Kudos from peeps is whip code out ye arse... rock on! Derived from Darron's _location. Prototype ain't dead, just under-used. Can you eat Kudos? // pt is an object... Read More

4 Comments

  • I have to say, I'm a pretty big fan of the new V2 components' "move" method, which takes two parameters (eg: mc.move(x,y); ). But retrieval of properties is still two lines.

    How about using an array of size two, [x,y] ? That should save you some keystrokes ;)

    I know, I know, everyone's a critic. Happy New Year.

     
  • Kudos Darron !!!
    Great work !!!

    -Pandu

    P.S. But this time, you are the father of the invention in regards to this context. :) (Just kidding)

     
  • lol

    my_mc._y = 40;
    my_mc._x = 20;

    is actually 3 characters less than

    my_mc._location = {x:20, y:40};


    :-)

     
  • Actually Peter, the _location property is really useful with a Point2D class that I didn't share.

    my_mc._location = new Point2D(20,40);

    And we can even shorten that to _loc to save some typing. It should be about as readable since loc is a common abbreviation.

    The Point2D class I'm using is basically just a port of the Java Point2D class: http://java.sun.com/j2se/1.3/docs/api/java/awt/geom/Point2D.html

    Since I was using Java before I was introduced to Flash, I recreated some Java classes that I found useful.. and by making the _location property I'm easily able to use a Point as the coords for a clip.

     

Leave a comment

Flex.org - The Directory for Flex

Archives