2 Replies Latest reply on Jun 11, 2009 3:34 AM by keithwoods

    'If' and 'Else If' conditionals not running properly?

    keithwoods

      I've got a function which looks at movieclips (ball#_mc) which have been placed in a target zone (_ballPlaced[#] boolean). It then decides where to go on each movieclip timeline depending on which other balls have been placed in a target zone.

       

      Only the first condition is ever met, i.e. when all the balls are in a zone, they jump to their correct places in their timelines....however, it doesn't work for any of the other 'else if' conditions?

       

      Here's my code...

       

      // function to determine which video to play for each ball
      private function playVideo(event:Event):void
                  {
                     
                             
                      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("playVideo function is running");
                  }
        • 1. Re: 'If' and 'Else If' conditionals not running properly?
          keithwoods Level 1

          Ok, something strange is happening.

           

          If I place one of the balls in a target zone.....then pick up and drop all the other balls, but not place them in a target zone the conditionals start working?

           

          Here's the full code....

           

          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
              {
                  // 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);
                  // Wall movieclip variables instantiated, so they can be placed on stage and used in bubble() function of Ball() Class
                  public var wall1:Wall=new Wall();
                  public var wall2:Wall=new Wall();            
                  // 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();
                  
                  // Constructor method for EgoGame Document Class
                  public function EgoGame()
                  {
                      // add wall movieclips to the left and right edges of the stage
                      stage.addChild(wall1);
                      wall1.x=32; 
                      wall1.y=510;
                      wall1.name = "wall1";
                      stage.addChild(wall2);
                      wall2.x=1170; 
                      wall2.y=510;
                      wall1.name = "wall2";
                      
                      // 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";
                      
                      stage.addChild(ball1_mc);
                      ball1_mc.x=Math.random() * 1000 + 100;
                      ball1_mc.y=Math.random()* 50 + 480; 
                      ball1_mc.name = "ball1_mc";
                  
                      stage.addChild(ball2_mc);
                      ball2_mc.x=Math.random()* 1000 + 100;
                      ball2_mc.y=Math.random()* 50 + 480;
                      ball2_mc.name = "ball2_mc";
                      
                      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);
                      ball1_mc.addEventListener(MouseEvent.MOUSE_UP, zoneFill);
                      ball2_mc.addEventListener(MouseEvent.MOUSE_UP, zoneFill);
                      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);
                      ball1_mc.addEventListener(Event.ENTER_FRAME, playVideo);
                      ball2_mc.addEventListener(Event.ENTER_FRAME, playVideo);
                      ball3_mc.addEventListener(Event.ENTER_FRAME, playVideo);
          
                  }// 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);
                          }
                          else if(event.target.hitTestObject(zone1_mc) && zoneFull[1] == false)
                          {
                              event.target.x = zone1_mc.x;
                              event.target.y = zone1_mc.y;
                              zoneFull[1] = true;
                              event.target._ballPlaced[event.target.name.substring(4,5)] = true;
                              event.target.lockBall(event);
                              event.target.removeEventListener(MouseEvent.MOUSE_UP, zoneFill);
                          }
                          else if(event.target.hitTestObject(zone2_mc) && zoneFull[2] == false)
                          {
                              event.target.x = zone2_mc.x;
                              event.target.y = zone2_mc.y;
                              zoneFull[2] = true;
                              event.target._ballPlaced[event.target.name.substring(4,5)] = true;
                              event.target.lockBall(event);
                              event.target.removeEventListener(MouseEvent.MOUSE_UP, zoneFill);
              
                          }
                          else if(event.target.hitTestObject(zone3_mc) && zoneFull[3] == false)
                          {
                              event.target.x = zone3_mc.x;
                              event.target.y = zone3_mc.y;
                              zoneFull[3] = true;
                              event.target._ballPlaced[event.target.name.substring(4,5)] = true;
                              event.target.lockBall(event);
                              event.target.removeEventListener(MouseEvent.MOUSE_UP, zoneFill);
              
                          }
                          else if(event.target.hitTestObject(zone4_mc) && zoneFull[4] == false)
                          {
                              event.target.x = zone4_mc.x;
                              event.target.y = zone4_mc.y;
                              zoneFull[4] = true;
                              event.target._ballPlaced[event.target.name.substring(4,5)] = true;
                              event.target.lockBall(event);
                              event.target.removeEventListener(MouseEvent.MOUSE_UP, zoneFill);
              
                          }
                          else if(event.target.hitTestObject(zone5_mc) && zoneFull[5] == false)
                          {
                              event.target.x = zone5_mc.x;
                              event.target.y = zone5_mc.y;
                              zoneFull[5] = true;
                              event.target._ballPlaced[event.target.name.substring(4,5)] = true;
                              event.target.lockBall(event);
                              event.target.removeEventListener(MouseEvent.MOUSE_UP, zoneFill);
              
                          }
                          else if(event.target.hitTestObject(zone6_mc) && zoneFull[6] == false)
                          {
                              event.target.x = zone6_mc.x;
                              event.target.y = zone6_mc.y;
                              zoneFull[6] = true;
                              event.target._ballPlaced[event.target.name.substring(4,5)] = true;
                              event.target.lockBall(event);
                              event.target.removeEventListener(MouseEvent.MOUSE_UP, zoneFill);
              
                          }
                          else if(event.target.hitTestObject(zone7_mc) && zoneFull[7] == false)
                          {
                              event.target.x = zone7_mc.x;
                              event.target.y = zone7_mc.y;
                              zoneFull[7] = true;
                              event.target._ballPlaced[event.target.name.substring(4,5)] = true;
                              event.target.lockBall(event);
                              event.target.removeEventListener(MouseEvent.MOUSE_UP, zoneFill);
              
                          }
                          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("playVideo function is running");
                      }// end of playVideo 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 fl.transitions.Tween;
              import fl.transitions.easing.*;
              import fl.transitions.TweenEvent;
              
              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);
                      
                      //var myTween:Tween = new Tween(this, "x", Regular.easeInOut, this.x, 1135, 10, true);
                  }// 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.");
                      }// end of dragIt function
                      
                      private function dropIt(event:MouseEvent):void
                      {
                          this.stopDrag();
                          trace(this.name + " dropped.");
                      }// end of dropIt function
                      
                      public function lockBall(event:MouseEvent):void
                      {
                          if(_ballPlaced[this.name.substring(4,5)] == true)
                          {
                              this.removeEventListener(MouseEvent.MOUSE_DOWN, dragIt);
                              this.removeEventListener(MouseEvent.MOUSE_UP, dropIt);
                              this.buttonMode = false;
                          }
                          trace(this.name + " '_ballPlaced' value is " + _ballPlaced[this.name.substring(4,5)])
                      }// end of lockBall function
              }//end of Ball Class
          }// end of package
          
          

          Cheers!

          • 2. Re: 'If' and 'Else If' conditionals not running properly?
            keithwoods Level 1

            Worked it out!

             

            I ran a trace and found out that I wasn't defining the _ballPlaced[#] boolean at the start. It came up with the message '_ballPlaced[#] undefined' for each ball.

             

            So all I've done now is set the _ballPlaced[#] value for each ball to 'false' at the start of the code in the Document Class.