AS3 Flash如何在游戏中保存得分并在不同的框架上显示

时间:2014-02-07 22:27:26

标签: actionscript-3 flash

我正在制作一个每级有3轮的琐事游戏。在玩游戏的框架上,您可以看到一个包含当前分数的框,当用户完成该轮时,他们将被带到另一个框架,在那里您可以看到所有三轮的分数。我似乎无法正确显示分数。这是一张大致说明我希望如何显示分数的图像。

这是我在round1上的代码

    import flash.display.MovieClip;
    import flash.events.MouseEvent;

    //Button 
    btn_six.addEventListener(MouseEvent.MOUSE_DOWN, onPressHandler);

    btn_six.buttonMode = true;
    btn_six.useHandCursor = true;

    function onPressHandler(myEvent:MouseEvent){
    trace('Press');
    btn_six.gotoAndPlay(5);
    }

    var saveDataObject:SharedObject;
    var currentScore:int;

    init();

    function init():void{

saveDataObject = SharedObject.getLocal("test");
currentScore = 0;

addEventListener(Event.ENTER_FRAME, saveData);

if(saveDataObject.data.savedScore == null){
    trace("No saved data yet.");
    saveDataObject.data.savedScore = currentScore;
} else {
    trace("Save data found.");
    loadData();
}
    }


    function saveData(e:Event):void{
        saveDataObject.data.savedScore = currentScore;
        trace("Data Saved!");
        saveDataObject.flush();
        trace(saveDataObject.size);
    }

    function loadData():void{
        currentScore = saveDataObject.data.savedScore;
        trace("Data Loaded!");
    }







    var questions:Array=['"I look like a dangling parsnip in this!" she cried. "I will never __________ these jeans!',
                 "Tell the freckled parrot to put his car in __________.",
                 "When purchasing a buttercream trowel, always choose one made of __________.",
                 "Everybody has a cracked snowflake. The question is, __________ one is yours? ",
                 "Standing at the dock, the loopy basket watched the cruise ship set __________ without him. ",];
    var answers:Array=[   ["wear","where"], ["idle","idol"], ["steel","steal"] ,[ "which","witch"] ,[ "sail","sale"] ];




     var qno=0;var rnd1; var rnd2;
      tick.visible=false;cross.visible=false;incorrect0.visible=false;incorrect1.visible=false;incorrect2.visible=false;incorrect3.visible=false;
    var right_answers=0;
    var wrong_answers=0;


    function change_question(){
if(tick.visible){right_answers++;}
if(cross.visible){wrong_answers++;}
if(qno==questions.length){gotoAndPlay(2);}else{
tick.visible=false;cross.visible=false;
rnd1=Math.ceil(Math.random()*2);
rnd2=Math.ceil(Math.random()*questions.length)-1;
q.text=questions[rnd2];
if(questions[rnd2]=="x"){change_question();}
questions[rnd2]="x";
enable_disable(1);
if(rnd1==1){opt1.text=answers[rnd2][0];opt2.text=answers[rnd2][1];}
if(rnd1==2){opt1.text=answers[rnd2][1];opt2.text=answers[rnd2][0];}
//if(rnd1==3){opt1.text=answers[rnd2][1];opt2.text=answers[rnd2][2];opt3.text=answers[rnd2][0];}
if(wrong_answers==0){incorrect0.visible=true;}
if(wrong_answers==1){incorrect1.visible=true;}
if(wrong_answers==2){incorrect2.visible=true;}
if(wrong_answers==3){incorrect3.visible=true;}
if(wrong_answers==3){gotoAndPlay(3);}
}}

    function enable_disable(a){
if(a==0){shade1.mouseEnabled=false;shade2.mouseEnabled=false;}
if(a==1){shade1.mouseEnabled=true;shade2.mouseEnabled=true;}}

    change_question();

    function ButtonAction1(eventObject:MouseEvent) {qno++;change_question();} 

    shade1.addEventListener(MouseEvent.CLICK, ButtonAction2);
    shade2.addEventListener(MouseEvent.CLICK, ButtonAction3);


    function ButtonAction2(eventObject:MouseEvent) {
enable_disable(0);if(rnd1==1)   {tick.visible=true;tick.y=shade1.y}else{cross.visible=true;cross.y=shade1.y}
qno++;change_question();
}
    function ButtonAction3(eventObject:MouseEvent) {
enable_disable(0);if(rnd1==2){tick.visible=true;tick.y=shade2.y}else{cross.visible=true;cross.y=shade2.y}
qno++;change_question();
}


    stop();

在第1轮清除时,代码如下所示:

    ra.text=right_answers +"/5";

    if(wrong_answers==3){gotoAndPlay(3);}

第2轮的代码与第1轮的代码相同 这是第二轮的代码清除:

    ra2.text=right_answers_r2 +"/10";

    if(wrong_answers==3){gotoAndPlay(3);}

    addEventListener(Event.ENTER_FRAME,myFunction);
    function myFunction(event:Event) {
        trace("Do Something");
        showScoreText();
    }

    function showScoreText():void{
        MovieClip(root).round1.loadData();
        ra.text = MovieClip(root).round1.loadData("");
        trace("Score text visible");
    }

3 个答案:

答案 0 :(得分:0)

loadData功能更改为:

function loadData(){
    currentScore = saveDataObject.data.savedScore;
    trace("Data Loaded!");
    return currentScore;
    //^One does not simply expect something returned without returning anything
}

并且没有可接受的参数,因此请更改

ra.text = MovieClip(root).round1.loadData("");

ra.text = MovieClip(root).round1.loadData();

答案 1 :(得分:0)

试试这个,

假设您已完成第1轮并且您的当前分数为10,那么现在您将进入第2帧的第2轮,并且您在舞台上有分数文本字段,现在您必须附加如下score_txt.appendText(String(currentScore)+"\n");的文本这有助于看到第1轮和第1轮的得分。第2轮继续这样做。而已。

答案 2 :(得分:0)

您可以在关联数组的每一轮结束时存储currentScore并显示它。

var scorearray:Array = [{scored:5, outof:10},{scored:10, outof:20},{scored:20, outof:30}];
function displayScore():void
{
    for(var i:int=0;i<scorearray.length; i++)
    {
        var texts:TextField = new TextField();
            texts.text = scorearray[i].scored +"/" + scorearray[i].outof;
            texts.y = i * 20 + 30;
            addChild(texts)
    }
}

displayScore();