Tag Archives: registration point

Reset Registration Point

Actionscript:
  1. // display object target, value - "center" or "upperLeft"
  2. function setRegistration(dsp:DisplayObjectContainer, v:String):void {
  3.     var i:int;
  4.     var child:DisplayObject;
  5.     var b:Rectangle=getBounds(dsp);
  6.     for (i = 0; i <dsp.numChildren; i++) {
  7.         child=dsp.getChildAt(i);
  8.         child.x+=b.x*-1;
  9.         child.y+=b.y*-1;
  10.     }
  11.     if (v=="center") {
  12.         dsp.x+=dsp.width/2;
  13.         dsp.y+=dsp.height/2;
  14.         for (i = 0; i <dsp.numChildren; i++) {
  15.             child=dsp.getChildAt(i);
  16.             child.x-=dsp.width/2;
  17.             child.y-=dsp.height/2;
  18.         }
  19.     } else if (v == "upperLeft") {
  20.         if (dsp.parent) {
  21.             b=getBounds(dsp.parent);
  22.             dsp.x=b.left;
  23.             dsp.y=b.top;
  24.         }
  25.     }
  26. }
  27.  
  28. // example:
  29. // (make a MovieClip and fill it with, text, shapes etc...)
  30.  
  31. setRegistration(clip, "center");
  32. // registration(clip, "upperLeft");
  33.  
  34. addEventListener(Event.ENTER_FRAME, onLoop);
  35. function onLoop(evt:Event):void {
  36.     clip.rotation+=1;
  37. }

Teaching beginner flash... I've noticed people always get confused about the registration point. They think they should be able to move it with the IDE or ActionScript. I always say "You can't change the registration point, you can only change the position of graphical elements in relationship to it." Anyway, as a sort of joke one day after class I wrote the above function... it changes the registration of any DisplayObjectContainer to either "center" or "upperLeft".

To test this code out, fill a MovieClip with a bunch of different things (text, graphics, scribbles)... give it a strange registration point and then run the above code on it. You'll be able to tell that the registration has changed by the way the clip rotates. Although I haven't been able to break this function, I think it's possible that it doesn't work under all circumstances.

Using reparenting is a more elegant solution to this issue. Maybe I'll post that in the next couple days.

Posted in DisplayObject, display list | Also tagged , | 2 Comments