问题
I have asked something similar before, but never go to the solution I need. So am starting a new stack to not confuse my issues, and to make a little clearer.
What I need to do
I have multiple flash objects on a html page, they are all identical, and all of them need to be sync'd with the correct time. In its current state, each one queries the timeserver, and gets the time, which works great. BUT what I want to achieve is the first one to load to be the only one that sync's with the time server, and then the other flash objects wait for it to finish, and load the sync time from that one.
The Problem
SharedObjects: On paper, this seamed like the perfect solution, I get the 1st one to load to save a SO "timestarted", if any loading flash after that sees this SO, it waits for a "synctime" SO to be created, and load that...great! But no... SharedObjects only appear to load the SO at the time of loading the flash, any changes, or new SO after the flash loads is completely unknown to any other flash than the one that created it! (until the flash is reloaded)...doh
LocalConnection: A little more complicated, but sounded like a good solution, apart from only one flash can have the connection open at any one time, gah!
What I need
In simple terms I need a way to have one flash object declare itself as the sync'er, and the others to listen up! Something I thought would be so simple, but turning out to be so complicated. I need any suggestions, advice on what to do.
回答1:
If you can use a tiny bit of javascript you can do this using ExternalInterface. Have the movies call out to a small js-script that keeps track of whoever is loading the time, then have the other flashes just register callbacks. Once the first flash has the time, call out to the js and let that update the other two.
回答2:
I think what you are really looking for is the LocalConnection object https://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/
It'll allow your swf's to talk to each other.
回答3:
Maybe I'm misunderstading the question, but this works:
1st swf (syncher):
import flash.net.SharedObject;
import flash.utils.Timer;
var time:String;
var timeValue:int;
var tf:TextField;
init();
function init():void{
var timer:Timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER, blah);
timer.start();
tf = new TextField();
addChild(tf);
SharedObject.getLocal("timer","/")
}
function blah(e:TimerEvent):void{
timeValue++;
SharedObject.getLocal("timer","/").data.timer = timeValue;
trace(SharedObject.getLocal("timer","/").data.timer);
SharedObject.getLocal("timer","/").flush();
tf.text = timeValue.toString();
}
2nd swf (synchee)
import flash.net.SharedObject;
import flash.utils.Timer;
var time:String;
var timeValue:int;
var tf:TextField;
init();
function init():void{
var timer:Timer = new Timer(100);
timer.addEventListener(TimerEvent.TIMER, blah);
timer.start();
tf = new TextField();
addChild(tf);
}
function blah(e:TimerEvent):void{
timeValue = SharedObject.getLocal("timer","/").data.timer;
tf.text = timeValue.toString();
}
I know they're not identical, but it would be easy enough to save a "loaded" propery to the SO, so subsequently loaded swf's know they're synchees, not the syncher?
来源:https://stackoverflow.com/questions/2154460/as3-sync-data-between-3-or-more-identical-flash-objects-on-the-same-page