Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 25th, 2012  |  syntax: None  |  size: 4.53 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Using custom graphical assets in pure AS3 project
  2. public class neatButton extends neatModule
  3.     {
  4.         private var stateOn:neatButton_on;
  5.         private var stateOff:neatButton_off;
  6.         private var modContainer:Sprite = new Sprite();
  7.  
  8.         public function neatButton()
  9.         {
  10.             addChild(modContainer);
  11.             modContainer.x = 200;
  12.             modContainer.y = 200;
  13.             stateOff = new neatButton_off();
  14.             stateOn = new neatButton_on();
  15.             modContainer.addChild(stateOn);
  16.             modContainer.addChild(stateOff);
  17.  
  18.             modContainer.addEventListener(MouseEvent.MOUSE_OVER, hover);
  19.             modContainer.addEventListener(MouseEvent.MOUSE_OUT, unhover);
  20.         }
  21.  
  22.         private function hover(e:MouseEvent):void
  23.     {
  24.         stateOff.visible = false;
  25.     }
  26.  
  27.     private function unhover(e:MouseEvent):void
  28.     {
  29.         stateOff.visible = true;
  30.     }
  31.     }
  32.        
  33. package com.b99.display.composite
  34.     {
  35.         import flash.display.*;
  36.         import flash.events.MouseEvent;
  37.  
  38.         import com.greensock.*;
  39.  
  40.         /**
  41.          * ...
  42.          * @author Bosworth99
  43.          */
  44.         public class ImgButton extends Sprite
  45.         {
  46.             private var _canvas         :Sprite = new Sprite();
  47.             private var _up             :Sprite;
  48.             private var _over           :Sprite;
  49.             private var _hit            :Shape;
  50.  
  51.             private var _intent         :String;
  52.  
  53.             public function ImgButton(intent:String)
  54.             {
  55.                 _intent = intent;
  56.  
  57.                 super();
  58.                 init();    
  59.             }
  60.  
  61.             private function init():void
  62.             {
  63.                 constructButton();
  64.                 addEventHandlers();
  65.             }
  66.  
  67.             private function constructButton():void
  68.             {
  69.  
  70.                 this.addChild(_canvas);
  71.  
  72.                 switch (_intent)
  73.                 {
  74.                     case "button1":
  75.                     {
  76.                         _up     = new yourUpClip1()
  77.                         _over   = new yourOverClip1();
  78.                         break;
  79.                     }
  80.                     case "button2":
  81.                     {
  82.                         _up     = new yourUpClip2()
  83.                         _over   = new yourOverClip2();
  84.                         break;
  85.                     }
  86.                     case "button3":
  87.                     {
  88.                         _up     = new yourUpClip3()
  89.                         _over   = new yourOverClip3();
  90.                         break;
  91.                     }
  92.                 }
  93.  
  94.                 _canvas.addChild(_up);
  95.  
  96.                 with (_over)
  97.                 {
  98.                     alpha   = 0;
  99.                 }
  100.                 _canvas.addChild(_over);
  101.  
  102.                 _hit = new Shape();
  103.                 with (_hit)
  104.                 {
  105.                     graphics.beginFill(0x00FF40, 0);
  106.                     graphics.drawRect(0, 0, _up.width + 5, _up.height +5);
  107.                     graphics.endFill();
  108.                     x       = -5;
  109.                     y       = -5;
  110.                 }
  111.                 _canvas.addChild(_hit)
  112.  
  113.                 _up.cacheAsBitmap       = true;
  114.                 _over.cacheAsBitmap     = true;
  115.                 _hit.cacheAsBitmap      = true;
  116.  
  117.                 this.buttonMode         = true;
  118.                 this.mouseEnabled       = true;
  119.  
  120.             }
  121.  
  122.             private function addEventHandlers():void
  123.             {
  124.                 _hit.addEventListener(MouseEvent.MOUSE_OVER,    over, false, 0, true);
  125.                 _hit.addEventListener(MouseEvent.MOUSE_OUT,     out, false, 0, true);
  126.             }
  127.  
  128.             private function over(e:MouseEvent):void
  129.             {
  130.                 TweenLite.to(_over, .2,     {alpha:1   } );
  131.             }
  132.             private function out(e:MouseEvent):void
  133.             {
  134.                 TweenLite.to(_over, .2,     {alpha:0   } );
  135.             }
  136.  
  137.             public function destroy():void
  138.             {
  139.                 _hit.removeEventListener(MouseEvent.MOUSE_OVER, over);
  140.                 _hit.removeEventListener(MouseEvent.MOUSE_OUT,  out);
  141.  
  142.                 _canvas.removeChild(_up);
  143.                 _up = null;        
  144.  
  145.                 _canvas.removeChild(_over);
  146.                 _over = null;      
  147.  
  148.                 _canvas.removeChild(_hit);
  149.                 _hit = null;
  150.  
  151.                 this.removeChild(_canvas)
  152.                 _canvas = null;
  153.  
  154.                 this.parent.removeChild(this);
  155.             }
  156.     //+++++++++++++++++++++++++++++++  end ++++++++++++++++++++++++++++++++++++++++
  157.         }
  158.  
  159.     }