1 Reply Latest reply on Jun 3, 2009 5:20 AM by Ned Murphy

    Function not reading 'if' and 'else' if coniditions correctly

    keithwoods Level 1

      I've got a function which is constantly running to see if a set of movie clips (ball0_mc, ball1_mc etc.) have been placed in a target zone.  Once the ballPlaced[#] value is true for a partiuclar moive clip, the movie clip starts acting in different ways depending on which other balls have been placed in a target zone (i.e. their ballPlaced[#] value is also true).

       

      The 'if' and 'else if' conditional statements are contained within the playVideo() function which is constantly running through the ENTER_FRAME event.

       

      Can anyone tell me why these 'if' and 'else if' statments aren't being read properly?

       

      Here's my code....

       

      Document Class (EgoGame.as)

       

      package
      {
          import flash.display.MovieClip;
          import flash.display.DisplayObject;
          import flash.events.MouseEvent;
          import flash.events.Event
          import Ball;
              
          public class EgoGame extends MovieClip
          {
              public var _zoneFull:Array = new Array(false, false, false);
              
              public function EgoGame()
              {
                  ball0_mc.addEventListener(MouseEvent.MOUSE_UP, zoneFill);
                  ball2_mc.addEventListener(MouseEvent.MOUSE_UP, zoneFill);
                  ball1_mc.addEventListener(MouseEvent.MOUSE_UP, zoneFill);
                  
                  ball0_mc.addEventListener(Event.ENTER_FRAME, playVideo);
                  ball1_mc.addEventListener(Event.ENTER_FRAME, playVideo);
                  ball2_mc.addEventListener(Event.ENTER_FRAME, playVideo);        
                  
              }
              
              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
                  {
                      event.target.x = event.target._startX;
                      event.target.y = event.target._startY;
                      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)]);
                  
              }
      
              // function to determine which video to play for each ball
              public function playVideo(event:Event):void
              {
                  
                  // ball0_mc = Blu   ball1_mc = Red   ball2_mc = Yel
                  if (ball0_mc._ballPlaced[0] == true)
                  {
                      ball0_mc.gotoAndPlay("Start");
                  }
                  else if (ball1_mc._ballPlaced[1] == true)
                  {
                      ball1_mc.gotoAndPlay("Start");
                  }
                  else if (ball2_mc._ballPlaced[2] == true)
                  {
                      ball2_mc.gotoAndPlay("Start");
                  }
                  else if (ball0_mc._ballPlaced[0] == true && ball1_mc._ballPlaced[1] == true)
                  {
                      ball0_mc.gotoAndPlay("Red");
                      ball1_mc.gotoAndPlay("Blu");
                  }
                  else if (ball0_mc._ballPlaced[0] == true && ball2_mc._ballPlaced[2] == true)
                  {
                      ball0_mc.gotoAndPlay("Yel");
                      ball2_mc.gotoAndPlay("Blu");
                  }
                  else if (ball0_mc._ballPlaced[0] == true && ball1_mc._ballPlaced[1] == true && ball2_mc._ballPlaced[2] == true)
                  {
                      ball0_mc.gotoAndPlay("RedYel");
                      ball1_mc.gotoAndPlay("BluYel")
                      ball2_mc.gotoAndPlay("RedBlu");
                  }
                  else 
                  { 
                      trace("No balls on stage"); 
                  }
              }
          
          }
      }
      
      

       

      Ball Class (Ball.as)

       

      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 _hitTarget:MovieClip;
              public var _startX:Number;
              public var _startY:Number;
              public var _ballPlaced:Array = new Array;
              public var selectedMovieClip;
                      
              public function Ball()
              {
                  _startX = this.x;
                  _startY = this.y;
                  _ballPlaced[this.name.substring(4,5)] = false;            
                  
                  this.buttonMode = true;
                  this.addEventListener(MouseEvent.MOUSE_DOWN, dragIt);
                  this.addEventListener(MouseEvent.MOUSE_UP, dropIt);
                  this.addEventListener(Event.ENTER_FRAME, bubble);
              }
              
              public function bubble(event:Event):void
              {
                  //trace("balls will start moving");
                  //this._x = 8;
              }
              
              private function dragIt(event:MouseEvent):void
              {
                              
                  this.startDrag();
                  this.parent.setChildIndex(this, this.parent.numChildren-1);
                  trace(this.name + " picked up.");
              }
              
              private function dropIt(event:MouseEvent):void
              {
                  this.stopDrag();
                  trace(this.name + " dropped.");
              }
              
              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;
                  }
              }
              
          }
      }

       

       


      Cheers!

        • 1. Re: Function not reading 'if' and 'else' if coniditions correctly
          Ned Murphy Adobe Community Professional & MVP

          If you look at the conditionals, they will likely never get past one of the first three.  As soon as any one is true, one of the first three conditions will be satisfied.  It will never get as far as the combinations the way you have it.  To make the combinations triggerable, you should place them first, with the one for all three leading the pack.

           

          Note: You can save code if those values are all boolean types...

           

          if (ball0_mc._ballPlaced[0] == true)   can be shortened to

           

          if (ball0_mc._ballPlaced[0])

           

           

          You should trace the values in your playVideo function to see what you are getting, and if what you are getting should trigger one of them and isn't.