package { import flash.display.MovieClip; import flash.display.Loader; import flash.display.DisplayObject; import flash.display.Bitmap; import flash.display.BitmapData; import flash.geom.ColorTransform; import flash.geom.Matrix; import flash.geom.Point; import flash.net.URLRequest; import flash.external.ExternalInterface; import flash.events.Event; import flash.events.MouseEvent; import flash.geom.Rectangle; import flash.events.TimerEvent; import flash.utils.Timer; public class Flag extends MovieClip { // BEGIN FLAG PARAMETERS // peel position on page. Values: topleft || topright || bottomleft || bottomright private var peelPosition:String; // mirror the image on peel. Values: true || false private var mirror:Boolean; // color of peel. Values: golden || silver || custom private var peelColor:String; // color of peel style. Values: flat || gradient private var peelColorStyle:String = "gradient"; // red value of custom color. Values 0-255 private var redValue:uint; // green value of custom color. Values 0-255 private var greenValue:uint; // blue value of custom color. Values 0-255 private var blueValue:uint; // speed of flag movement. Values: 1-9 private var flagSpeed:uint; // END FLAG PARAMETERS private var imageLoader:Loader; private var toScaleX:Number; private var toScaleY:Number; private var mirrorImageBitmap:Bitmap = new Bitmap(); private var updateMirrorImageTimer:Timer; private const childNames = new Array("image", "inner_shadow", "peel", "top_shadow", "mirror_image", "button"); // this flag controls the movement direction. False -> forward. True -> backward private var moveBackwards:Boolean = false; // tracking current frame of childs private var flagCurrentFrame:uint; private const FRAMESCOUNT:uint = 50; /* * Constructor */ public function Flag(peelPosition:String, mirror:Boolean, peelColor:String, peelColorStyle:String, redValue:uint, greenValue:uint, blueValue:uint, flagSpeed:uint, imageLoader:Loader, toScaleX:Number, toScaleY:Number ) { this.peelPosition = peelPosition; this.mirror = mirror; this.peelColor = peelColor; this.peelColorStyle = peelColorStyle; this.redValue = redValue; this.greenValue = greenValue; this.blueValue = blueValue; this.flagSpeed = flagSpeed; this.imageLoader = imageLoader; this.toScaleX = toScaleX; this.toScaleY = toScaleY; //set peel color if (peelColor == "golden") { setPeelColor(50, 100, 10); } else if (peelColor == "silver") { if (peelColorStyle == "flat") { setPeelColor(192, 192, 192); } } else { setPeelColor(redValue, greenValue, blueValue); } // add small image to its container setImage(); // add mirror image to peel if configured to do so if (mirror) { setMirrorImage(); } // add mouse over event to peel MovieClip(getChildByName("button")).peelSmallButton.addEventListener(MouseEvent.MOUSE_OVER, smallButtonAction); this.flagCurrentFrame = 1; this.addEventListener(Event.ENTER_FRAME, do_move); } /* * Sets color of peel. */ private function setPeelColor (redValue, greenValue, blueValue: uint) : void { var peelTransform:ColorTransform; if (this.peelColorStyle == "flat") { peelTransform = new ColorTransform(0,0,0,1,redValue,greenValue,blueValue,0); } else { peelTransform = new ColorTransform(1,1,1,1,redValue,greenValue,blueValue,0); } MovieClip(getChildByName("peel")).transform.colorTransform = peelTransform; } /* * Flip horizontal */ private function flipHorizontal(dsp:DisplayObject):void { var matrix:Matrix = dsp.transform.matrix; matrix.transformPoint(new Point(dsp.width/2, dsp.height/2)); if(matrix.a > 0){ matrix.a = -1 * matrix.a; matrix.tx = dsp.width + dsp.x; } else { matrix.a = -1 * matrix.a; matrix.tx = dsp.x - dsp.width; } dsp.transform.matrix = matrix; } /* * Flip vertical */ private function flipVertical(dsp:DisplayObject):void { var matrix:Matrix = dsp.transform.matrix; matrix.transformPoint(new Point(dsp.width/2, dsp.height/2)); if(matrix.d > 0){ matrix.d = -1 * matrix.d; matrix.ty = dsp.height + dsp.y; } else { matrix.d = -1 * matrix.d; matrix.ty = dsp.y - dsp.height; } dsp.transform.matrix = matrix; } /* * Move clips to specific frame */ private function gotoFrame (frame:uint) { //gotoAndStop(frame); for each (var childname:String in childNames) { MovieClip(getChildByName(childname)).gotoAndStop(frame); } this.flagCurrentFrame = frame; } /* * Move clips forward */ private function goNext (speed:uint = 1) : void { var frameToGo:uint; frameToGo = this.flagCurrentFrame + speed; if (frameToGo > this.FRAMESCOUNT - 1) { frameToGo = this.FRAMESCOUNT - 1; } gotoFrame(frameToGo); } /* * Move clips backward */ private function goPrev (speed:uint = 1) : void { var frameToGo:int; frameToGo = this.flagCurrentFrame - speed; if (frameToGo < 2) { frameToGo = 2; } gotoFrame(frameToGo); } /* * Move clips */ private function do_move (e:Event) { if (this.flagCurrentFrame == 2) { // when flag is moving backward this makes it move forward again moveBackwards = false; } if (this.flagCurrentFrame == this.FRAMESCOUNT - 1) { // when movie reach here starts moving backward moveBackwards = true; } if (moveBackwards) { goPrev(flagSpeed); } else { goNext(flagSpeed); } } /* * Moves a clip in a loop */ private function doLoop(e:Event) : void { if (e.target.currentFrame == e.target.totalFrames) { e.target.gotoAndPlay(1); } else { e.target.nextFrame(); } } /* * Sets the image */ private function setImage () : void { if (imageLoader.getChildAt(0) is MovieClip) { imageLoader.getChildAt(0).addEventListener(Event.ENTER_FRAME, doLoop); } MovieClip(getChildByName("image")).small_image.addChild(imageLoader); MovieClip(getChildByName("image")).small_image.scaleX = 1/toScaleX; MovieClip(getChildByName("image")).small_image.scaleY = 1/toScaleY; // mirror peel image if configured on top left corner if (peelPosition == "topleft" || peelPosition == "bottomleft") { flipHorizontal(MovieClip(getChildByName("image")).small_image); } if (peelPosition == "bottomleft" || peelPosition == "bottomright") { flipVertical(MovieClip(getChildByName("image")).small_image); } } /* * Updates the mirror image on flag when it is a clip */ private function updateMirrorImage(e:TimerEvent):void { //updates the reflection to visually match the movie clip var mirrorBitmapData:BitmapData; mirrorBitmapData = new BitmapData(imageLoader.width, imageLoader.height, true, 0xFFFFFF); mirrorBitmapData.draw(imageLoader); mirrorImageBitmap.bitmapData = mirrorBitmapData; } /* * Sets the mirror image on peel */ private function setMirrorImage () : void { mirrorImageBitmap.bitmapData = new BitmapData(imageLoader.width, imageLoader.height, true, 0xFFFFFF); mirrorImageBitmap.bitmapData.draw(imageLoader); MovieClip(getChildByName("mirror_image")).mirror_small_image_container.mirror_small_image.addChild(mirrorImageBitmap); if (imageLoader.getChildAt(0) is MovieClip) { // if the media on flag is a movieclip this reflection has to be updated every 100ms to // properly display the current frame of the loading flash movie updateMirrorImageTimer = new Timer(100, 0); updateMirrorImageTimer.addEventListener(TimerEvent.TIMER, updateMirrorImage); updateMirrorImageTimer.start(); } MovieClip(getChildByName("mirror_image")).mirror_small_image_container.mirror_small_image.scaleX = 1/toScaleX; MovieClip(getChildByName("mirror_image")).mirror_small_image_container.mirror_small_image.scaleY = 1/toScaleY; // mirror the media on peel if it is configured on top right corner if (peelPosition == "topright" || peelPosition == "bottomright") { flipHorizontal(MovieClip(getChildByName("mirror_image")).mirror_small_image_container.mirror_small_image); } if (peelPosition == "bottomleft" || peelPosition == "bottomright") { flipVertical(MovieClip(getChildByName("mirror_image")).mirror_small_image_container.mirror_small_image); } } /* * open the peel */ private function do_peel () : void { ExternalInterface.call("doPeel", this.peelPosition); } /* * mouse over event handler */ private function smallButtonAction (e:MouseEvent) : void { do_peel(); } } }