0

我试图弄清楚如何在 AS3 中使 2 个对象相互环绕。例如,假设 2 个对象是杯子。我希望它们围绕彼此旋转一圈。另一种看待它的方式是,他们每个人都在同一个圆圈中旅行,但彼此相对,所以看起来他们正在旋转。我需要圆的直径是一个变量。

我已经尝试了很多事情,但似乎无法做到正确。

4

1 回答 1

1

这是一个将围绕另一个精灵的类的快速示例。

public class C extends Sprite {
    public var target:Sprite;
    private var curTan:Number = 0;

    public var distance:Number = 200;
    public var speed:Number = .05;
    public var decay:Number = .05;

    public function C(size:Number = 10, color:uint = 0xFF0000, seed:Number = 0) {
        curTan = seed;

        graphics.beginFill(color);
        graphics.drawCircle(0, 0, size);
    }

    public function go(target_:Sprite):void {
        target = target_;
        addEventListener(Event.ENTER_FRAME, tick, false, 0, true);
    }

    protected function tick(e:Event):void {
        x += ((target.x + Math.sin(curTan) * distance) - x) * decay;
        y += ((target.y + Math.cos(curTan) * distance) - y) * decay;

            //if you don't want a decay (only useful if the target is moving) then use this instead:
            //x = target.x + Math.sin(curTan) * distance;
            //y = target.y + Math.cos(curTan) * distance;
        curTan += speed;
    }
}
于 2012-08-07T23:39:54.353 回答