Tasos
Tasos

Reputation: 1642

Trace x,y of movieclip that is animated through timeline

I am trying to trace the position of a movieclip(that contains a simple timeline animation inside) so that I can attach another movieclip to be able to follow it.

How can I do that?

empty = the movieclip that contains timeline animation

mc = the movieclip I want to follow the "empty" movieclip

empty.addEventListener(Event.ENTER_FRAME, onMove);

function onMove(event:Event):void {
    var mc:MovieClip = new SmokeTween();
    mc.x = empty.x;
    mc.y = empty.y;
    mc.rotation = Math.round(Math.random() * 70);
    this.addChild(mc);
}

Actually I went into "empty" mc and and used this code and seems to work fine:

this.addEventListener ( Event.ENTER_FRAME, traceFrame );

function traceFrame ( e : Event ) : void
{
    if (e.target.currentFrame > 0){
        MovieClip(parent.parent).mc.x = e.target.x;
    }
}

Upvotes: 0

Views: 785

Answers (2)

Tasos
Tasos

Reputation: 1642

Actually I went into "empty" mc and and used this code and seems to work fine:

this.addEventListener ( Event.ENTER_FRAME, traceFrame );

function traceFrame ( e : Event ) : void
{
    if (e.target.currentFrame > 0){
        mc.x = e.target.x;
    }
}

Upvotes: 0

Dave
Dave

Reputation: 46249

I imagine that empty doesn't animate, so you need to use the root's ENTER_FRAME event instead of empty's:

addEventListener(Event.ENTER_FRAME, onMove); // no "empty."
function onMove(event:Event):void {
    var mc:MovieClip = new SmokeTween();
    mc.x = empty.x;
    mc.y = empty.y;
    mc.rotation = Math.round(Math.random() * 70);
    this.addChild(mc);
}

As your project gets bigger, you'll also find that recycling objects becomes important (especially in Flash). Keep an array of SmokeTweens and keep recycling them, instead of creating new ones and letting them delete themselves.

Upvotes: 1

Related Questions