开发者

move a displayobject based on another objects rotation

Ive probably ran into some code like this before. just cant remember where. I have 2 display objects on my stage. One is rotating and the other is positioned 90 degrees from the rotating object. As the object rotates, the other object adjusts its x and y positioning from the rotating object..

If this doesnt make sense, then let me explain what I am trying to do. I have a spaceship in my flash game. this ship has 2 torrets siting on the left and right wing. as the ship turns, the torrets need to remain on the wings. Does anyone know how I would go ab开发者_开发知识库out doing this.


why don't you just nest them inside the wings? if the wing rotates they will remain on top of the wing.


If you insist on keeping the DisplayObjects non-nested, use the power of trigonometry.

Assuming that at rotation = 0, your turret object is a distance, r, directly to the right of the ship center.

private function degreesToRadians(degrees:Number):Number {
    return degrees * Math.PI / 180;
}

turret.x = shipCenter.x + r*Math.cos(degreesToRadians(ship.rotation));
turret.y = shipCenter.y + r*Math.sin(degreesToRadians(ship.rotation));

However, learning how to use a proper transformation matrix or nesting your display objects will ultimately lead to a cleaner solution

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜