Search code examples
androidactionscript-3flashair

I can't display Shared object another SWF on AIR


I have created an test app for test shared objects.I have three fla,two game and one main fla for call other swf files.

Main FLA

import tsiko;

var mypipi:Loader = new Loader();                     // create a new instance of the Loader class
var myurl:URLRequest = new URLRequest("game2.swf"); // This is game 2

var mypipi2:Loader = new Loader();                     
var myurl2:URLRequest = new URLRequest("game1.swf"); //This is game 1 in this case both SWFs are in the same folder 



wh3gna.addEventListener(MouseEvent.CLICK, dinsvslov);

function dinsvslov(event:MouseEvent):void
{


    mypipi.load(myurl);                                     // load the Game 2
addChild(mypipi); 


}





stage.addEventListener(Event.ENTER_FRAME,bibisa);
function bibisa(oly:Event) {


    //if game 2 done than call the game 1 and remove game 2


    if(tsiko.sansangelme==true){

        mypipi2.load(myurl2);                                     // load the SWF file
addChild(mypipi2);



        mypipi.unloadAndStop();

        tsiko.sansangelme=false;


}


}

Game 2 save the number.

import tsiko;
import flash.net.SharedObject;
import flash.events.NetStatusEvent;

tsiko.peko=555;

var bsa4:SharedObject = SharedObject.getLocal("goster");

bsa4.data.tok = tsiko.peko;



bsa4.flush();


var flashstatus:String=bsa4.flush();
if(flashstatus !=null){
    switch(flashstatus){
case SharedObjectFlushStatus.PENDING:
trace("waiting")
break;
case SharedObjectFlushStatus.FLUSHED:
trace("great saved")
break;
}
}

tsiko.sansangelme=true; // Goto main menu 


stop();

And game 1 for show the shared number but it's not working.

import tsiko;
import Turn;
import flash.net.SharedObject;
import flash.events.NetStatusEvent;


var bsa1:SharedObject = SharedObject.getLocal("goster");

var myno:Number=0;


myno=bsa1.data.csok;


tipo.text = String(myno);


stop();

Game 2 flushed the number perfect but I can't see on the game 1 swf.I get NaN message.How should I do for display the data on game 2.

**UPDATE *****

· I use 2 swf in the same app.I check the Local Connection and it says:

" AIR profile support: This feature is supported on all desktop operating systems and on all AIR for TV devices, but is not supported on mobile devices. You can test for support at run time using the LocalConnection.isSupported property. See AIR Profile Support for more information regarding API support across multiple profiles. "

So if I can't use on mobile its not required,is it ?

·I try to flush value on main fla(goster)

var bsa4:SharedObject = SharedObject.getLocal("goster");

bsa4.data.toka = tsiko.peko;






var flashstatus:String=bsa4.flush();

·I use this statement in Event listener on main fla to.

if(tsiko.skordangelme==true){
    Skoryukle.unloadAndStop();

    trace(tsiko.peko + "Last score");
    bsa4.flush(); //So if game completed and user come back the main menu then save the score.

    tsiko.skordangelme=false;

}

·I can catch the value(tsiko.peko) on main fla,but its not saving.Also when I check the shared folder..

C:\Users\miko\AppData\Roaming\goster\Local Store#SharedObjects

I can see the 3 swf folder.

game2.swf goster.swf game1.swf

What Should I do now ?


Solution

  • Side note. If you're setting up communication between two SWFs, then shared object might be one of the worst solutions to use. If these SWFs are in the same application, just use shared classes or something. Worse, yet still viable: use LocalConnection, it provides event handling on data arrival.

    But oh well. There are conditions to be met for two different SWFs to use the same shared object.

    • They should exist in the same security domain. Basically, you cannot share the same shared object between SWFs loaded from different domains. It is not a problem for AIR application.
    • Use the same second argument: var SO:SharedObject = SharedObject.getLocal("goster", "/"); If you don't, it uses the full SWF path by default, that's why your two SWFs access distinct shared objects.
    • Synchronization. Obviously. You cannot read any data from shared object that are not written there yet.
    • Read/write synchronization. I am quite sure that Flash Player, once connected to local shared object, does not monitor its changes runtime, so you probably cannot open it and, like, wait for changes. You always need to open-write-flush-close it and open-read-close it respectively to ensure you're accessing the actual data.