AS3: Refresh a Function

馋奶兔 提交于 2019-12-23 06:14:33

问题


In Action Script 3 is there a way to refresh a function? I guess what I am looking for is when I switch screens for a function to reset or refresh, also the function is tied into an Array so I don't know if that will effect anything. The program is working like this there are 59 numbers the user picks 5. So here is the code that I am using and the code I am trying to reset.

    var pickFive:Array = new Array (5);
    var m_iNextElement:int = 0;
    var b1:TextField = new TextField();
    var b2:TextField = new TextField();
    var b3:TextField = new TextField();
    var b4:TextField = new TextField();
    var b5:TextField = new TextField();

    var myFormat:TextFormat = new TextFormat();
    myFormat.size = 45;

    Game.board.btn_1.addEventListener(MouseEvent.CLICK, btn1);

//Trying to reset this
    function btn1(event:MouseEvent):void{
        if(m_iNextElement <= 4){
        Game.board.btn_1.gotoAndStop(2);
        pickFive[m_iNextElement] = 1;
        m_iNextElement++;

        b1.defaultTextFormat = myFormat;
        b1.text = pickFive[0];
        b1.x = 510;
        b1.y = 103;
        addChild(b1);

        b2.defaultTextFormat = myFormat;
        b2.text = pickFive[1];
        b2.x = 593;
        b2.y = 103;
        addChild(b2);

        b3.defaultTextFormat = myFormat;
        b3.text = pickFive[2];
        b3.x = 673;
        b3.y = 103;
        addChild(b3);

        b4.defaultTextFormat = myFormat;
        b4.text = pickFive[3];
        b4.x = 760;
        b4.y = 103;
        addChild(b4);

        b5.defaultTextFormat = myFormat;
        b5.text = pickFive[4];
        b5.x = 840;
        b5.y = 103;
        addChild(b5);
        }
    }

回答1:


To have a reset you need simply need to create a reset function. That is to say a function that contains instructions to "adjust various variables as preferred until considered to be a reset". Then whenever you need that to happen you simply trigger the execution of that function eg: do_Reset(); Make sure that do_Reset is not an event-based function eg: function do_Rest (event:MouseEvent):void will not work if you try to execute it manually cos ONLY a mouse event can trigger that function if it's setup like that..

Anyway it sounds like you're not actually looking for what most would call a reset but more what would be called a re-run. Reset means you start with nothing, things happen and to reset is to go back to nothing again like a new beginning, Re-run is when things are happening and you want something to re-happen.

So onto a possible solution..

    var pickFive:Array = new Array(5);
    var m_iNextElement:int = 0;
    var b1:TextField = new TextField();
    var b2:TextField = new TextField();
    var b3:TextField = new TextField();
    var b4:TextField = new TextField();
    var b5:TextField = new TextField();

    var myFormat:TextFormat = new TextFormat();
    myFormat.size = 45;

    Game.board.btn_1.addEventListener(MouseEvent.CLICK, btn1);

    function btn1(event:MouseEvent):void
    {
        trace(" *** Clicked btn1 : Setting up game... ");

        Game.board.btn_1.gotoAndStop(2);

        setup_Board(); //to manually run that function
    }

    //Trying to reset this...
    function setup_Board():void
    {
        if (m_iNextElement <= 4)
        {

            pickFive[m_iNextElement] = 1;
            m_iNextElement++;

            b1.defaultTextFormat = myFormat;
            b1.text = pickFive[0];
            b1.x = 510; b1.y = 103; addChild(b1);

            b2.defaultTextFormat = myFormat;
            b2.text = pickFive[1];
            b2.x = 593; b2.y = 103; addChild(b2);

            b3.defaultTextFormat = myFormat;
            b3.text = pickFive[2];
            b3.x = 673; b3.y = 103; addChild(b3);

            b4.defaultTextFormat = myFormat;
            b4.text = pickFive[3];
            b4.x = 760; b4.y = 103; addChild(b4);

            b5.defaultTextFormat = myFormat;
            b5.text = pickFive[4];
            b5.x = 840; b5.y = 103; addChild(b5);
        }
    }

    function Game_Over():void
    {
        trace(" *** Game Over : Re-set game for new try... ");

        Game.board.btn_1.gotoAndStop(1);

        reset_Board(); //to manually run that function
    }

    function reset_Board():void
    {
        b1.text = b2.text = b3.text = b4.text = b5.text = "";

        removeChild(b1); removeChild(b2); removeChild(b3);
        removeChild(b4); removeChild(b5);

        trace(" *** Re-set complete... ");

        Game.board.btn_1.addEventListener(MouseEvent.CLICK, btn1);

        //// Or manual function re-run without waiting for above mouse click
        //setup_Board();
    }

Also you're gunna need a game over function to take care of clearing things up. This can be a Mouse_Event or Timer_Event (eg: MClip with "restart" is shown and its click listener should target the Game Over function I've included. Or timer just manually runs Game_Over(); after certain time countdown.

Otherwise just multiple re-running will cause memory usage to increase and just generally slow the system. Consider your mouse click that just keeps adding 5 movieclips, after 10 clicks you have 50 MClips on screen and in RAM memory somewhere but after X-num hours of clicking you would be blaming Adobe for being such bad programmers who couldnt get Flash "right" without it killing your computer. And threatening to take your skills to a better system like HTML or MS Publisher (..just joking)



来源:https://stackoverflow.com/questions/24271396/as3-refresh-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!