Actionscript:
-
[SWF(backgroundColor=0x000000, frameRate=30)]
-
-
var elements:Array = new Array();
-
for (var i:int = 0; i<200; i++){
-
var c:MovieClip= MovieClip(addChild(new MovieClip()));
-
c.x = Math.random()*(stage.stageWidth + 100) - 50;
-
c.y = Math.random()*stage.stageHeight;
-
with(c.graphics) lineStyle(2, 0xFFFFFF,3), drawCircle(0,0,2 + c.y / 20);
-
c.startX = c.x;
-
elements.push(c);
-
}
-
-
var offset:Number = 0;
-
var t:Number = 0;
-
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
function onLoop(evt:Event):void {
-
t+=.1;
-
offset = 200 * Math.cos(t);
-
for (var i:int = 0; i<elements.length; i++){
-
elements[i].x = elements[i].startX + offset / ((stage.stageWidth - elements[i].y) / 80);
-
}
-
}
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.
2 Comments
thank you, i was looking for something like this.. now to mod it for something that is based on where mouseX and mouseY are…
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.