任何人都可以帮我如何访问fla文件中的数据到另一个fla文件?

时间:2014-02-05 11:37:13

标签: actionscript-3

任何人都可以帮我如何访问fla文件中的数据到另一个fla文件吗?

我有一个名为“savescore.fla”的起始fla文件,它连接到名为“score.as”的类。我有一个“btnAdd”按钮,如果单击它,它将加1到变量“scoring”在“score.as”课程中。我还有一个可以保存得分的动画片段“btnSave”。最后,我有一个“next_btn”按钮,它将直接转到我的另一个fla文件 这是“finalscore.fla”。我希望“finalscore.fla”显示“score.as”类中当前的“得分”变量,但问题是它只会显示0/0作为分数。我认为当“savescore.fla”保存并编辑“score.as”中的变量时,它只会对自身进行更改,而不会对“score.as”进行更改,这就是为什么我仍然将0作为最终得分的原因,因为它来自“score.as”而不是来自“savescore.fla”。所以我在想如果我能从“savescore.fla”中获取分数,它就能解决问题。但是我不知道要放什么代码......或者你可以用另一种方式给我?

以下是代码:(顺便说一下,我正在使用actionscript 3.0)

savescore.fla:

var lol:score = new score();

var saveDataObject:SharedObject;
//var currentScore:int;

init(); // this line goes directly beneath the variables

function init():void{ // call once to set everything up

     saveDataObject = SharedObject.getLocal("test"); // give the save data a location

     btnAdd.addEventListener(MouseEvent.CLICK, addScore); // clicking on +1
     btnSave.addEventListener(MouseEvent.CLICK, saveData); // clicking on Save
 next_btn.addEventListener(MouseEvent.CLICK, sunod); // clicking on +1

 if(saveDataObject.data.savedScore == null){ // checks if there is save data
      trace("No saved data yet."); // if there isn't any data on the computer...
      saveDataObject.data.savedScore = lol.scoring; // ...set the savedScore to 0

 } else {
      trace("Save data found."); // if we did find data...
      loadData(); // ...load the data
 }

 }


function addScore(e:MouseEvent):void{
     lol.scoring+=1; // add 1 to the score
}

function saveData(e:MouseEvent):void{
     saveDataObject.data.savedScore = lol.scoring; // 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
 trace(lol.GetFullScore());
}

function loadData():void{
     lol.scoring = saveDataObject.data.savedScore; // set the current score to the saved score

 trace("Data Loaded!");
}

function sunod(event:MouseEvent):void{

var ldr:Loader=new Loader();
ldr.load(new URLRequest("finalscore.swf"));
addChild(ldr);
}

score.as:

package{


public class score{

    public var scoring:int;



    function score()
    {

    }





public function SetScore(val:int):void 
{ 
  scoring = val;
} 





public function GetFullScore():int 
{ 
  return scoring;
  trace(scoring);
}


    }
}

finalscore.fla:

var lol1:score = new score();

txtScore.text = ("Score: " + lol1.GetFullScore()); // set the text property of the    txtScore


trace(lol1.GetFullScore());

2 个答案:

答案 0 :(得分:0)

加载外部swf后,您可以在onLoadedHandler中获取对它的引用。所以在savescore中:

var myReference:Object;

function loadComplete(event:Event):void {

    myReference= event.target.content;

        myReference.lol1.GetFullScore();
... or...
       var myScore = event.target.lol1.GetFullScore();
 etc...

}

您需要在使用之前确保该引用存在

修改

您的子SWF中的

创建一个存储父项的变量和方法:

var parentSWF:MovieClip;


function setParent(myParent:MovieClip):void{
    parentSWF = myParent;
}

然后在父母中向孩子发送一个参考:

function loadComplete(event:Event):void {

    myReference= event.target.content;

        // use the reference to child to call
        // any variables or functions in it
        myReference.lol1.GetFullScore();

        // set the reference in child with the following
        // so that the child can call the parent
        myReference.setParent(this);

}

然后在孩子中你可以使用lol:

 parentSWF.lol;

如果你必须采用这种方式,你最好在使用之前创建检查对象/方法等的措施

最终编辑

首先,移动

var ldr:Loader=new Loader();
从sunod函数中取出

并将其放在顶部,并使用另一个声明,以便loadcomplete可以使用它。您需要为loadComplete处理程序设置一个侦听器。把它放在你的其他听众之后

ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
在savecore add

中的loadcomplete处理程序中

   myReference.doThisWhenLoaded();

在finalscore put

function doThisWhenLoaded():void{
    txtScore.text = ("Score: " + parentSWF.lol);


}

这似乎完全矫枉过正,我确信我已经过度复杂了你的过度并发症(!)。我的建议是取消第二个swf并在preservecore中创建一个finalscore.as对象。这会容易得多。目前我无法理解为什么你想让另一个swf漂浮在周围。我从来没有成为多个swf的粉丝,所以其他人可能会跳进来纠正这个。

答案 1 :(得分:0)

以下是我的代码的更改:

<强> savescore.fla:

var lol:score = new score();

var saveDataObject:SharedObject;

init(); // this line goes directly beneath the variables

function init():void{ // call once to set everything up

 saveDataObject = SharedObject.getLocal("test"); // give the save data a location

 btnAdd.addEventListener(MouseEvent.CLICK, addScore); // clicking on +1
 btnSave.addEventListener(MouseEvent.CLICK, saveData); // clicking on Save
 next_btn.addEventListener(MouseEvent.CLICK, sunod); // clicking on +1

 if(saveDataObject.data.savedScore == null){ // checks if there is save data
      trace("No saved data yet."); // if there isn't any data on the computer...
      saveDataObject.data.savedScore = lol.scoring; // ...set the savedScore to 0

 } else {
      trace("Save data found."); // if we did find data...
      loadData(); // ...load the data
 }

}


function addScore(e:MouseEvent):void{
     lol.scoring+=1; // add 1 to the score
}

function saveData(e:MouseEvent):void{
     saveDataObject.data.savedScore = lol.scoring; // 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
     trace(lol.GetFullScore());
}

function loadData():void{
     lol.scoring = saveDataObject.data.savedScore; // set the current score to the saved score
     trace("Data Loaded!");
}

function sunod(event:MouseEvent):void{

var ldr:Loader=new Loader();
ldr.load(new URLRequest("finalscore.swf"));
addChild(ldr);
}

var myReference:Object;


function loadComplete(event:Event):void {

    myReference= event.target.content;

    myReference.lol1.GetFullScore();

    myReference.setParent(this);

}

<强> finalscore.fla:

var lol1:score = new score();
var parentSWF:MovieClip;

txtScore.text = ("Score: " + parentSWF.lol); // this line has the error because of parentSWF.lol

trace(lol1.GetFullScore());

function setParent(myParent:MovieClip):void{
    parentSWF = myParent;
}

<强> score.as:

package{

public class score{

    public var scoring:int;

    function score()
    {

    }


public function SetScore(val:int):void 
{ 
  scoring = val;
} 


public function GetFullScore():int 
{ 
  return scoring;
  trace(scoring);
}

 }
}