I can’t find a solution to this problem. There is a movie clip MyRectangle and it is being moved by a different class Test around the stage.
Class Test has no idea where the registration point is of MyRectangle is, it assumes it is always at top left corner.
Class MyRectangle sometimes logically changes its registration point during runtime according to some algorithm. By this I mean it changes the position of all its children, the same way as we would do it in Flash IDE. So logically, the registration point can be e.g. in the midle.
The point is to write the MyRectangle class to behave correctly at all times, so that other classes, although unaware where the registration point really is (they assume it is in top left corner), are still capable to position the rectangle exactly where they want.
For example:
Class Test wants to position the rectangle in the top left corner of the stage. So it makes assignments:
myRect.x = 0;
myRect.y = 0;
But it will not work if MyRectangle changed its registration point by moving all children by (-50,-50). In this case, only bottom right quarter of the rectangle will be visible.
Tried solution:
I have no clue how to solve it. Tried to write setters and getters of x and y, but it doesn’t work. I adjusted the registration point of MyRectangle in Flash IDE instead of doing it programatically, but the result would be the same.
Also to make it easier I assume the registration point is in the middle. Once I know how to solve this problem, I’ll know how to solve it for registration point anywhere.
public class MyRectangle extends MovieClip {
public function MyRectangle() {
// constructor code
}
private var rawX:Number;
private var rawY:Number;
public override function get x():Number {
return rawX-width/2;
}
public override function get y():Number {
return rawY-height/2;
}
public override function set x(val:Number):void {
rawX = val+width/2;
}
public override function set y(val:Number):void {
rawY = val+height/2;
}
}
And then use it in Test function:
public function Test() {
var mc:MyRectangle = new MyRectangle();
addChild(mc);
mc.x = 200;
}
But only the bottom right quarter of the rectangle is visible, despite of any assignments of x or y in class Test.
Any idea how to solve this problem?