PDA

View Full Version : Can ActionScript detect when a Motion Tween ends or begins?


britsky
11-03-2009, 04:20 PM
I also posted this in the Newbie section last week, but didn't get a response there yet.

Project:
Reading text from XML document
Text scrolls up, pauses, then automatically scrolls up and off the screen
Text content changes to the next node.

Scene Level:
I have a movie clip named "symbol headline" (see attached screenshot)

Within the Movie Clip:
I have 2 dynamic text boxes: headlineText and contentText (see attached screenshot)

Action Script:

// Import flash libraries
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.xml.*;
import flash.net.*;

// Declare variable to hold XML and load the XML document
var vendorFlash_xml:XML = new XML();
var XML_URL:String = "VendorFlash.xml";
var myXMLURL:URLRequest = new URLRequest(XML_URL);
var myLoader:URLLoader = new URLLoader(myXMLURL);
myLoader.addEventListener("complete", xmlLoaded);

// This function is run when the load of the XML is complete
function xmlLoaded(event:Event):void
{
vendorFlash_xml = XML(myLoader.data);

var i:int = 1; //Starting at 1 because we'll be displaying the first node manually
var item_count:int = vendorFlash_xml.item.length();
var pattern:RegExp = /\\n/gi; //We'll use \n in the XML to denote a newline. This pattern is used below to insert a \r
var contentString:String //Needed for string replace function

var timer:Timer = new Timer(6000,0); //Timer(milliseconds, repeat count[0 for infinite])
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();

// Display the first node so there's not a delay.
headlineText.text=vendorFlash_xml.item[0].headline.text();
contentString=vendorFlash_xml.item[0].content.text();
contentText.text=contentString.replace(pattern,"\r");

function timerHandler(event:TimerEvent):void {
headlineText.text=vendorFlash_xml.item[i].headline.text();
contentString=vendorFlash_xml.item[i].content.text();
contentText.text=contentString.replace(pattern,"\r");

i++;
if (i == item_count) {
i = 0;
}
}
}


The Problem:
Everything works GREAT except there is a slight difference between the TIMER in the ActionScript and the frames in the GUI. That means that after 3 or 4 iterations, the text starts changing before the motion tween is done.

My Idea:
Instead of using a timer in my ActionScript, can I create and Event Listener that listens for when the movie clip starts to replay? If so, HOW ?

This is my first ActionScript project, and I've exhausted my Google abilities to figure out this last hurdle.

Many thanks, and God bless!
-Jeff

midnit
11-03-2009, 04:53 PM
You can create an Event Listener to detect when a Motion Tween has finished using the Tween Event (http://help.adobe.com/en_US/AS3LCR/Flash_10.0/fl/transitions/TweenEvent.html) class, however, I don't know if the listener will work with timeline tweens.

You may or may not need to create your tween using the Tween Class (http://help.adobe.com/en_US/AS3LCR/Flash_10.0/fl/transitions/Tween.html) (or TweenLite (http://blog.greensock.com/tweenlite/) or Tweener (http://code.google.com/p/tweener/))

Hope that helps!

Edit: If I get some free time here at work, I can write an example of how this is done, if the Adobe Language Reference page doesn't have one ( I didn't bother to check :) )

krayzeebean
11-03-2009, 05:01 PM
On the last frame of your tween have it dispatch an event so you know it's done. dispatchEvent(new Event("AnimationComplete", true));

CMYMatt
11-03-2009, 05:17 PM
I would suggest using TweenLite.
Then using the "onComplete:someFunction;".
You will avoid being garbage collected too.

What krayzeebean said would also work.

Good luck.

omega_platypus
11-03-2009, 05:30 PM
For britsky:


MovieClips do not dispatch any event when they start to replay. However, you can use the currentFrame and totalFrame of the movie to determine when it hits the first frame. For example:



/*m refers to the movieclip we want to listen for. If this code is on the timeline we want to check, just leave it set to 'this' */
var m:MovieClip=this;
/*Since the playhead will reach frame 1 before it ever loops, we want to be sure it goes around at least once before saying it has restarted. If you don't want this check, set looped to true right here */
var looped:Boolean=false;

m.addEventListener(Event.ENTER_FRAME,onEnterFrame) ;

function onEnterFrame(e:Event):void
{
looped=true;
if(m.currentFrame == 1)
{
//Then the movieclip has restarted from the first frame. Act accordingly.
}
}


Let me know if this helps!