Вопрос

I need some help with a really specific problem. I've looked and found some answers, but nothiung as specific as what I need.

I'm using flashdevelop, and for now, I've a rectangle (it's a Sprite) following my mouse cursor (centered). I'd like to apply a rotation on it when I scroll my mouse wheel, but I need the rotation to apply on it's center, and I need the object to stay centered on my mouse cursor at the same time. And I also need to apply the rotation in radians, not in degree.

For now I got this :

var mod:Number;

if (e.delta <= 0) {
    mod = -0.1;
}else {
    mod = 0.1;
}

Could someone help me with it please ? After what I've seen it seems that I've to use matrix, and complex maths, but I've no idea how to do what I want to do with this.

Это было полезно?

Решение

You just need to keep the registration point of the sprite in center of the rectangle to rotate it about it's center. When you draw a sprite from (0,0) the registration point is top-left and not center. Here's how you make it center:

var rectangleSprite:Sprite = new Sprite();
rectangleSprite.graphics.lineStyle(3,0x00ff00);
rectangleSprite.graphics.beginFill(0x0000FF);
//please note that i am not drawing the sprite from (0,0), 
//instead i start from (-width/2,-height/2)
rectangleSprite.graphics.drawRect(-150,-75,300,150);
rectangleSprite.graphics.endFill();

And for degree to radian conversion you just need multiply your value in radians by (180/Math.PI) before applying your rotation.

var rotationInDegrees = rotationInRadians*(180/Math.PI);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top