检索场景之间的sharedObjects

时间:2015-12-15 00:20:17

标签: actionscript-3 flash shared-objects

我想将shower_cost的变量从第一个场景传递到第二个场景。变量将以动态文本显示。 当在第二个场景上点击显示按钮时,将出现show_cost。

这里的问题似乎是在检索共享对象时出现了问题,因为在显示按钮被点击时它没有显示成本。

第一个场景动作

import flash.events.MouseEvent;
import flash.net.SharedObject;

shower_ok_btn.addEventListener(MouseEvent.CLICK, shower_multiplyClick);
shower_back_btn.addEventListener(MouseEvent.CLICK, shower_backClick);

var shower_multiSym:Boolean = false;
var shower_backSym:Boolean = false;

shower_usage.restrict="0-9";
shower_power.restrict="0-9";
shower_power.border=true;
shower_usage.border=true;

var shower_input1:String;
var shower_input2:String;
var shower_multiResult:Number;
var shower_costResult:Number;
var show_shower=SharedObject.getLocal("shower");

function shower_multiplyClick(event:MouseEvent):void{
    shower_multiSym=true;
    shower_backSym=false;


    show_shower.data.showcost =shower_cost.text;
    show_shower.data.showpower=shower_total_power.text;

    shower_text_total_power.text="total power(kwh):";
    shower_text_cost.text="Cost (rm):";

    shower_total_power.border=true;
    shower_cost.border=true;

    shower_input1=shower_usage.text;
    shower_input2=shower_power.text;

    if(shower_multiSym==true){
        shower_multiResult=parseInt(shower_input1)*parseInt(shower_input2)/1000;
        shower_multiResult.toString();
        shower_total_power.text = String(shower_multiResult);
    }
    else{
        shower_total_power.text="Please Choose an option";
    }

    shower_costResult=.218*(parseInt(shower_input1)* parseInt(shower_input2))/1000;
    shower_costResult.toString();
    shower_cost.text=String(shower_costResult);
    trace(shower_costResult);

}

function shower_backClick(event:MouseEvent):void{
    shower_multiSym=false;
    shower_backSym=true;
    gotoAndStop(1,"Scene 2");
}

stop();

第二个场景动作:

import flash.net.SharedObject;

show_btn.addEventListener(MouseEvent.CLICK,showReport);

var state=0;
function showReport(e:MouseEvent):void{

    var state=1;

    if(state==1){

    Report_cost_shower.border=true;
    var show_shower2=SharedObject.getLocal("shower");
    Report_cost_shower.text=show_shower2.data.showcost;

}
}
stop();

1 个答案:

答案 0 :(得分:0)

我不建议使用SharedObject在对象之间传递数据,它很慢且不可靠。 我一直在使用Global类 - 请参阅 https://github.com/inruntime/AS3-Global-Object

你可以这样使用它 -

import com.inruntime.utils.*;

    // initialize the global object
    // you have to repeat this step in every class that will use the global
    private var $:Global = Global.getInstance();

    $.show_shower = shower_cost.text;