Flash Triggering AS Code on a Frame
I'm a Flex developer who had to do some coding in Flash, so my understanding of how putting an action on a time line is somewhat limited.
I'm trying to attach this action on the first frame of the movie that I'm planning play. When I play the movie for the first time, the code executes as expected. However, when I restart or rewind to the first frame of the movie, the code no longer executes. I'm not sure why this is happening, as it seemed added actionscript code to a frame seems pretty straight forward.
import flash.utils.Timer;
import flash.net.URLLoader;
import flash.net.URLRequest;
var urlLoader:URLLoader;
var timer:Timer;
startAdPlay();
function startAdPlay():void
{
if( urlLoader == null ) {
urlLoader = new URLLoader();
}
urlLoader.load( new URLRequest( "http://localhost:20081/startadplay/15" ) );
timer = new Timer( (15 * 1000) );
timer.addEventListener(TimerEv开发者_如何学编程ent.TIMER, stopAdPlay );
timer.start();
}
function stopAdPlay(event:TimerEvent):void
{
urlLoader.load( new URLRequest( "http://localhost:20081/stopadplay" ) );
timer.stop();
stop();
}
Given that you are already a Flex developer and thus used to writing your code in separate files outside Flash, I would recommend keep doing that and put as little code as possible on the timeline. Purists will recommend no code at all on the timeline, but I will put function calls and dispatchEvent() commands on the timeline. The actual function being called is defined in a separate .as file however.
So in your case I would just put the startAdPlay(); function call on the timeline, and put the actual function body in a separate class that is linked to the Flash movie in the Properties window. That'll make it more logical when and where the function is defined.
I suppose you don't call startAdPlay()
while restarting the movie in app
精彩评论