Parallax Based on Y

Actionscript:
  1. [SWF(backgroundColor=0x000000, frameRate=30)]
  2.  
  3. var elements:Array = new Array();
  4. for (var i:int = 0; i<200; i++){
  5.     var c:MovieClip= MovieClip(addChild(new MovieClip()));
  6.     c.x = Math.random()*(stage.stageWidth + 100) - 50;
  7.     c.y = Math.random()*stage.stageHeight;
  8.     with(c.graphics) lineStyle(2, 0xFFFFFF,3), drawCircle(0,0,2 + c.y / 20);
  9.     c.startX = c.x;
  10.     elements.push(c);
  11. }
  12.  
  13. var offset:Number = 0;
  14. var t:Number = 0;
  15.  
  16. addEventListener(Event.ENTER_FRAME, onLoop);
  17. function onLoop(evt:Event):void {
  18.     t+=.1;
  19.     offset = 200 * Math.cos(t);
  20.     for (var i:int = 0; i<elements.length; i++){
  21.        elements[i].x = elements[i].startX + offset / ((stage.stageWidth - elements[i].y) / 80);
  22.     }
  23. }

I wrote this snippet originally to automatically add parallax motion to a bunch of quick drawings I did within the flash IDE. Each movieClip in the elements array is moved from left to right based on it's y position. Clips with a higher y value will oscillate more from left to right than clips with lower y values.

Here is the original drawing I used this on.

And here is what the above snippet will create.

This entry was posted in motion and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Comments

  1. Janice
    Posted March 6, 2009 at 1:46 pm | Permalink

    thank you, i was looking for something like this.. now to mod it for something that is based on where mouseX and mouseY are…

  2. Posted March 6, 2009 at 2:31 pm | Permalink

    hey janice,

    you may know this… but all you need to do is replace the offset variable with something like:

    offset = mouseX - stage.stageWidth / 2;

    and then duplicate the the same logic for the y axis. You can also tweak the number 80, lower and it and the effect will become less extreme.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*