Slight problem i have been pondering on, i have a class that loads a text from a url as a means to sync the time.
The swf file can be on the same page multiple times, but i want it to check to see if one of the swf is sync'ing, if so then wait for it to complete and load its sync'd value (rather then query the url itself), so that they are all sync'd to the same value (of the 1st swf file to load).
I am trying this using Shared Objects, which looks a little like this (snippets):
public function sync():void
{
sharedObject = SharedObject.getLocal("synctime", "/");
trace(sharedObject.data.startOfRequest, sharedObject.data.endTime);
if ( ((new Date().getTime() - 10000) < sharedObject.data.startOfRequest) && (sharedObject.data.endTime !== undefined))
{
loadUTCFromSharedObject();
}
else if ( ((new Date().getTime() - 10000) < sharedObject.data.startOfRequest) && (sharedObject.data.endTime == undefined ) )
{
timer.addEventListener(TimerEvent.TIMER, loadUTCFromSharedObject);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, startSync);
timer.start();
} else {
startSync()
}
}
private function startSync(e:Event = null)
{
var syncRequest:URLRequest = new URLRequest(TIMEURL + "?cb=" + Math.random());
var syncTimeLoader:URLLoader = new URLLoader();
syncTimeLoader.dataFormat = URLLoaderDataFormat.TEXT;
syncTimeLoader.addEventListener(Event.COMPLETE, syncTimeComplete );
syncTimeLoader.addEventListener(IOErrorEvent.IO_ERROR, syncTimeFailed );
sharedObject.clear();
startOfRequest = new Date();
sharedObject.data.startOfRequest = startOfRequest.getTime();
sharedObject.flush();
syncTimeLoader.load(syncRequest);
}
private function loadUTCFromSharedObject(e:Event = null):void
{
var sharedObjectCheck:SharedObject = SharedObject.getLocal("synctime", "/");
trace (sharedObject.data.endTime, sharedObjectCheck.data.endTime);
if ( sharedObject.data.endTime !== undefined )
{
timer.removeEventListener(TimerEvent.TIMER, loadUTCFromSharedObject);
timer.removeEventListener(TimerEvent.TIMER_COMPLETE, startSync);
dispatchEvent( new LoadUTCTimeEvent(LoadUTCTimeEvent.SYNC_COMPLETE, null, null, sharedObject, true));
trace(sharedObject.data.UTCOffset);
}
}
private function syncTimeComplete(event:Event):void
{
var loadedText:URLLoader = URLLoader(event.target);
dispatchEvent( new LoadUTCTimeEvent(LoadUTCTimeEvent.SYNC_COMPLETE, loadedText.data, startOfRequest, sharedObject, false));
}
Event Dispatcher:
public function LoadUTCTimeEvent(eventStatus:String, timeString:String = null, startOfRequest:Date = null, sharedObject:SharedObject = null, loadFromSharedObject:Boolean = false):void
{
super(eventStatus);
if (eventStatus == SYNC_COMPLETE) {
if (loadFromSharedObject == true) {
time = sharedObject.data.syncTime;
UTCOffset = sharedObject.data.UTCOffset;
}
else
{
//...snip....//
sharedObject.data.UTCOffset = UTCOffset;
sharedObject.data.syncTime = time;
sharedObject.data.endTime = curDate.getTime();
sharedObject.flush(1000);
}
}
}
The problem i am getting is that the 1st swf creates the sharedobject fine, but the second one reads the endTime as undefined, unless i refresh the page (i.e it doesnt pick up live changes). Is this method not posible, should i try another aproach?
Thanks in advance.
Edit: decided to add another stack to explain a little more:(as3) Sync data between 3 or more identical flash objects on the same page
you can call a javascript function to do the locking if you stuck with this solution
Okay, use localconnection
Typically, you should not use Local SharedObjects to sync things. The flush() command has never worked for me in the way you are intending it to.
Related
I try to make simple scorebug. It’s feed from external XML file.
I want to start an animation when score changing, but i doesn’t found event listener or dispatcher to get change of value.
var myTimer:Timer = new Timer(100);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
myTimer.start();
function timerListener (e:TimerEvent):void
{
var myXMLLoader:URLLoader = new URLLoader();
myXMLLoader.load(new URLRequest("Xml.xml"));
myXMLLoader.addEventListener(Event.COMPLETE, processXML);
function processXML (e:Event):void
{
var myXML:XML = new XML(e.target.data);
ShotClock.text = myXML.timestamp;
GameClock.text = myXML.Clock;
HS.HomeScore.text = myXML.HomeScore;
AS.AwayScore.text = myXML.AwayScore;
Period.text = myXML.Period;
AwayTeam.text = myXML.AwayName;
HomeTeam.text = myXML.HomeName;
}
if ( (myXML.HomeScore).CHANGE )
{ var myTween:Tween = new Tween(HS.HomeScore, "alpha", Strong.easeIn, 0, 1, 1, true); }
}
You need to change the logic of loading. Instead of starting timed loadings, which won't be accurate 100 ms anyway, could lag, could fail, could arrive in order other than you issued them, etc., you need a single-thread asynchronous loop that does, simply, the following:
Start data loading.
(async pause)
Handle the loaded data.
Get XML data from loaded text.
Parse XML attributes.
Check if the score variable changed and trigger the things you want to.
Wait 100 ms.
(async pause)
Go to step №1.
Something like that:
var theLoader:URLLoader;
var theScore:Number = 0;
var theTimer:Timer;
// Start the tracking routine.
trackNext();
function trackNext():void
{
theLoader = new URLLoader;
theLoader.addEventListener(Event.COMPLETE, onXML);
// Error tracking is a must or the logic flow
// might just stop when you least expect it to.
theLoader.addEventListener(IOErrorEvent.IO_ERROR, onError, false, 0, true);
theLoader.load(new URLRequest("xml.xml"));
}
function onXML(e:Event):void
{
// Sanity check. There should be only one valid URLLoader instance.
if (e.target != theLoader)
{
return;
}
var X:XML;
try
{
X = new XML(theLoader.data);
}
catch (fail:Error)
{
// Processing the case where data is loaded successfully,
// but it is not a valid XML String.
onError(e);
return;
}
// Here's the place for your code
// that extracts data from XML.
// ...
// Get the new score value.
var aScore:Number = X.HomeScore;
// Compare it to previous value.
if (aScore != theScore)
{
// ATTENTION!!! THIS IS THE PLACE YOU ARE LOOKING FOR!!!
// Trigger things if the new score is any different.
// ...
// Keep the new score value.
theScore = aScore;
}
finishLoading();
}
// This method just ignores an error, think of it
// as of blank to process the exceptional cases.
function onError(e:Event):void
{
// Sanity check. There should be only one valid URLLoader instance.
if (e.target != theLoader)
{
return;
}
finishLoading();
}
// Call it finishLoading(true) to stop the tracking routine.
function finishLoading(thenstop:Boolean = false):void
{
// Dispose of the URLLoader instance if any.
if (theLoader)
{
var aLoader:URLLoader = theLoader;
theLoader = null;
aLoader.removeEventListener(Event.COMPLETE, onXML);
try
{
aLoader.close();
}
catch (fail:Error)
{
// Do nothing about it.
}
}
finishTimer();
if (thenstop)
{
return;
}
// Wait 100 ms to give Flash Player a breather
// before starting to load the file once more.
theTimer = new Timer(100, 1);
theTimer.addEventListener(TimerEvent.TIMER, onTime);
theTimer.start();
}
function finishTimer():void
{
// Dispose of the Timer instance if any.
if (theTimer)
{
theTimer.removeEventListener(TimerEvent.TIMER, onTime);
theTimer.stop();
theTimer = null;
}
}
function onTime(e:TimerEvent):void
{
// Now go for the next iteration.
finishTimer();
trackNext();
}
Okay, so I'm trying to create a mobile flash game (I'm mostly an animator, a storyteller) and I'm having trouble with the input text for the player name. I've often read helpful tips on this site so I signed up to get some help. The code I use to load and save data is saveDataObject, But as far as I know input text has to be used with package code. I tried to convert it to function var, but then these errors occur. I am unsure how to use the Package class code, and everything I've read on it has been confusing. I am pretty much self taught for everything I know about code though tutorials and forums, so if it isn't explained in a way I can understand I wont be able to do it...
Here's the section of code if I wasn't clear(error lines separate):
var playerName:int;
init(); // this line goes directly beneath the variables
function f_1(init):void
{ // call once to set everything up
saveDataObject = SharedObject.getLocal("DataBattle/character/name"); // give the save data a location
-
playerName = txtPlayer;
-
function addName(e:TouchEvent):void
{
var myTextBox1:TextField = new TextField();
var txtPlayer:TextField = new TextField();
var myText:String = "Cyan";
function CaptureUserInput()
{
captureText();
}
function captureText():void
{
myTextBox1.type = TextFieldType.INPUT;
myTextBox1.background = true;
addChild(myTextBox1);
myTextBox1.text = myText;
myTextBox1.addEventListener(TextEvent.TEXT_INPUT, textInputCapture);
}
function textInputCapture(event:TextEvent):void
{
var str:String = myTextBox1.text;
createOutputBox(str);
}
function createOutputBox(str:String):void
{
txtPlayer.background = false;
txtPlayer.x = 200;
addChild(txtPlayer);
txtPlayer.text = str;
}
-
if(saveDataObject.data.characterName = playerName == null)
-
{ // checks if there is save data
trace("No Player data yet."); // if there isn't any data on the computer...
saveDataObject.data.characterName = playerName; // ...set the savedScore to 0
}
else
{
trace("Player data found."); // if we did find data...
loadData1(); // ...load the data
}
function loadData1():void
{
playerName = saveDataObject.data.characterName; // set the current score to the saved score
trace("Data Loaded!");
}
}
}
function saveData(e:TouchEvent):void
{
saveDataObject.data.characterName = playerName; // set the saved score to the current score
trace("Data Saved!");
saveDataObject.flush(); // immediately save to the local drive
trace(saveDataObject.size); // this will show the size of the save file, in bytes
}
Would be nice to have some more details about this error. Like which line is actually throwing this error.
However even without it there are few things that looks weird right away.
var playerName:int;
// And few lines below
playerName = txtPlayer;
// Few more lines below
var txtPlayer:TextField = new TextField();
Looks like you're declaring playerName as int so you want to store numeric values, but then txtPlayer is a TextField. So by playerName = txtPlayer; you're trying to store TextField as Int value, which is not gonna work.
I can also see
if(saveDataObject.data.characterName = playerName == null)
and it's like :O
I'm not even sure how this part is being compiled. You want to do playerName == null check (which would return true/false) and then assign it to saveDataObject.data.characterName ?
So I'm creating a platform game in Actionscript 3.0, trying to call a function that spawns blocks based on an array. The code is in a 'Game' class and is directed towards a movieclip on my .fla
When it is ran I get the error:
"cannot convert flash.display::Stage#2a2cdf99 to flash.display.MovieClip."
Here's the code:
public function GameScreen(stageRef:Stage = null )
{
this.stageRef = stageRef;
btnReturn.addEventListener(MouseEvent.MOUSE_DOWN, returnMainMenu, false, 0, true);
mcMain.addEventListener(Event.ENTER_FRAME, moveChar);
this.addEventListener(Event.ENTER_FRAME, createLvl);
this.stageRef.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
this.stageRef.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
this.stageRef.addChild(blockHolder);
}
And
private function createLvl(event:Event):void
{
var lvlArray:Array = MovieClip(root)['lvlArray' +lvlCurrent];
var lvlColumns:int = Math.ceil(lvlArray.length/16);
for(var i:int = 0;i<lvlArray.length;i++){
if(lvlArray[i] == 1){
if(i/lvlColumns == int(i/lvlColumns)){
row ++;
}
var newBlock:Block = new Block();
newBlock.graphics.beginFill(0xFFFFFF);
newBlock.graphics.drawRect(0,0,50,50);
newBlock.x = (i-(row-1)*lvlColumns)*newBlock.width;
newBlock.y = (row - 1)*newBlock.height;
blockHolder.addChild(newBlock);
} else if (lvlArray[i] == 'MAIN'){
mcMain.x = (i-(row-1)*lvlColumns)*newBlock.width;
mcMain.y = (row-1)*newBlock.height;
}
}
row = 0;
}
Please help =(
Thanks!
First of all:
Turn on "permit debugging" in your publish settings. This will give you line numbers with your errors, so you can determine the exact location of your error.
Post the entire stack trace. That error by itself is not a lot to go on.
Given the error and the code you've posted, the error must be caused by your use of MovieClip(root). The root property does not always point to the main timeline in Flash, it will point to the Stage if the display object is added directly to the stage. For example:
trace(stage.addChild(new Sprite()).root) // [object Stage]
Documentation on root: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html#root
In a function that creates a new timer every time it is called, is this the correct way to dispose of it?
private var _timers:Vector.<Timer> = new Vector.<Timer>;
private var _timer:Timer
private function timer():void
{
_timer = new Timer(10000, 1);
_timers.push(_timer);
_timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimer, false,0,true);
_timer.start();
}
private function onTimer(e:TimerEvent):void
{
e.target.removeEventListener(TimerEvent.TIMER_COMPLETE, onTimer);
_timers[0] = null;
_timers.shift();
}
Maybe you can add a
_timers[0].stop();
Just in case your times change for whatever reason, it's best to deal with the object directly instead of assuming the Timer object is at the 0 index.
Just a small adjustment required for onTimer():
private function onTimer(e:TimerEvent):void
{
var timer:Timer = e.currentTarget as Timer;
timer.removeEventListener(TimerEvent.TIMER_COMPLETE, onTimer);
_timers.splice(_timers.indexOf(timer), 1);
}
You don't need to set the index to null and it's never good practice to assume your item will be at a specified index, always make sure. In this case you make sure by using the indexOf() method available in the Array type object (_timers).
Hope it helps.
I've made function "delay" that handles delay calls. I made it for my game in which I have used delay calls alot and I had to find a way to handle delays efficiently. Still AS3 guru's out there may still find more efficient ways, please let know if any.
public static var timer_stack:Vector.<Timer> = new Vector.<Timer>();
public static function delay(delaytime:Number, func_name:Function, repeat:Number = 1)
{
var timer:Timer = new Timer(delaytime, repeat);
timer_stack.push(timer);
timer_stack[timer_stack.length-1].addEventListener(TimerEvent.TIMER, func_name ,false,0,true);
timer_stack[timer_stack.length-1].addEventListener(TimerEvent.TIMER_COMPLETE,
function(e:TimerEvent){ delay_complete(e, func_name); });
timer_stack[timer_stack.length-1].start();
}
public static function delay_complete(e, func_name:Functio):void
{
e.target.stop();
e.target.removeEventListener(TimerEvent.TIMER, func_name);
timer_stack[timer_stack.length-1].removeEventListener(TimerEvent.TIMER_COMPLETE,
function(){ func_name_complete(e, func_name);} );
for(var i=0; i < timer_stack.length; i++)
{
if(timer_stack[i].running == true)
trace("timer # "+i+" is running");
if(timer_stack[i].running == false)
{
timer_stack[i] = null;
timer_stack.splice(i,1);
trace("remove timer # "+i);
}
}
}
So my problem may be a little bigger than that, but I'm not certain. I'm creating a custom flash photo viewer for a client that dynamically loads the images with Loader objects. It takes a folder name in a specified directory on the server and calls a php page (using URLRequest) to get the workable contents of the folder. And then it loads it up from that array.
Here's the problem: it works FINE on my machine and every machine I've tested it on, but it DOESN'T work on my client's machine or some of his customer's machines. The loader bar that's at the bottom of the screen doesn't start to fill, which means that the images aren't starting to even load, and the status bar indicates that it continues to wait for a the server, yet never finishes waiting. This is consistent on a single machine, but varies across machines.
I've added diagnostics (a custom class I made to track certain vars and collect errors) to the thing to catch any errors, but it reports nothing on my client's machine. I've added flash version checking code, so the possibility of a lower version messing it up is gone...
I guess my question is, is there anything (security, environment, etc) that I could have overlooked that doesn't allow request objects or something to work on certain machines?? I've been ripping my hair out trying to figure this out!
My code (relevant sections):
public static const LOADLIMIT:int = 4;
public var paramObj:Object;
private var imageRequests:Vector.<URLRequest>;
private var loaderObj:Vector.<Loader>;
private var nextToLoad:int = -1;
//...
public function Player(){
//...
paramObj = loaderInfo.parameters;
if (!paramObj.root) paramObj.root = "http://site.com/images/";
paramObj.imgloc = paramObj.imgloc;
var res = bootstrapImages(paramObj.imgloc);
if (res is String){
loadErrorMsg.text = res;
loadErrorMsg.visible = true;
log.log(res);
}
//...
}
private function bootstrapImages(imgloc:String):*{
try{
if (!imgloc) return "No image location specified";
var req:URLRequest = new URLRequest(paramObj.root+"getdirlist.php?name="+imgloc);
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, directoryLoaded);
loader.addEventListener(IOErrorEvent.IO_ERROR, function(ex:IOError){
loadErrorMsg.text = "Error retreiving album data.";
loadErrorMsg.visible = true;
log.log(ex);
});
loader.load(req);
} catch (ex:Error){
log.log(ex); throw ex;
}
}
private function directoryLoaded(e:Event){
try{
//directory will come down as a json array
trace(e.target.data);
try {
var items:Array = JSON.decode(e.target.data);
} catch (ex:Error){
trace (ex.getStackTrace());
loadErrorMsg.text = "Error parsing album data.";
loadErrorMsg.visible = true;
log.log(ex);
return;
}
if (items.length == 0){
loadErrorMsg.text = "Invalid album name";
loadErrorMsg.visible = true;
log.log("Items length is 0.");
return;
}
imageRequests = new Vector.<URLRequest>();
loaderObj = new Vector.<Loader>();
for each(var item:String in items){
imageRequests.push(new URLRequest(paramObj.root+"coffeeimages/"+paramObj.imgloc+"/"+item));
loaderObj.push(null);
}
//...show UI...
for (var i:int = 0; i < LOADLIMIT; i++){
imageLoaded(null);
}
} catch (ex:Error){
log.log(ex); throw ex;
}
}
private function imageLoaded(e:Event){
try{
//if this was called as a result of the load event, start the slideshow! :D
if (e != null && e.target != null) {
//stage.addChild((e.target as LoaderInfo).loader);
trace(loaderObj[0]);
if (loaderObj[0] != null && //if the first image is loaded
loaderObj[0].contentLoaderInfo.bytesLoaded >= loaderObj[0].contentLoaderInfo.bytesTotal){
trace(loaderObj[0].contentLoaderInfo.bytesLoaded, loaderObj[0].contentLoaderInfo.bytesTotal);
loadErrorMsg.visible = false;
playSlideshow(true);
}
}
trace((e)?e.target:null, loaderObj);
nextToLoad++;
if (nextToLoad >= imageRequests.length) return;
var r:URLRequest = imageRequests[nextToLoad];
var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
l.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function(ex:ErrorEvent){
loadErrorMsg.text = "Error parsing image data.";
loadErrorMsg.visible = true;
log.log(ex);
});
l.load(r);
loaderObj[nextToLoad] = l;
log.addObjectWatch(l);
} catch (ex:Error){
log.log(ex); throw ex;
}
}
Also, here's the player live on the site. Ctrl+Alt+Home brings up my diagnostics panel. Please tell me if it doesn't work for you as well, so I can be more worried about this... :/ Thanks.
Edit: I have added more debugging information. Turns out that on the computers that cannot load it, they are getting an HTTP status 0 returned when trying to bootstrap the album information. I have no idea what that means...
So, the problem was a matter of the URL its requesting...
My client was typing "www.site.com", while my code was requesting to "site.com". To Adobe, this is a cross site request, and NOT ALLOWED by default. One solution (and probably the easiest one) is to add a file called "crossdomain.xml" to the root folder of the website. Flash looks for this file when making a request. One example that worked for me is as follows:
//crossdomain.xml
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="*.site.com" secure="false"/>
</cross-domain-policy>
Other solutions involve redirecting to one of the two ("site.com" redirects to "www.site.com"), or doing url rewriting, or getting url that they entered.
A few things I noticed, you are only listening for Event.COMPLETE and IOErrorEvent.IO_ERROR. I'd suggest also listening for HTTPStatusEvent.HTTP_STATUS, Event.OPEN, though I haven't added those in, they may provide useful information if you still aren't getting anywhere.
You should not need a LoaderContext object if you are only loading images and not accessing their content, so that (security) really should not be a problem. You should definitely separate out your logic in imageLoaded so that it is not both the initiator and the completion handler for your Loaders.
Anyway, no clue if this will be of use to you, but I've made your code substantially more readable and reworked some things that made little sense.
public static const LOADLIMIT:int = 4;
private var paramObj:Object;
private var imageRequests:Vector.<URLRequest>;
private var loaderObj:Vector.<Loader>;
private var nextToLoad:int = -1;
public function Player(){
paramObj = loaderInfo.parameters;
//what is this all about, your backup URL is useless?
if (!paramObj.root) {
paramObj.root = "http://site.com/images/";
}
var res = bootstrapImages();
if (res.length > 0){
loadErrorMsg.text = res;
loadErrorMsg.visible = true;
//log.log kills me
log.log(res);
}
}
private function bootstrapImages():String {
try {
var req:URLRequest = new URLRequest(paramObj.root + "getdirlist.php?name=" + paramObj.imgloc);
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, directoryLoaded);
loader.addEventListener(IOErrorEvent.IO_ERROR, dataIOError);
loader.load(req);
} catch (ex:Error) {
log.log(ex);
}
return "";
}
private function directoryLoaded(e:Event):void{
trace(e.target.data);
try {
var items:Array = JSON.decode(e.target.data);
} catch (ex:Error){
trace (ex.getStackTrace());
loadErrorMsg.text = "Error parsing album data.";
loadErrorMsg.visible = true;
log.log(ex);
return;
}
if (items.length == 0){
loadErrorMsg.text = "Invalid album name";
loadErrorMsg.visible = true;
log.log("Items length is 0.");
return;
}
imageRequests = new Vector.<URLRequest>();
loaderObj = new Vector.<Loader>();
for each(var item:String in items){
imageRequests.push(new URLRequest(paramObj.root+"coffeeimages/"+paramObj.imgloc+"/"+item));
loaderObj.push(new Loader());
}
for (var i:int = 0; i < LOADLIMIT; i++){
loadNextImage();
}
}
private function loadNextImage():void {
nextToLoad++;
if (nextToLoad < imageRequests.length) {
loaderObj[nextToLoad].contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
loaderObj[nextToLoad].contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, dataIOError);
try {
loaderObj[nextToLoad].load(imageRequests[nextToLoad]);
}
catch (e:Error) {
log.log(ex);
}
log.addObjectWatch(loaderObj[nextToLoad]);
}
}
private function dataIOError(e:IOError):void {
loadErrorMsg.text = "IOError: " + e.errorID + " - " + e.name + ", " + e.message;
loadErrorMsg.visible = true;
log.log(ex);
}
private function imageLoaded(e:Event){
//Start the slideshow if the first image has loaded
loadNextImage();
if (e.target == loaderObj[0]) {
trace(loaderObj[0].contentLoaderInfo.bytesLoaded, loaderObj[0].contentLoaderInfo.bytesTotal);
loadErrorMsg.visible = false;
playSlideshow(true);
}
}
Edit: You may simply wish to scrap your loading code entirely and instead use Bulk Loader
I like the crossdomain.xml solution mentioned above, but we had a similar problem on our client PC's. The problem was that adblocker browser plugins were blocking the URLRequest of the actionscript. We are currently finding a solution to this.