3 Replies Latest reply on May 19, 2009 8:05 AM by keithwoods

    Drag, Drop, hitTargetObject, Classes, MouseEvents, MovieClip variables...you name it!

    keithwoods Level 1

      Hey there,

       

      I've got 3 coloured balls and 3 zones which any of the balls can be dropped in.

       

      The idea is that once a ball is placed in a zone, it stays in that zone and no other ball can be added to that zone until the current ball is removed.

       

      I've got to a point so that any ball can be placed in any zone.

       

      EgoGame1.jpg

       

      However, the problem is there seems to be some hierarchy with regards to the balls.  ball0_mc seems to be the chief ball, and will replace any of the other two balls if there placed in a zone, instead of snapping back to it's original position because the zone is full.  ball0_mc also can't be replaced by any of the other balls if its placed in a zone.  ball1_mc is the next one down the line and when in a zone, can be replaced by ball0_mc if its dragged over it, but it cannot be replaced by the third and final ball2_mc.  ball2_mc seems to be the runt of the pack and can be replaced by either of the two other balls when its in a zone and cannot replace any of the other balls when they're in a zone.

       

      Hope you're still with me?!  So, what I'd like to do is make it so each ball is equal, so when any ball is placed in a zone, it can't be replaced with another ball until the current ball has been removed...and they live happily ever after in the land of ballshire.

       

      Here's my code.  I've created two classes, one linked to the movie clip balls called 'Ball', and another called 'EgoGame' which is being used as the document class for the main .fla file.

       

      'Ball' Class

      package
      {
          import flash.display.MovieClip;
          import flash.events.MouseEvent;
          import flash.display.DisplayObject;
          
          public class Ball extends MovieClip
          {
              public var _hitTarget:MovieClip;
              public var _startX:Number;
              public var _startY:Number;
              
              public function Ball()
              {
                  _startX = this.x;
                  _startY = this.y;
                  this.buttonMode = true;
                  this.addEventListener(MouseEvent.MOUSE_DOWN, dragIt);
                  this.addEventListener(MouseEvent.MOUSE_UP, dropIt);
              }
              
              private function dragIt(event:MouseEvent):void
              {
                  this.startDrag();
              }
              
              public function dropIt(event:MouseEvent):void
              {
                  this.stopDrag();
              }
              
          }
      }
      

       

      'EgoGame' document class

      package
      {
          import flash.display.MovieClip;
          import flash.display.DisplayObject;
          import flash.events.MouseEvent;
          import Ball;
              
          public class EgoGame extends MovieClip
          {
              public var _zone0Full:Boolean = false;
              public var _zone1Full:Boolean = false;
              public var _zone2Full:Boolean = 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(MouseEvent.MOUSE_DOWN, zoneEmpty);
                  ball1_mc.addEventListener(MouseEvent.MOUSE_DOWN, zoneEmpty);
                  ball2_mc.addEventListener(MouseEvent.MOUSE_DOWN, zoneEmpty);
              }
              
              private function zoneFill(event:MouseEvent):void
              {
                  if(event.target.hitTestObject(zone0_mc) && _zone0Full == false)
                  {
                      event.target.x = zone0_mc.x;
                      event.target.y = zone0_mc.y;
                      _zone0Full = true;
                  }
                  else if(event.target.hitTestObject(zone1_mc) && _zone1Full == false)
                  {
                      event.target.x = zone1_mc.x;
                      event.target.y = zone1_mc.y;
                      _zone1Full = true;
                  }
                  else if(event.target.hitTestObject(zone2_mc) && _zone2Full == false)
                  {
                      event.target.x = zone2_mc.x;
                      event.target.y = zone2_mc.y;
                      _zone2Full = true;
                  }
                  else
                  {
                      event.target.x = event.target._startX;
                      event.target.y = event.target._startY;
                  }            
              }
              
              private function zoneEmpty(event:MouseEvent):void
              {
                  if(event.target.hitTestObject(zone0_mc) && _zone0Full == true)
                  {
                      _zone0Full = false;
                  }
                  else if(event.target.hitTestObject(zone1_mc) && _zone1Full == true)
                  {
                      _zone1Full = false;
                  }
                  else if(event.target.hitTestObject(zone2_mc) && _zone2Full == true)
                  {
                      _zone2Full = false;
                  }
                  else
                  {
                      event.target.x = event.target.x;
                      event.target.y = event.target.y;
                  }            
              }
              
          }
      }
      

       

      Cheers...

       

      Oh yeh, I'm using Flash CS3 with ActionScript 3.0

        • 1. Re: Drag, Drop, hitTargetObject, Classes, MouseEvents, MovieClip variables...you name it!
          RossRitchey Level 4

          It sounds to me like the _zone0full, _zone1full, and _zone2full vars aren't getting set properly.

           

          Time to throw some traces in there so you know what is going on at any point.

           

           

          package
          {
              import flash.display.MovieClip;
              import flash.display.DisplayObject;
              import flash.events.MouseEvent;
              import Ball;
                  
              public class EgoGame extends MovieClip
              {
                  public var _zone0Full:Boolean = false;
                  public var _zone1Full:Boolean = false;
                  public var _zone2Full:Boolean = 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(MouseEvent.MOUSE_DOWN, zoneEmpty);
                      ball1_mc.addEventListener(MouseEvent.MOUSE_DOWN, zoneEmpty);
                      ball2_mc.addEventListener(MouseEvent.MOUSE_DOWN, zoneEmpty);
                  }
                  
                  private function zoneFill(event:MouseEvent):void
                  {
                      trace("zoneFill | "+event.target);
                      trace("zone 0 full | "+_zone0Full);
                      trace("zone 1 full | "+_zone1Full);
                      trace("zone 2 full | "+_zone2Full);
          
                         if(event.target.hitTestObject(zone0_mc) && _zone0Full == false)
                      {
                          trace("Dropping in Zone 0");
                          
                          event.target.x = zone0_mc.x;
                          event.target.y = zone0_mc.y;
                          _zone0Full = true;
                      }
                      else if(event.target.hitTestObject(zone1_mc) && _zone1Full == false)
                      {
                          trace("Dropping in Zone 1");
                          
                          event.target.x = zone1_mc.x;
                          event.target.y = zone1_mc.y;
                          _zone1Full = true;
                      }
                      else if(event.target.hitTestObject(zone2_mc) && _zone2Full == false)
                      {
                          trace("Dropping in Zone 2");
                          
                          event.target.x = zone2_mc.x;
                          event.target.y = zone2_mc.y;
                          _zone2Full = true;
                      }
                      else
                      {
                          trace("Reverting to start position");
                          
                          event.target.x = event.target._startX;
                          event.target.y = event.target._startY;
                      }            
                  }
                  
                  private function zoneEmpty(event:MouseEvent):void
                  {
                      trace("zoneEmpty | "+event.target);
                      trace("zone 0 full | "+_zone0Full);
                      trace("zone 1 full | "+_zone1Full);
                      trace("zone 2 full | "+_zone2Full);
                      
                      if(event.target.hitTestObject(zone0_mc) && _zone0Full == true)
                      {
                          trace("Emptying Zone 0");
                          
                          _zone0Full = false;
                      }
                      else if(event.target.hitTestObject(zone1_mc) && _zone1Full == true)
                      {
                          trace("Emptying Zone 1");
                          
                          _zone1Full = false;
                      }
                      else if(event.target.hitTestObject(zone2_mc) && _zone2Full == true)
                      {
                          trace("Emptying Zone 2");
                          
                          _zone2Full = false;
                      }
                      else
                      {
                          trace("No zones to empty");
                          
                          event.target.x = event.target.x;
                          event.target.y = event.target.y;
                      }            
                  }
                  
              }
          }
          

          1 person found this helpful
          • 2. Re: Drag, Drop, hitTargetObject, Classes, MouseEvents, MovieClip variables...you name it!
            keithwoods Level 1

            Thanks for your reply Ross, I'm new to actionscript but I'm fast learning the trace call can always help you out!

             

            Ok, so I've done some traces and the zone#Full variable is getting stored correctly.  Which doesn't really help!  Why is it still possible to place a ball in the zone when it's zoneFull variable is true?

             

            One thing I have noticed is that if I drag a new ball over a another ball placed in a zone, the balls will only swap if the newly dragged ball's x and y coordinates (i.e. the centre of the ball) touch any part of the ball inside the zone.  So is this something to do with the hitTestObject parameter?  I tested this by reducing the alpha on all balls so I could see the overlap, and also placed a cross in the x and y coordinates, and sure enough as soon as the '+' of the newly dragged ball touched any part of the ball in the zone then it would replace that ball.

             

            EgoGame2.jpg

             

            Although this isn't always the case as there's still a hierarchy, so when ball2_mc is placed completely over either ball0_mc or ball1_mc when they're in a zone, it won't replace them.  But if ball0_mc or ball1_mc x and y coordinates touch any part of ball2_mc, then ball2_mc gets shoved out of its current zone and back to where it started?

             

            So I'm still looking for equal opportunities amongst all my balls!!!

             

            Any help would be greatly appreciated.

            • 3. Re: Drag, Drop, hitTargetObject, Classes, MouseEvents, MovieClip variables...you name it!
              keithwoods Level 1

              I've tried replacing all the hitTestObject properties with a dropTarget property to see if that would make a difference, but no luck...it still did the same thing? ball0_mc can still override the ball1_mc and ball2_mc?