jbassking10
jbassking10

Reputation: 803

Tweening in AS3 to make object circle each other

I'm trying to figure out how in AS3 to make 2 objects circle each other. For example lets say the 2 objects are cups. I want them to spin around each other in a circle. Another way to look at it that they each travel in the same circle but across from each other so it will appear that they're spinning. I need the diameter of the circle to be a variable.

I've tried a number of things but can't seem to get it right.

Upvotes: 0

Views: 446

Answers (1)

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

Here is a quick example of a class that will circle another sprite.

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;
    }
}

Upvotes: 1

Related Questions