3 Replies Latest reply on May 25, 2009 9:50 AM by kglad

    Accessing a public variable between classes

    keithwoods

      Hi there,

       

      I've got two classes running...one is a document class (EgoGame.as) and another is a class linked to several similar movie clips (Ball.as).

       

      I'm trying to access a public variable from Ball.as which has been declared in the doucment class EgoGame.as.

       

      When I run the test the outputs states the following...

       

      1120: Access of undefined property _ballPlaced.

       


      Here's my code.  What I'm trying to do is remove the event listeners from the Ball.as when the _ballPlaced variable is true, so that the user can't drag and drop the balls after they've been placed in a zone....any pointers greatly appreciated!

       

      Document Class
      EgoGame.as

       

      package
      {
          import flash.display.MovieClip;
          import flash.display.DisplayObject;
          import flash.events.MouseEvent;
          import Ball;
              
          public class EgoGame extends MovieClip
          {
              public var __zoneFull:Array = new Array(false, false, false);
              
              public var __ballPlaced:Array = new Array(false, false, false);
              
              public function EgoGame()
              {
                  ball0_mc.addEventListener(MouseEvent.MOUSE_DOWN, zoneEmpty);
                  ball1_mc.addEventListener(MouseEvent.MOUSE_DOWN, zoneEmpty);
                  ball2_mc.addEventListener(MouseEvent.MOUSE_DOWN, zoneEmpty);
                  
                  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_UP, playMovie);
                  ball1_mc.addEventListener(MouseEvent.MOUSE_UP, playMovie);
                  ball2_mc.addEventListener(MouseEvent.MOUSE_UP, playMovie);
                  
                  }
              
              private function zoneEmpty(event:MouseEvent):void
              {
                  if(event.target.hitTestObject(zone0_mc) && _zoneFull[0] == true)
                  {
                      _zoneFull[0] = false;
                      _ballPlaced[event.target.name.substring(4,5)] = false;
                  }
                  else if(event.target.hitTestObject(zone1_mc) && _zoneFull[1] == true)
                  {
                      _zoneFull[1] = false;
                      _ballPlaced[event.target.name.substring(4,5)] = false;
                  }
                  else if(event.target.hitTestObject(zone2_mc) && _zoneFull[2] == true)
                  {
                      _zoneFull[2] = false;
                      _ballPlaced[event.target.name.substring(4,5)] = false;
                  }
                  else
                  {
                      event.target.x = event.target._startX;
                      event.target.y = event.target._startY;
                      _ballPlaced[event.target.name.substring(4,5)] = false;
                  }   
              }
              
              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;
                      _ballPlaced[event.target.name.substring(4,5)] = true;
                  }
                  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;
                      _ballPlaced[event.target.name.substring(4,5)] = true;
                  }
                  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;
                      _ballPlaced[event.target.name.substring(4,5)] = true;
                  }
                  else
                  {
                      event.target.x = event.target._startX;
                      event.target.y = event.target._startY;
                      _ballPlaced[event.target.name.substring(4,5)] =false;
                  }
              }
              
              private function playMovie(event:MouseEvent):void
              {
                  if (_ballPlaced[0] == true)
                  {
                      ball0_mc.gotoAndPlay(2);
                  }
                  else
                  {
                      ball0_mc.gotoAndStop(1);
                  }
                  
                  if (_ballPlaced[1] == true)
                  {
                      ball1_mc.gotoAndPlay(2);
                  }
                  else
                  {
                      ball1_mc.gotoAndStop(1);
                  }
                  
                  if (_ballPlaced[2] == true)
                  {
                      ball2_mc.gotoAndPlay(2);
                  }
                  else
                  {
                      ball2_mc.gotoAndStop(1);
                  }
              }
              
              
          }
      }

       


      Ball.as

       

      package
      {
          import flash.display.MovieClip;
          import flash.events.MouseEvent;
          import flash.display.DisplayObject;
          import EgoGame;
          
          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();
              }
              
              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);
                  }
              }
              
          }
      }