I converted this to AS3 -
I needed a flag effect for a full size screensaver - all the ones i tested using Perlin Distort methods were way too taxing on the processor. This code gets the job done and doesn't kill the processor. Thanks!
//the number of slats used to make the flag effect privatevar number_of_slats:Number = 30; //speed at which the flag waves privatevar speed_of_flag:Number = .3; //the ammount the flag waves privatevar ammount_of_movement:Number = 9; //the length of the wave. small numbers make a larger wave. privatevar wave_length:Number = 0.2; //width of each slat privatevar slat_width:Number //the flag bitmap with mask over it privatevar _photo:Photo; privatevar _photoArray:Array = newArray(); privatevar j:Number;
//FLAG ---------------------------------------------------------------------------- //creates the movie clips of the flag for(var i:uint = 0; i<number_of_slats; i++){ //creates an instance of the flag movie _photo = new Photo() as Photo; _photo.name = 'photo' + i; this.addChild(_photo); if(!slat_width){ slat_width = _photo.width/number_of_slats; } _photoArray.push(_photo); //sets the mask width to the slats width. the +1 is to remove hariline gaps _photo.flag_mask.width = slat_width+1; //sets the masks possition according to its number _photo.flag_mask.x = slat_width*i; } j=1;
// add enter fram handler this.addEventListener(Event.ENTER_FRAME, enterFrameHandler); //END FLAG --------------------------------------------------------------------------
}
//enterFrameHandler privatefunction enterFrameHandler(e:Event):void{ j += speed_of_flag; //trace(j); var i:uint = 0; foreach(var pSlice:Photo in _photoArray){ //move the slats Y position based on sin. pSlice.y = Math.sin(j + i * wave_length)* ammount_of_movement; i++; } }