Flash Helps: Moving Background Objects with AS3 (actionScript 3) in Flash
Jesse Harding just posted an awesome Flash sample of how to have a background object that moves as your mouse moves. It’s a pretty cool effect. The stuff in the background moves less and the stuff in the foreground moves more. It almost gives the site a 3D feel.
Jess’es sample uses As2 so I thought I’d do a AS3 version real quick. Use Jesse’s file here and then use this AS3 (actionScript 3) code if you want to use AS3 vs. AS2. Here is sample of what the end product looks like in Flash.
The AS3 code:
function mouseMoveHandler(event):void
{
//If you want to hide the cursor, uncomment the next line
//Mouse.hide();
//Move against
var cursorX = stage.stageWidth – mouseX;
var cursorY = stage.stageHeight – mouseY;
cir1.x = (cursorX * .15);
cir2.x = (cursorX * .15 + 450);
cir3.x = (cursorX * .01 + 350);
cir4.x = (cursorX * .05 + 200);
cir5.x = (cursorX);
cir5.y = (cursorY);
}
addEventListener(Event.ENTER_FRAME, mouseMoveHandler);
Posted in: Flash Helps | Comments Off