hicimos tendríamos que adaptar su código a los proyectos que tengamos. Por ejemplo,
habíamos hecho que al hacer click en el boton redireccione a una página web, si quisiéramos
que redireccione a un número de fotograma, tendríamos que modificar el código de la clase.
En esta oportunidad vamos a crear una clase Button que sea reutilizable y adaptable a
todos los proyectos que tengamos.
Para lograr esto vamos a separar la funcionalidad de la estética.
Video:
http://www.youtube.com/watch?v=nelZkgLAS0c
Archivos del Tutorial:
http://www.megaupload.com/?d=R2CIDF5C
Mirrors:
http://rapidshare.com/#!download|121...-Button.zip|62
http://www.mediafire.com/?ldb6fv1vspyvkuq
Código:
package ar.com.lucasmoyano.buttons { import flash.display.MovieClip; import flash.events.Event; import flash.events.MouseEvent; import flash.media.Sound; public class Button extends MovieClip { private var content:MovieClip; private var indexPressed:int; private var flag:Boolean; private var soundMouseOver:Sound; private var soundMouseOut:Sound; private var soundMouseDown:Sound; private var soundMouseUp:Sound; public function Button(_content:MovieClip = null, _soundMouseUp:Sound = null, _soundMouseOver:Sound = null, _soundMouseOut:Sound = null, _soundMouseDown:Sound = null):void { addEventListener(Event.ADDED_TO_STAGE, onAddedStage); content = _content; soundMouseOver = _soundMouseOver; soundMouseOut = _soundMouseOut; soundMouseDown = _soundMouseDown; soundMouseUp = _soundMouseUp; flag = false; } private function onAddedStage(e:Event):void { content.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver); try { indexPressed = content.currentLabels[0].frame; } catch (error:Error) { trace("Class Button - Error: In the MovieClip content, enter a label name to Frame"); } content.mouseChildren = false; content.buttonMode = true; content.stop(); addChild(content); } private function onMouseOver(e:MouseEvent):void { content.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut); content.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); content.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); flag = true; if (content.currentFrame == 1) content.addEventListener(Event.ENTER_FRAME, onEnterFrame); if (soundMouseOver != null) soundMouseOver.play(); } private function onMouseOut(e:MouseEvent):void { content.removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut); content.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); content.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp); flag = false; if (soundMouseOut != null) soundMouseOut.play(); } private function onMouseDown(e:MouseEvent):void { content.gotoAndStop(indexPressed); if (soundMouseDown != null) soundMouseDown.play(); } private function onMouseUp(e:MouseEvent):void { content.play(); if (soundMouseUp != null) soundMouseUp.play(); } private function onEnterFrame(e:Event):void { if (content.currentFrame < indexPressed) { if (flag) { if (content.currentFrame < indexPressed - 1) content.nextFrame(); } else content.prevFrame(); } if (content.currentFrame == 1) content.removeEventListener(Event.ENTER_FRAME, onEnterFrame); } // GETTERS AND SETTERS public function get Content():MovieClip { return content; } public function set Content(value:MovieClip):void { content = value; } public function get SoundMouseOver():Sound { return soundMouseOver; } public function set SoundMouseOver(value:Sound):void { soundMouseOver = value; } public function get SoundMouseOut():Sound { return soundMouseOut; } public function set SoundMouseOut(value:Sound):void { soundMouseOut = value; } public function get SoundMouseDown():Sound { return soundMouseDown; } public function set SoundMouseDown(value:Sound):void { soundMouseDown = value; } public function get SoundMouseUp():Sound { return soundMouseUp; } public function set SoundMouseUp(value:Sound):void { soundMouseUp = value; } } }