Wednesday, April 20, 2011

AS3 Code: Parallax

//create movieclips to be used in parallax, increasing in width

//stack movieclips, "closest" on top

var speed:Number = .05; //range .01-1

this.addEventListener(Event.ENTER_FRAME,parallax,false,0,true);
 
function parallax(e:Event):void{
    var myPos = mouseX/stage.stageWidth; //# between 0-1
    moveMe(trees_mc,myPos);
    moveMe(hill_mc,myPos);
    moveMe(mtn_mc,myPos);
    moveMe(sky_mc,myPos);
}

function moveMe(me:MovieClip,myPos:Number){
    me.maxwidth = stage.stageWidth - me.width;
    var destinationX = myPos * me.maxwidth;
    me.x += speed * (destinationX - me.x); //easing
}









0 comments:

Post a Comment