Playing with anchor after playing with position will screw over all your work with position. When you set the anchor, you set the origin point of a sprite to that anchor. If the anchor.x = 0.5, that means that when you set the x of the sprite, you set the middle of the sprite to the x position.
Btw, anchor is a value between 0 and 1:
- 0: Refers to the extreme left for the x, and the top of the sprite for the y.
- 1: Refers to the extreme right for the x, and the bottom of the sprite for the y.
- Values in between refer the ratio of the width/height. If a sprite's width is 100, anchor.x = 0.5 will set the pixel 50 as the center.
So you understand the problem now. When you call the a.x and the a.y, you're calling the position at the anchor point of the sprite and not the top left corner of the sprite, like the original code was intended to do. So to fix the problem you have to take into account the x and y position of the top left corner, not the anchor point:
Code:
var real_x = a.x - a.anchor.x * a.width;
var real_y = a.y - a.anchor.y * a.height;
// Then the same as before, just replace a.x by real_x and a.y by real_y
// Note that I have not tested, and it is only based on the theory lol.