Guest

SimpleParallax by Michael Hartmayer

By: a guest on Jun 18th, 2010  |  syntax: ActionScript 3  |  size: 2.82 KB  |  hits: 430  |  expires: Never
download  |  raw  |  embed  |  report abuse
This paste has a previous version, view the difference. Copied
  1. package Machinespark.SimpleParallax
  2. {
  3.         import flash.display.MovieClip;
  4.         import flash.display.Stage;
  5.         import flash.events.TimerEvent;
  6.         import flash.utils.Timer;
  7.         import flash.events.Event;
  8.        
  9.         public class SimpleParallax extends MovieClip{
  10.                
  11.                 public var FLAG_MOVE_X_AXIS:Boolean =   true;
  12.                 public var FLAG_MOVE_Y_AXIS:Boolean =   true;
  13.                
  14.                 private var ParallaxTimer:Timer =               new Timer(20);
  15.                 private var ParallaxObjects:Array =             new Array();
  16.                 private var ParallaxParent:MovieClip =          new MovieClip();
  17.                 private var ParallaxEase:Number =               6;
  18.                 private var ParallaxWidth:Number =              0;
  19.                 private var ParallaxHeight:Number =             0;
  20.                
  21.                 public function SimpleParallax(intParallaxWidth:Number, intParallaxHeight:Number) {
  22.                         ParallaxWidth = intParallaxWidth;
  23.                         ParallaxHeight = intParallaxHeight;
  24.                         ParallaxTimer.addEventListener(TimerEvent.TIMER, UpdateParallax);
  25.                 }
  26.                
  27.                 public function AddPane(mcPane:MovieClip, zDepth:Number):void {
  28.                         var NewPane:MovieClip = new MovieClip();
  29.                                 NewPane.addChild(mcPane);
  30.                                 NewPane.zDepth = zDepth;
  31.                                
  32.                                 ParallaxParent.addChild(NewPane);
  33.                                 ParallaxObjects.push(NewPane);
  34.                                 SortParallax();
  35.                 }
  36.                
  37.                 public function StartParallax():void {
  38.                         ParallaxTimer.start();
  39.                 }
  40.                
  41.                 public function StopParallax():void {
  42.                         ParallaxTimer.stop();
  43.                 }
  44.                
  45.                 public function AttachParallax(mcTarget:MovieClip):void {                      
  46.                         mcTarget.addChild(ParallaxParent);
  47.                 }
  48.                
  49.                 public function DestroyParallax():void {
  50.                         var i:Number = ParallaxObjects.length;
  51.                         while (i--) {
  52.                                 var ThisPane:MovieClip = MovieClip(ParallaxObjects[i]);
  53.                                         ParallaxParent.removeChild(ThisPane);
  54.                         }
  55.                        
  56.                         ParallaxParent.parent.removeChild(ParallaxParent);
  57.                         ParallaxTimer.removeEventListener(TimerEvent.TIMER, UpdateParallax);
  58.                 }
  59.                
  60.                 private function SortParallax():void {
  61.                         var i:Number = ParallaxObjects.length;
  62.                         var j:Number = 0;
  63.                         while (i--) {
  64.                                 var ThisPane:MovieClip = MovieClip(ParallaxObjects[i]);
  65.                                 var zDepth:Number = ThisPane.zDepth;
  66.                                 if (zDepth > j) ParallaxParent.addChild(ThisPane);
  67.                         }
  68.                 }
  69.                
  70.                 private function UpdateParallax(e:Event):void {
  71.                         var mX:Number = ParallaxParent.mouseX;
  72.                         var mY:Number = ParallaxParent.mouseY;
  73.                        
  74.                         var i:Number = ParallaxObjects.length;
  75.                         while (i--) {
  76.                                 // Get This Pane
  77.                                 var ThisPane:MovieClip = MovieClip(ParallaxObjects[i]);
  78.                                
  79.                                 // Current Coordinates
  80.                                 var cX:Number = ThisPane.x;
  81.                                 var cY:Number = ThisPane.y;
  82.                                 var cZ:Number = ThisPane.zDepth;
  83.                                
  84.                                 // Plot Destination Coordinates
  85.                                 var dX:Number = cX - (cX + mX * ThisPane.zDepth/5);
  86.                                 var dY:Number = cY - (cY + mY * ThisPane.zDepth/5);
  87.                                
  88.                                 // Move Pane to Destination
  89.                                 ThisPane.x = cX + ((dX - cX) / ParallaxEase) + ParallaxWidth / 2;
  90.                                 ThisPane.y = cY + ((dY - cY) / ParallaxEase) + ParallaxHeight / 2;
  91.                         }
  92.                 }
  93.                
  94.         }
  95.  
  96. }