import flash.display.Shape; import flash.utils.Timer; import flash.events.TimerEvent; import flash.display.Sprite; // start rotation loop using a Timer to turn all objects var moveTimer:Timer = new Timer(10,0); // function to rotate objects moveTimer.addEventListener(TimerEvent.TIMER, handleTimer); // draw a rect shape var redrect:Shape = new Shape(); redrect.graphics.lineStyle(3,0x0,1,true); redrect.graphics.beginFill(0xFF0000); redrect.graphics.drawRect(0,0,100,100); redrect.graphics.endFill(); addChild(redrect); // position at x:100 / y:100 on stage redrect.x = 150; redrect.y = 200; // now draw a blue square but with a container while centering the shape // container var blueContainer:Sprite = new Sprite(); addChild(blueContainer); // draw a rect shape var bluerect:Shape = new Shape(); bluerect.graphics.lineStyle(3,0x0,1,true); bluerect.graphics.beginFill(0x0000FF); bluerect.graphics.drawRect(0,0,100,100); bluerect.graphics.endFill(); blueContainer.addChild(bluerect); // position in center of container (subtract half width/height) //-------------centering code---------------- bluerect.x -= bluerect.width / 2; bluerect.y -= bluerect.height / 2; //------------------------------------------- // position container blueContainer.x = 400; blueContainer.y = 200; // start timer which invokes function below moveTimer.start(); // rotate the red rect (upper left reg) and blue (objects centered); function handleTimer(e:TimerEvent):void { // just rotate both to see the registration redrect.rotation += 2; blueContainer.rotation += 2; } |