Category Archives: DisplayObject

Isometric Box

Actionscript:
  1. stage.frameRate = 30;
  2.  
  3. for (var i:int = 0; i<100; i++){
  4.     makeBoxSegment(200, 200 - i, i * 2);
  5. }
  6.  
  7. function makeBoxSegment(xp:Number, yp:Number, col:uint):Sprite {
  8.     var isoBox:Sprite = Sprite(addChild(new Sprite()));
  9.     with (isoBox) scaleY = .5, y = yp, x = xp;
  10.     var box:Shape = Shape(isoBox.addChild(new Shape()));
  11.     box.rotation = 45;
  12.     with (box.graphics) beginFill(col), drawRect(-50,-50,100,100);
  13.     isoBox.addEventListener(Event.ENTER_FRAME, onRotate);
  14.     return isoBox;
  15. }
  16.  
  17. function onRotate(evt:Event):void {
  18.     evt.currentTarget.getChildAt(0).rotation = mouseX;
  19. }

An isometric box that rotates with the mouseX.

Also posted in Graphics | Tagged , , , | 2 Comments

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.

Also posted in display list | Tagged , , | 2 Comments

Skew DisplayObject

Actionscript:
  1. var box:Shape = Shape(addChild(new Shape()));
  2. with (box.graphics) beginFill(0x006666), drawRect(0,0,50,50);
  3. box.x = box.y = 100;
  4.  
  5. addEventListener(Event.ENTER_FRAME, onLoop);
  6.  
  7. function onLoop(evt:Event):void {
  8.    
  9.     var m:Matrix = box.transform.matrix;
  10.     // skew on the X
  11.     m.c = (mouseX - stage.stageWidth / 2 ) / stage.stageWidth;
  12.    
  13.     // skew on the Y
  14.     // m.b = (mouseX - stage.stageWidth / 2 ) / stage.stageWidth
  15.    
  16.     box.transform.matrix = m
  17. }

This skews a box Shape using the c and b properties of the transformation matrix. Note that these values don't match those in the IDE's transform window. This is good further reading if your interested in this topic.

Also posted in motion | Tagged , , | Leave a comment

DisplayObject describeType()

Actionscript:
  1. for each(var prop:String in describeType(DisplayObject).factory.accessor.@name){
  2.     trace(prop);
  3. }
  4. /* outputs
  5. scaleY
  6. mouseX
  7. mouseY
  8. mask
  9. rotation
  10. alpha
  11. transform
  12. blendMode
  13. x
  14. root
  15. loaderInfo
  16. width
  17. z
  18. rotationX
  19. scale9Grid
  20. filters
  21. rotationY
  22. y
  23. stage
  24. scaleZ
  25. parent
  26. accessibilityProperties
  27. scrollRect
  28. rotationZ
  29. height
  30. name
  31. opaqueBackground
  32. blendShader
  33. cacheAsBitmap
  34. visible
  35. scaleX
  36. */

I First saw describeType() at senocular's AS3 Tip of the Day on Kirupa.

Also posted in XML | 1 Comment