4 Replies Latest reply on Oct 25, 2010 11:53 PM by srirama.83

    object staying after changing scene - need help

    nuxnux

      okey i am making this simple drag and drop game using flash cs3 using as2.

       

      i got 4 games and it has the same codes but different game type.. basically it uses drag and drop.

      here is the problem, when i finish the game, and press the back button to go to the selecting game scene, the last object dropped in the previous game stays and appears in the scene after that.

       

       

      below is the as2 code i am using.

       

       

      ---

       

      // global variables
      var tabbingON = false;
      var draggingON = false;
      var selectedPiece = 0;
      var totalPieces = 10;
      var total = 0;
      var mySound = new Sound();
      var origPos:Array = new Array();
      var justHit = false;
      var offset = 50;
      var counter = 0;

       


      // function calls
      initGame();

       

      // track to see if user is tabbing
      function down( ) {
        if (x = Key.isDown(Key.TAB)) {
          tabbingON = true;
          btn_access._visible = true;
          }
          delete this.onKeyDown;
          this.onKeyUp = up;
      }
      function up( ) {
        this.onKeyUp = undefined;
        this.onKeyDown = down;
      }
      var keyListener:Object = new Object( );
      keyListener.onKeyDown = down;
      Key.addListener(keyListener);

       

      // check to see if the mouse is moved
      onMouseMove = function () {
          tabbingON = false;
          btn_access._visible = false;
          if (selectedPiece > 0) {
              _root["dd" + selectedPiece].startDrag(true);
          }
      }

       

      // puzzle pieces
      dd1.onPress =
      dd2.onPress =
      dd3.onPress =
      dd4.onPress =
      dd5.onPress =
      dd6.onPress =
      dd7.onPress =
      dd8.onPress =
      dd9.onPress =
      dd10.onPress =
      function() {
          stopDrag();
          if (tabbingON || !draggingON) {
              pickupPiece(this._name);
          }
          // if puzzle piece is in right spot, then place
          else if (eval(_root[this._name]._droptarget) == _root["hitarea" + selectedPiece]) {
              placePiece(this._name);
              counter++;
              reply_txt.text = "Good Job!";
              playSound("5.3right");
          // if puzzle piece is not in right spot, then return it
          } else {
              returnPiece(this._name);
              reply_txt.text = "Try again!";
              playSound("5.3beep-10");
          }
              if(counter == 10){
              reply_txt.text = "Finish!";
              playSound("5.3greatjob1");
          }

       

      }
      dd1.onRollOver =
      dd2.onRollOver =
      dd3.onRollOver =
      dd4.onRollOver =
      dd5.onRollOver =
      dd6.onRollOver =
      dd7.onRollOver =
      dd8.onRollOver =
      dd9.onRollOver =
      dd10.onRollOver =
      function() {
          if (tabbingON) {
              n1 = Number(subString(this._name,3,4));
              playSound("pp" + n1);
          }
      }       
      dd1.onRollOut =
      dd2.onRollOut =
      dd3.onRollOut =
      dd4.onRollOut =
      dd5.onRollOut =
      dd6.onRollOut =
      dd7.onRollOut =
      dd8.onRollOut =
      dd9.onRollOut =
      dd10.onRollOut =
      function() {
          n1 = Number(subString(this._name,3,4));
          takeOut(n1);
      }

       

      // hitareas
      hitarea1.onPress =
      hitarea2.onPress =
      hitarea3.onPress =
      hitarea4.onPress =
      hitarea5.onPress =
      hitarea6.onPress =
      hitarea7.onPress =
      hitarea8.onPress =
      hitarea9.onPress =
      hitarea10.onPress =
      function() {
          n1 = Number(subString(this._name,8,9));
          if (tabbingON) {
              // when no piece is picked up
              if (selectedPiece == 0) {
                  playSound("nopiece");
              // when it is in the right place, place it
              } else if (_root.selectedPiece == n1) {
                  placePiece("dd" + selectedPiece);
              // when it is in the wrong piece, play message
              } else {
                  returnPiece("dd" + selectedPiece);
              }
          }
      }
      hitarea1.onRollOut =
      hitarea2.onRollOut =
      hitarea3.onRollOut =
      hitarea4.onRollOut =
      hitarea5.onRollOut =
      hitarea6.onRollOut =
      hitarea7.onRollOut =
      hitarea8.onRollOut =
      hitarea9.onRollOut =
      hitarea10.onRollOut =
      function() {
          n1 = Number(subString(this._name,8,9));
          takeOut(n1);
      }
      hitarea1.onRollOver =
      hitarea2.onRollOver =
      hitarea3.onRollOver =
      hitarea4.onRollOver =
      hitarea5.onRollOver =
      hitarea6.onRollOver =
      hitarea7.onRollOver =
      hitarea8.onRollOver =
      hitarea9.onRollOver =
      hitarea10.onRollOver =
      function() {
          if (tabbingON) {
              n1 = Number(subString(this._name,8,9));
              playSound("spot" + n1);
          }
      }
         
      // functions
      function initGame() {
          // hide The Access Button
          btn_access._visible = false;
         
          // initialize array with original x and y positions of the puzzle pieces
          for (i = 1; i <= totalPieces; i++) {
              origPos[i] = new Array(2);
              origPos[i][0] = _root["dd" + (i)]._x;
              origPos[i][1] = _root["dd" + (i)]._y;
          }

       

          // accessibility
          btn_access.tabIndex = 1;
          dd2.tabIndex = 2;
          dd3.tabIndex = 3;
          dd5.tabIndex = 4;
          dd4.tabIndex = 5;
          dd1.tabIndex = 6;
          dd6.tabIndex = 7;
          dd7.tabIndex = 8;
          dd8.tabIndex = 9;
          dd9.tabIndex = 10;
          dd10.tabIndex = 11;
          hitarea1.tabIndex = 12;
          hitarea2.tabIndex = 13;
          hitarea3.tabIndex = 14;
          hitarea4.tabIndex = 15;
          hitarea5.tabIndex = 16;
          hitarea6.tabIndex = 17;
          hitarea7.tabIndex = 18;
          hitarea8.tabIndex = 19;
          hitarea9.tabIndex = 20;
          hitarea10.tabIndex = 21;
      }
      function pickupPiece(ppName) {
          _root[ppName].swapDepths(10);
          draggingON = true;
          selectedPiece = Number(subString(ppName,3,4));
          if (tabbingON) {
              playSound("yourturn03");
          }
      }
      function placePiece(ppName) {
          if (tabbingON) {
              playSound("right");
          }
          _root[ppName]._x = _root["hitarea" + selectedPiece]._x;
          _root[ppName]._y = _root["hitarea" + selectedPiece]._y;
          justHit = true;
          draggingON = false;
          total += 1;
          selectedPiece = 0;
          checkFinal();
      }
      function returnPiece(ppName)  {
          if (tabbingON) {
              playSound("wrong");
          }
          _root[ppName]._x = origPos[selectedPiece][0];
          _root[ppName]._y = origPos[selectedPiece][1];
          draggingON = false;
          selectedPiece = 0;
      }
      //function takeOut(n1) {
          //if (justHit) {
              //trace("pp" + n1+ "justHit");
              //_root["pp" + n1].tabIndex = -1;
              //_root["pp" + n1].enabled = false;
              ///_root["hitarea" + n1].tabIndex = -1;
              //_root["hitarea" + n1].enabled = false;
              //justHit = false;
          //}
      //}
      function playSound(nameSound) {
          stopAllSounds();   
          mySound.attachSound(nameSound);
          mySound.start();   
      }
      function checkFinal() {
          if (total >= 5) {
              if (tabbingON) {
                  //playSound("final");
              }
          }
      }

       

      // buttons
      btn_access.onRollOver = function() {
              //playSound("access");   
      }
      btn_access.onRollOut = function() {
          stopAllSounds();   
          mc_keypress.removeMovieClip();
      }
      btn_access.onRelease = function() {
          if (total <5) {
              //playSound("expl");
              _root.attachMovie("mc_keypress", "mc_keypress",11);
              mc_keypress._x += offset;
              mc_keypress._y += offset;
          } else {
              //playSound("expl2");
          }
      }

       

      ---

       

      i really need help on this whoever that is good with flash.

        • 1. Re: object staying after changing scene - need help
          Ned Murphy Adobe Community Professional & MVP

          If you can highlight the relevant parts in your code where the last object gets dropped, and where that object was created to begin with, you stand a better chance of getting some help.  Otherwise it is more like a research project trying to figure out what's where, the effort of which few people might want to entertain.

          • 2. Re: object staying after changing scene - need help
            srirama.83

            From whatever I understood , There seems to be problem of some movieclips not cleared when you wanted it to clear.

            I feel it may be because of swapDepth to some level

            • 3. Re: object staying after changing scene - need help
              nuxnux Level 1

              function() {
                  stopDrag();
                  if (tabbingON || !draggingON) {
                      pickupPiece(this._name);
                  }
                  // if puzzle piece is in right spot, then place
                  else if (eval(_root[this._name]._droptarget) == _root["hitarea" + selectedPiece]) {
                      placePiece(this._name);
                      counter++;
                      reply_txt.text = "Good Job!";
                      playSound("5.3right");
                  // if puzzle piece is not in right spot, then return it
                  } else {
                      returnPiece(this._name);
                      reply_txt.text = "Try again!";
                      playSound("5.3beep-10");
                  }
                     if(counter == 10){
                      reply_txt.text = "Finish!";
                      playSound("5.3greatjob1");

                  }

               

              Okey that is the last object to be place.. when the last object is placed there, text will changed and a different sound effect will be heard.

              Once that is done, i press my back button to go to the game selection scene but the last object stays.

               

              i tried reset button using

               

              function reset()
              {
              dd1._x = 910
              dd1._y = 277
              dd2._x = 913
              dd2._y = 384
              dd3._x = 914
              dd3._y = 486
              dd4._x = 918
              dd4._y = 591
              }

               

              and used in on the codings for my Back Button before it went the game menu.. hmm here

               

              backbtn.onRelease = function(){

                  reset();
                  gotoAndPlay("5.0Games", 1);

               

              but it still wont work and the last object stays.

               

              Hmm i have 4 games with the same codes, the names of object were the same at the beginning using pp1 til pp5 or pp10 but i changed everyone of them to pp dd qq gg so having directory problem isn't the case. I am still finding the solution now trying whatever i can think of, i need help on this..

              • 4. Re: object staying after changing scene - need help
                srirama.83 Level 1

                As I told you earlier the problem is with the swapDepths of the movieclip.

                 

                The Last movieclip has been assigned a new level so it stays there.You can remove it by Lastmc.unloadMovie().,