6 Replies Latest reply: Mar 16, 2011 2:35 PM by Smitch1581 RSS

    resetting an array

    Smitch1581 MeganK

      Hi all.

       

      I have created a game with my trusted AS3 CIB series! Its a simple game that when the user collects or drops 10 pieces of fruit the game ends. And displays if you won or lost. (Code is below)

       

      I have created a button (play_btn) that displays when the game finishes. What i am trying to achieve is when you press the button, the game starts again. I have created a listener and a function "startAgain" that resets the fruitLost and fruitCollected variable that i think i should do. But what i am having problems with is resetting the fruitsOnStage array and executing the for loop to create ten items of the fruitsOnStage array. I know the .splice command removes them from the array but what is the easiest way to add them back. Think i'm on the right lines but have no idea how to achieve this!!!

       

      Any help is greatly appreciated

       

      Cheers

       

       

      import flash.display.MovieClip;

      import flash.events.Event;

       

      var fruitArray:Array=new Array(Apple,Banana,Strawberry,Orange,Pear);

      var enemy:Array=new Array(blob);

      var fruitsOnStage:Array = new Array();

      play_btn.visible=false;

       

      var fruitLost:int=0;

      var fruitCollected:int=0;

       

       

      function startAgain(e:Event):void

      {

      fruitLost=0;

      fruitCollected=0;

      trace("Hello again");

      stage.addEventListener(Event.ENTER_FRAME, startGame);

       

       

       

      }

       

       

      for (var i:int = 0; i <10; i++) {

      var pickFruit=fruitArray[int(Math.random()*fruitArray.length)];

      var fruit:MovieClip = new pickFruit();

      addChild(fruit);

      fruit.x=Math.random()*stage.stageWidth;

      fruit.y=Math.random()*-500;

      fruit.speed=Math.random()*15+5;

      fruitsOnStage.push(fruit);

       

       

       

      }

       

       

       

      stage.addEventListener(Event.ENTER_FRAME, startGame);

       

      function startGame(e:Event):void

      {

      for (var i:int= 0; i <20; i++) {

       

      var currentFruit:MovieClip=fruitsOnStage[i];

      currentFruit.y+=currentFruit.speed;

      if (currentFruit.y>stage.stageHeight-currentFruit.height) {

      currentFruit.y=0-currentFruit.width;

      fruitLost++;

      field1_txt.text="You lost "+fruitLost+" pieces of fruit you loser";

       

       

      }

      if (currentFruit.hitTestObject(basket_mc)) {

      fruitCollected++;

      removeChild(currentFruit);

      field2_txt.text="You collected "+fruitCollected+" Bravo";

      fruitsOnStage.splice(i,1);

      }

      if (fruitsOnStage.length<=0) {

      field1_txt.text="You won!!!";

      play_btn.visible=true;

      stage.removeEventListener(Event.ENTER_FRAME, startGame);

      play_btn.addEventListener(MouseEvent.CLICK, startAgain);

      }

      if (fruitLost>=10) {

      field1_txt.text="You lost, MUPPET";

      play_btn.visible=true;

      play_btn.addEventListener(MouseEvent.CLICK, startAgain);

      stage.removeEventListener(Event.ENTER_FRAME, startGame);

       

      }

      }

      }

       

      basket_mc.addEventListener(MouseEvent.MOUSE_DOWN, startDragg);

       

      function startDragg(e:Event):void

      {

      basket_mc.startDrag();

      }

       

      basket_mc.addEventListener(MouseEvent.MOUSE_UP, stopDragg);

       

      function stopDragg(e:Event):void

      {

      basket_mc.stopDrag();

      }

       

        • 1. Re: resetting an array
          kglad CommunityMVP

          the easiest way to re-initialize an array is to use:

           

          fruitsOnStage = [];

          • 2. Re: resetting an array
            Smitch1581 MeganK

            Hi thanx for the reply.

             

            I tried adding the code but to no joy. I managed to get it to work if i copied and pasted the following and added it in the startAgain function. That did work but i don't believe i should do it like that as i should reuse that code instead of copying and pasting.

             

            Think i have got it now tho, i wrapped it in a function, and then created the play button to initiate that function. and i think its working well now.

             

            Is that the best way to do it?

             

            function startLoop(e:Event):void

            {

            for (var i:int = 0; i <10; i++) {

            var pickFruit=fruitArray[int(Math.random()*fruitArray.length)];

            var fruit:MovieClip = new pickFruit();

            addChild(fruit);

            fruit.x=Math.random()*stage.stageWidth;

            fruit.y=Math.random()*-500;

            fruit.speed=Math.random()*15+5;

            fruitsOnStage.push(fruit);

             

             

             

            }

            • 3. Re: resetting an array
              kglad CommunityMVP

              you're not re-initializing fruitsOnStage with that code.  you're just adding more fruit movieclips to fruitsOnStage.

              • 4. Re: resetting an array
                Andrei1 Community Member

                Your code should be something like below (I did not test it - just an idea). Also note that you declare instances in the loops which is very expensive. The code below remedies this as well.

                 

                import flash.display.MovieClip;
                import flash.events.Event;
                
                var fruitArray:Array = new Array(Apple, Banana, Strawberry, Orange, Pear);
                var enemy:Array = new Array(blob);
                var fruitsOnStage:Array = new Array();
                play_btn.visible = false;
                var fruitLost:int = 0;
                var fruitCollected:int = 0;
                
                basket_mc.addEventListener(MouseEvent.MOUSE_DOWN, startDragg);
                basket_mc.addEventListener(MouseEvent.MOUSE_UP, stopDragg);
                
                init();
                function init(e:Event = null):void {
                     fruitLost = 0;
                     fruitCollected = 0;
                     var pickFruit:Class;
                     var fruit:MovieClip;
                     fruitsOnStage = [];
                     for (var i:int = 0; i < 10; i++) {
                          pickFruit = fruitArray[int(Math.random() * fruitArray.length)];
                          fruit = new pickFruit();
                          addChild(fruit);
                          fruit.x = Math.random() * stage.stageWidth;
                          fruit.y = Math.random() * -500;
                          fruit.speed = Math.random() * 15 + 5;
                          fruitsOnStage.push(fruit);
                     }
                     stage.addEventListener(Event.ENTER_FRAME, startGame);
                }
                
                function startGame(e:Event):void {
                     var currentFruit:MovieClip;
                     for (var i:int = 0; i < fruitsOnStage.length; i++) {
                          currentFruit = fruitsOnStage[i];
                          currentFruit.y += currentFruit.speed;
                          if (currentFruit.y > stage.stageHeight - currentFruit.height) {
                               currentFruit.y = 0 - currentFruit.height;
                               fruitLost++;
                               field1_txt.text = "You lost " + fruitLost + " pieces of fruit you loser";
                          }
                
                          if (currentFruit.hitTestObject(basket_mc)) {
                               fruitCollected++;
                               removeChild(currentFruit);
                               field2_txt.text = "You collected " + fruitCollected + " Bravo";
                               fruitsOnStage.splice(i, 1);
                          }
                
                          if (fruitsOnStage.length == 0) {
                               field1_txt.text="You won!!!";
                               play_btn.visible = true;
                               stage.removeEventListener(Event.ENTER_FRAME, startGame);
                               play_btn.addEventListener(MouseEvent.CLICK, init);
                          }
                
                          if (fruitLost >= 10) {
                               field1_txt.text="You lost, MUPPET";
                               play_btn.visible = true;
                               play_btn.addEventListener(MouseEvent.CLICK, init);
                               stage.removeEventListener(Event.ENTER_FRAME, startGame);
                          }
                     }
                }
                
                function startDragg(e:Event):void {
                     basket_mc.startDrag();
                }
                
                function stopDragg(e:Event):void {
                     basket_mc.stopDrag();
                }
                
                
                

                 

                Message was edited by: Andrei1

                • 5. Re: resetting an array
                  Andrei1 Community Member

                  Another thing - you don't remove previous fruits from stage. This code does it in removeAll() function:

                   

                  import flash.display.MovieClip;
                  import flash.events.Event;
                  
                  var fruitArray:Array = new Array(Apple, Banana, Strawberry, Orange, Pear);
                  var enemy:Array = new Array(blob);
                  var fruitsOnStage:Array = new Array();
                  play_btn.visible = false;
                  var fruitLost:int = 0;
                  var fruitCollected:int = 0;
                  
                  basket_mc.addEventListener(MouseEvent.MOUSE_DOWN, startDragg);
                  basket_mc.addEventListener(MouseEvent.MOUSE_UP, stopDragg);
                  
                  init();
                  function init(e:Event = null):void {
                       // reset array
                       removeAll();
                       fruitLost = 0;
                       fruitCollected = 0;
                       var pickFruit:Class;
                       var fruit:MovieClip;
                       
                       for (var i:int = 0; i < 10; i++) {
                            pickFruit = fruitArray[int(Math.random() * fruitArray.length)];
                            fruit = new pickFruit();
                            addChild(fruit);
                            fruit.x = Math.random() * stage.stageWidth;
                            fruit.y = Math.random() * -500;
                            fruit.speed = Math.random() * 15 + 5;
                            fruitsOnStage.push(fruit);
                       }
                       stage.addEventListener(Event.ENTER_FRAME, startGame);
                  }
                  
                  function removeAll():void {
                       for each(var mc:MovieClip in fruitsOnStage) {
                            removeChild(mc);
                            mc = null;
                       }
                       fruitsOnStage = [];
                  }
                  
                  function startGame(e:Event):void {
                       var currentFruit:MovieClip;
                       for (var i:int = 0; i < fruitsOnStage.length; i++) {
                            currentFruit = fruitsOnStage[i];
                            currentFruit.y += currentFruit.speed;
                            if (currentFruit.y > stage.stageHeight - currentFruit.height) {
                                 currentFruit.y = 0 - currentFruit.height;
                                 fruitLost++;
                                 field1_txt.text = "You lost " + fruitLost + " pieces of fruit you loser";
                            }
                  
                            if (currentFruit.hitTestObject(basket_mc)) {
                                 fruitCollected++;
                                 removeChild(currentFruit);
                                 field2_txt.text = "You collected " + fruitCollected + " Bravo";
                                 fruitsOnStage.splice(i, 1);
                            }
                  
                            if (fruitsOnStage.length == 0) {
                                 field1_txt.text="You won!!!";
                                 play_btn.visible = true;
                                 stage.removeEventListener(Event.ENTER_FRAME, startGame);
                                 play_btn.addEventListener(MouseEvent.CLICK, init);
                            }
                  
                            if (fruitLost >= 10) {
                                 field1_txt.text="You lost, MUPPET";
                                 play_btn.visible = true;
                                 play_btn.addEventListener(MouseEvent.CLICK, init);
                                 stage.removeEventListener(Event.ENTER_FRAME, startGame);
                            }
                       }
                  }
                  
                  function startDragg(e:Event):void {
                       basket_mc.startDrag();
                  }
                  
                  function stopDragg(e:Event):void {
                       basket_mc.stopDrag();
                  }
                  
                  
                  
                  • 6. Re: resetting an array
                    Smitch1581 MeganK

                    Thats great. Thank you both so much. Both of the codes worked but have used the last one as its a great idea to remove the fruits before the game starts again. Your help is greatly appreciated.

                     

                    Cheers