Hey folks!
I could use some help here, been stuck for a while and cant really understand what goes wrong :bored:
Im buidling a class that I will use to offset the register point in a movieclip, so it will scale or rotate around that new point. To do this, I wrap it in a container clip, moves the mc within the container, and then moves the container back so the mc ends up at the same coordinates as it had before. Then I use the container to “fake” a new register point.
The problem is; it works great the first time the method is run but the next time the container starts getting wrong offsets and “jumps” on the stage. I want my red dot (drawn by devPoint method) wich represents the new register point to just move to where I click, and nothing else.
It´s probably overlooking something simple, but I cant seem to get my head around it…
In McRegisterPoint.as
package{
import flash.display.MovieClip;
import flash.display.Shape;
public class McRegisterPoint extends MovieClip{
private var _mc:MovieClip // movieclip to get a new register point
private var _container:MovieClip;
private var ox:Number //original x pos
private var oy:Number // original y pos
public function McRegisterPoint(mc:MovieClip){
//constructor
trace("new McRegisterPoint created for: " + mc.name)
_mc = mc;
}
public function setRegister(regX:Number, regY:Number, dev:Boolean = false):MovieClip{
if(!_container){
// first time
ox = _mc.x;
oy = _mc.y;
_container = new MovieClip();
_mc.parent.addChildAt(_container,1);
_container.addChild(_mc);
// puts the new container with the _mc in it at the original coords
_container.x = ox;
_container.y = oy;
}
//all times
// moves _mc to the new coords, within container
_mc.x = regX;
_mc.y = regY;
// moves container negative to how mc was moved
_container.x += regX * -1;
_container.y += regY * -1;
if (dev){
devPoint();
}
return _container;
}
//draws a red dot to show the new register point
private function devPoint(){
var circle:Shape = new Shape( );
circle.graphics.beginFill( 0xff0000 , 1 );
circle.graphics.drawCircle( 0 , 0 , 2 );
_container.addChild( circle );
}
}
}
On main timeline, I have mc called “box”.
var fixedBox = new McRegisterPoint(box);
box.addEventListener(MouseEvent.CLICK, handleClick);
function handleClick(evt:MouseEvent){
var myClip:MovieClip = fixedBox.setRegister(evt.target.mouseX * -1, evt.target.mouseY * -1, true);
myClip.rotation = 45;
}