0 Replies Latest reply on Dec 10, 2010 11:04 AM by ekora

    Help with zoom function

    ekora

      I have some mouse commands to scroll a screen horizontally... need some help adding in a zoom feature where moving the mouse up will zoom in, moving out will zoom out.  Here is the code I have so far:

       

      import mx.transitions.easing.*;
      import mx.transitions.Tween;
      import flash.filters.DropShadowFilter;
      import mx.events.EventDispatcher;

       

      //constants
      var CENTER_STAGE:Number = Stage.width/2;
      var SCROLL_HIT_AREA_POSITION:Number = Stage.width/2;//distance from edges of screen that should trigger scrolling
      var RIGHT_SCROLL_HIT_AREA:Number = SCROLL_HIT_AREA_POSITION;
      var LEFT_SCROLL_HIT_AREA:Number = Stage.width - SCROLL_HIT_AREA_POSITION;
      var RIGHT_SCROLL_EDGE:Number = 100;//x pos at which front scene should stop scrolling left
      var LEFT_SCROLL_EDGE:Number = -200;//x pos at which front scene should stop scrolling right
      var scrollableArea:Number = RIGHT_SCROLL_EDGE - LEFT_SCROLL_EDGE;//hill1._width - Stage.width;
      //var SCROLL_CENTER_POINT:Number = scrollableArea/2;
      var SCROLL_DECELERATE_POSITION:Number = 75;//how far from the edges of the scene does the scrolling slow down
      var DEFAULT_FRICTION:Number = 1;
      var SPEED:Number = 50;

       

      //variables
      var friction:Number;
      var distFromEdge:Number;
      //var swfContainer:MovieClip;
      //var localRoot:MovieClip = this;
      //var localParent:MovieClip = this._parent;
      // determine the correct path to the photos

       

      var dispatchEvent:Function;

       

      EventDispatcher.initialize(this);

       

      setBitmapCaching(true);

       

      this.onEnterFrame = testForScroll;

       

      // determine whether to scroll or not and by how much, based on mouse position and position of the scene
      function testForScroll():Void{
          var xMouse:Number = this._xmouse;// where is the mouse on the x axis?
          var yMouse:Number = this._ymouse;// where is the mouse on the x axis?
         
          var mouseOnStage:Boolean = xMouse > 0 && yMouse > 0; //before someone scrolls into the swf stage area, the mousex and mouse y values are 0, 0 so we need to make sure they are positive values or the scene will begin scrolling to the right immediately
          var mouseInScrollArea:Boolean = xMouse > LEFT_SCROLL_HIT_AREA || xMouse < RIGHT_SCROLL_HIT_AREA;//is the mouse close enough to edge of screen to trigger scrolling
         
          if(mouseOnStage && mouseInScrollArea){//only execute if mouse is over the scroll trigger area   
              var xScrollPos:Number = pintMC.cowMC._x;//x position of the main movieClip
             
              var xDirection:Number = (xMouse - CENTER_STAGE)/Math.abs(xMouse - CENTER_STAGE);//what direction is the scene moving   
              var nearLeftEdge:Boolean = xScrollPos > (scrollableArea - 100);
              var nearRightEdge:Boolean = xScrollPos < (RIGHT_SCROLL_EDGE + 100);
             
              if(nearLeftEdge && xDirection == -1){
                  distFromEdge = scrollableArea - xScrollPos;
                  friction = (scrollableArea - xScrollPos)/SCROLL_DECELERATE_POSITION;
              }else if(nearRightEdge && xDirection == 1){
                  distFromEdge = xScrollPos - RIGHT_SCROLL_EDGE;
                  friction = distFromEdge/SCROLL_DECELERATE_POSITION;
              }else{
                  friction = DEFAULT_FRICTION;
              }
             
              //determine how far to move the scene
              var acc:Number = (xMouse - CENTER_STAGE) * (1/SPEED) * friction;
              //will next movement make scene scroll past one of it's edges
              var reachedEdge:Boolean = (xScrollPos - acc) < RIGHT_SCROLL_EDGE || (xScrollPos - acc) > LeftScrollEdge;
             
              if(!reachedEdge){// only scroll if not going to pass edge
                  scrollScene(acc);
              }
          }
      }

       

      // move the various scenes at relative speeds
      function scrollScene(acc:Number){
          pintMC.cowMC._x -= acc;
          pintMC.flutterbyMC._x -= acc;
          pintMC.grassMC._x -= acc;
          pintMC.cloudMC._x -= (acc/2);
          pintMC.barnMC._x -= (acc/3);
          pintMC.bgMC._x -= (acc/5);
      }

       

      // in order to optimize performance, we create a bitmap of each moving scene. This eliminates the enormous number of vector redraws Flash must perform if vectors are intersecting.
      function setBitmapCaching(cache:Boolean):Void{
          pintMC.cowMC.cacheAsBitmap = cache;
          pintMC.flutterbyMC.cacheAsBitmap = cache;
          pintMC.grassMC.cacheAsBitmap = cache;
          pintMC.cloudMC.cacheAsBitmap = cache;
          pintMC.barnMC.cacheAsBitmap = cache;
          pintMC.bgMC.cacheAsBitmap = cache;
      }

       

       

      Thanks for any help