0 Replies Latest reply on Jun 12, 2009 5:34 AM by keithwoods

    Resetting Movieclip problem

    keithwoods Level 1

      I've got a game where the user can place several balls (ball#_mc) in several different targets (zone#_mc).  I'd like the user to be able to remove a ball if they wish.  What I've setup so far is to display a little cross which can be clicked on (ballResetBtn), this then removes the ball from its current zone back down to the bottom of the stage.

       

      I'm at a point where any ball can be placed in any zone.  The reset button can be clicked and the ball is return to a random position at the bottom of the stage.  However, once this is done, the ball then loses all its powers to be placed in a zone again.  It can be dragged and dropped like before.  However, it doesn't snap into a zone if dropped over one, or snap back to the bottom of the stage if it's not dropped near a zone?

       

      Any pointers greatly appreciated!

       

      Here's an abridged version of my code as I've left out anything repetive:

      Document Class
      
      package
      {
          import flash.display.MovieClip;
          import flash.display.DisplayObject;
          import flash.events.MouseEvent;
          import flash.events.Event
          import Ball;
          
          /*                Document Class for Ego Game                */
          public class EgoGame extends MovieClip
          {
              public var zone0_mc:Zone = new Zone();
              //...code repeated consequtively up to...
              public var zone8_mc:Zone = new Zone();
              
              // assign false zoneFull value to the 9 target zones so they can potentially be filled with a ball
              public var zoneFull:Array = new Array(false, false, false, false, false, false, false, false, false);
              
              // Individual balls instantiated so they can be placed on stage
              public var ball0_mc:Ball_Blu=new Ball_Blu();
              public var ball1_mc:Ball_Yel=new Ball_Yel();
              public var ball2_mc:Ball_Red=new Ball_Red();
              public var ball3_mc:Ball_Gre=new Ball_Gre();
              
              // Instantiate Ball Reset Button
              public var ballResetBtn:BallReset = new BallReset();
                  
              // Constructor method for EgoGame Document Class
              public function EgoGame()
              {
                  ball0_mc._ballPlaced[0] = false;
                  //...code repeated consequtively up to...
                  ball3_mc._ballPlaced[3] = false;
                  
                  stage.addChild(zone0_mc);
                  zone0_mc.x=267.6;
                  zone0_mc.y=120.5;
                  zone0_mc.name = "zone0_mc";
                  //...code repeated consequtively up to...
                  stage.addChild(zone8_mc);
                  zone8_mc.x=925;
                  zone8_mc.y=364.6;
                  zone8_mc.name = "zone8_mc";
                  
                  // ball movieclips to stage
                  stage.addChild(ball0_mc);
                   // places the ball randomly in the x pos so it doesn't touch target areas or go off screen
                  ball0_mc.x=Math.random() * 1000 + 100; 
                   // places the ball randomly in the y pos so it doesn't touch target areas or go off screen
                  ball0_mc.y=Math.random()* 50 + 480;
                  ball0_mc.name = "ball0_mc";
                  //...code repeated consequtively up to...
                  stage.addChild(ball3_mc);
                  ball3_mc.x=Math.random() * 1000 + 100;
                  ball3_mc.y=Math.random()* 50 + 480;
                  ball3_mc.name = "ball3_mc";
                  
                  // call zoneFill function when Mouse button is released
                  ball0_mc.addEventListener(MouseEvent.MOUSE_UP, zoneFill);
                  //...code repeated consequtively up to...
                  ball3_mc.addEventListener(MouseEvent.MOUSE_UP, zoneFill);
                  
                  // constantly call on playVideo function to decide which videos to play
                  ball0_mc.addEventListener(Event.ENTER_FRAME, playVideo);
                  //...code repeated consequtively up to...
                  ball3_mc.addEventListener(Event.ENTER_FRAME, playVideo);
                  
                  ball0_mc.addEventListener(MouseEvent.ROLL_OVER, ballResetVisible);
                  //...code repeated consequtively up to...
                  ball3_mc.addEventListener(MouseEvent.ROLL_OVER, ballResetVisible);
      
                  ball0_mc.addEventListener(MouseEvent.ROLL_OUT, ballResetInvisible);
                  //...code repeated consequtively up to...
                  ball3_mc.addEventListener(MouseEvent.ROLL_OUT, ballResetInvisible);    
      
              }// end of Ball Class constructor    
      
                  private function zoneFill(event:MouseEvent):void
                  {
                      if(event.target.hitTestObject(zone0_mc) && zoneFull[0] == false)
                      {
                          event.target.x = zone0_mc.x;
                          event.target.y = zone0_mc.y;
                          zoneFull[0] = true;
                          event.target._ballPlaced[event.target.name.substring(4,5)] = true;
                          event.target.lockBall(event);
                          event.target.removeEventListener(MouseEvent.MOUSE_UP, zoneFill);
                      }
                      //...code repeated consequtively up to...
                      else if(event.target.hitTestObject(zone8_mc) && zoneFull[8] == false)
                      {
                          event.target.x = zone8_mc.x;
                          event.target.y = zone8_mc.y;
                          zoneFull[8] = true;
                          event.target._ballPlaced[event.target.name.substring(4,5)] = true;
                          event.target.lockBall(event);
                          event.target.removeEventListener(MouseEvent.MOUSE_UP, zoneFill);
                      }
                      else
                      {
                          event.target.x = Math.random() * 1000 + 100;
                          event.target.y = Math.random()* 50 + 480; 
                          event.target._ballPlaced[event.target.name.substring(4,5)] =false;
                      }
                      trace(event.target.name + " _ballPlaced value is " + event.target._ballPlaced[event.target.name.substring(4,5)]);
                  }// end of zoneFill function
                  
                  // function to determine which video to play for each ball
                  private function playVideo(event:Event):void
                  {
                      // ball0_mc = Blu     ball1_mc = Yel     ball2_mc = Red     ball3_mc = Gre
      
                      if (ball0_mc._ballPlaced[0] && ball1_mc._ballPlaced[1] && ball2_mc._ballPlaced[2] && ball3_mc._ballPlaced[3])
                      {
                          ball0_mc.gotoAndPlay("BluYelRedGre");
                          ball1_mc.gotoAndPlay("BluYelRedGre");
                          ball2_mc.gotoAndPlay("BluYelRedGre");
                          ball3_mc.gotoAndPlay("BluYelRedGre");
                      }
                      else if (ball0_mc._ballPlaced[0] == false && ball1_mc._ballPlaced[1] && ball2_mc._ballPlaced[2] && ball3_mc._ballPlaced[3])
                      {
                          ball1_mc.gotoAndPlay("YelRedGre");
                          ball2_mc.gotoAndPlay("YelRedGre");
                          ball3_mc.gotoAndPlay("YelRedGre");
                      }
                      else if (ball0_mc._ballPlaced[0] && ball1_mc._ballPlaced[1] == false && ball2_mc._ballPlaced[2] && ball3_mc._ballPlaced[3])
                      {
                          ball0_mc.gotoAndPlay("BluRedGre");
                          ball2_mc.gotoAndPlay("BluRedGre");
                          ball3_mc.gotoAndPlay("BluRedGre");
                      }
                      else if (ball0_mc._ballPlaced[0] && ball1_mc._ballPlaced[1] && ball2_mc._ballPlaced[2] == false && ball3_mc._ballPlaced[3])
                      {
                          ball0_mc.gotoAndPlay("BluYelGre");
                          ball1_mc.gotoAndPlay("BluYelGre");
                          ball3_mc.gotoAndPlay("BluYelGre");
                      }
                      else if (ball0_mc._ballPlaced[0] && ball1_mc._ballPlaced[1] && ball2_mc._ballPlaced[2] && ball3_mc._ballPlaced[3]  == false)
                      {
                          ball0_mc.gotoAndPlay("BluYelRed");
                          ball1_mc.gotoAndPlay("BluYelRed");
                          ball2_mc.gotoAndPlay("BluYelRed");
                      }
                      else if (ball0_mc._ballPlaced[0] == false && ball1_mc._ballPlaced[1] == false && ball2_mc._ballPlaced[2] && ball3_mc._ballPlaced[3])
                      {
                          ball2_mc.gotoAndPlay("RedGre");
                          ball3_mc.gotoAndPlay("RedGre");
                      }
                      else if (ball0_mc._ballPlaced[0] == false && ball1_mc._ballPlaced[1] && ball2_mc._ballPlaced[2] == false && ball3_mc._ballPlaced[3])
                      {
                          ball1_mc.gotoAndPlay("YelGre");
                          ball3_mc.gotoAndPlay("YelGre");
                      }
                      else if (ball0_mc._ballPlaced[0] == false && ball1_mc._ballPlaced[1] && ball2_mc._ballPlaced[2] && ball3_mc._ballPlaced[3] == false)
                      {
                          ball1_mc.gotoAndPlay("YelRed");
                          ball2_mc.gotoAndPlay("YelRed");
                      }
                      else if (ball0_mc._ballPlaced[0] && ball1_mc._ballPlaced[1] == false && ball2_mc._ballPlaced[2] == false && ball3_mc._ballPlaced[3])
                      {
                          ball0_mc.gotoAndPlay("BluGre");
                          ball3_mc.gotoAndPlay("BluGre");
                      }
                      else if (ball0_mc._ballPlaced[0] && ball1_mc._ballPlaced[1] == false && ball2_mc._ballPlaced[2] && ball3_mc._ballPlaced[3]  == false)
                      {
                          ball0_mc.gotoAndPlay("BluRed");
                          ball2_mc.gotoAndPlay("BluRed");
                      }
                      else if (ball0_mc._ballPlaced[0] && ball1_mc._ballPlaced[1] && ball2_mc._ballPlaced[2] == false && ball3_mc._ballPlaced[3]  == false)
                      {
                          ball0_mc.gotoAndPlay("BluYel");
                          ball1_mc.gotoAndPlay("BluYel");
                      }
                      else if (ball0_mc._ballPlaced[0] == false && ball1_mc._ballPlaced[1] == false && ball2_mc._ballPlaced[2] == false && ball3_mc._ballPlaced[3])
                      {
                          ball3_mc.gotoAndPlay("Gre");
                      }
                      else if (ball0_mc._ballPlaced[0] == false && ball1_mc._ballPlaced[1] == false && ball2_mc._ballPlaced[2] && ball3_mc._ballPlaced[3]  == false)
                      {
                          ball2_mc.gotoAndPlay("Red");
                      }
                      else if (ball0_mc._ballPlaced[0] == false && ball1_mc._ballPlaced[1] && ball2_mc._ballPlaced[2] == false && ball3_mc._ballPlaced[3]  == false)
                      {
                          ball1_mc.gotoAndPlay("Yel");
                      }
                      else if (ball0_mc._ballPlaced[0] && ball1_mc._ballPlaced[1] == false && ball2_mc._ballPlaced[2] == false && ball3_mc._ballPlaced[3]  == false)
                      {
                          ball0_mc.gotoAndPlay("Blu");
                      }
                      //trace("ZoneFull:   0: " + zoneFull[0] + ", 1: " + zoneFull[1] + ", 2: " + zoneFull[2] + ", 3: " + zoneFull[3] + ", 4: " + zoneFull[4] + ", 5: " + zoneFull[5] + ", 6: " + zoneFull[6] + ", 7: " + zoneFull[7] + ", 8: " + zoneFull[8]);
                  }// end of playVideo function        
                  
                  private function ballResetVisible(event:MouseEvent):void
                  {
                      var currentBall = event.target;
                      
                      if (currentBall._ballPlaced[currentBall.name.substring(4,5)] == true)
                      {
                          stage.addChild(ballResetBtn);
                          ballResetBtn.alpha = 1;
                          ballResetBtn.x = event.target.x + 161.2;
                          ballResetBtn.y = event.target.y - 58;
                          trace(currentBall.name + " is being hovered over");
                          ballResetBtn.addEventListener(MouseEvent.ROLL_OVER, ballResetActive)
                          ballResetBtn.addEventListener(MouseEvent.MOUSE_DOWN, ballResetPressed)
                      }
                      
                      function ballResetActive(event:MouseEvent):void
                      {
                          ballResetBtn.buttonMode = true;
                          ballResetBtn.alpha = 1;
                      }//end of ballResetActive function
                  
                      function ballResetPressed(event:MouseEvent):void
                      {
                          if(currentBall.hitTestObject(zone0_mc))
                          {
                              zoneFull[0] = false;
                          }
                          //...code repeated consequtively up to...
                          }
                          else if(currentBall.hitTestObject(zone8_mc))
                          {
                              zoneFull[8] = false;
                          }
                          
                          currentBall.x = Math.random() * 1000 + 100;
                          currentBall.y = Math.random()* 50 + 480;
                          currentBall._ballPlaced[currentBall.name.substring(4,5)] = false;
                          currentBall.gotoAndPlay(1);
                          ballResetBtn.alpha = 0;
                          currentBall.unlockBall(event);
                          trace(currentBall.name + "'s reset button has been pressed and its ballPlaced value is " + currentBall._ballPlaced[currentBall.name.substring(4,5)]);
                      }//end of ballResetPressed function
                  }//end of ballResetVisible function
                  
                  private function ballResetInvisible(event:MouseEvent):void
                  {
                      ballResetBtn.alpha = 0;
                  }//end of ballResetInvisible function
                  
                  
          }// end of Ball Class
      }// end of package
      

       

      Ball Class

       

       

      package
      {
          import flash.display.MovieClip;
          import flash.events.MouseEvent;
          import flash.display.DisplayObject;
          import flash.events.Event
          import EgoGame;
          
          public class Ball extends MovieClip
          {
              
              public var _ballPlaced:Array = new Array;
      
              public function Ball()
              {
                  //_ballPlaced[this.name.substring(4,5)] = false;            
                  this.buttonMode = true;
                  this.addEventListener(MouseEvent.MOUSE_DOWN, dragIt);
                  this.addEventListener(MouseEvent.MOUSE_UP, dropIt);
              }// end of Ball constructor function
              
                  private function dragIt(event:MouseEvent):void
                  {
                      this.startDrag();
                      this.parent.setChildIndex(this, this.parent.numChildren-1);
                      trace(this.name + " picked up.");
                      trace(this.name + " ballPlaced value is " + this._ballPlaced[this.name.substring(4,5)]);
                  }// end of dragIt function
                  
                  private function dropIt(event:MouseEvent):void
                  {
                      this.stopDrag();
                      trace(this.name + " dropped.");
                  }// end of dropIt function
                  
                  public function lockBall(event:Event):void
                  {
                      if(_ballPlaced[this.name.substring(4,5)] == true)
                      {
                          this.removeEventListener(MouseEvent.MOUSE_DOWN, dragIt);
                          this.removeEventListener(MouseEvent.MOUSE_UP, dropIt);
                          this.buttonMode = false;
                      }
                  }// end of lockBall function
                  
                  public function unlockBall(event:Event):void
                  {
                      if(_ballPlaced[this.name.substring(4,5)] == false)
                      {
                          this.addEventListener(MouseEvent.MOUSE_DOWN, dragIt);
                          this.addEventListener(MouseEvent.MOUSE_UP, dropIt);
                          this.buttonMode = true;
                      }
                  }// end of lockBall function
          }//end of Ball Class
      }// end of package
      
      

       

       

      Cheers!