rodrigolopezpeker

Genome2D > VirtualMaestro Creation TextureAtlas

Jun 23rd, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Created with IntelliJ IDEA.
  3.  * User: rodrigolopez
  4.  * Date: 6/22/12
  5.  * Time: 11:28 AM
  6.  */
  7. package {
  8.  
  9.     import com.genome2d.components.GCamera;
  10.     import com.genome2d.components.renderables.GMovieClip;
  11.     import com.genome2d.context.GContext;
  12.     import com.genome2d.core.GConfig;
  13.     import com.genome2d.core.GNode;
  14.     import com.genome2d.core.GNodeFactory;
  15.     import com.genome2d.core.Genome2D;
  16.     import com.genome2d.textures.GTextureAlignType;
  17.     import com.genome2d.textures.GTextureAtlas;
  18.     import com.greensock.TweenLite;
  19.     import com.greensock.easing.Expo;
  20.  
  21.     import flash.display.MovieClip;
  22.  
  23.     import flash.display.Sprite;
  24.     import flash.events.Event;
  25.     import flash.display.StageAlign;
  26.     import flash.display.StageQuality;
  27.     import flash.display.StageScaleMode;
  28.     import flash.events.MouseEvent;
  29.     import flash.geom.Rectangle;
  30.  
  31.     [SWF(width="800", height="600", backgroundColor="#DEDEDE", frameRate="60")]
  32.     public class Main extends Sprite {
  33.  
  34.         private var _genome: Genome2D;
  35.         private var _rootNode: GNode;
  36.         private var _camera: GCamera;
  37.  
  38.         private var _chars:Array = [];
  39.         private var _isAlign:Boolean = false ;
  40.  
  41.         /***********************
  42.          * Main Constructor.
  43.          **********************/
  44.         public function Main() {
  45.             if (stage) {
  46.                 addEventListener ( Event.ADDED_TO_STAGE, onAddedToStage );
  47.             } else {
  48.                 onAddedToStage ( null );
  49.             }
  50.         }
  51.  
  52.         private function onAddedToStage( event: Event ): void {
  53.             removeEventListener ( Event.ADDED_TO_STAGE, onAddedToStage )
  54.             initStage ();
  55.             init ();
  56.         }
  57.  
  58.         /***********************
  59.          * Access point.
  60.          **********************/
  61.         private function init(): void {
  62.             initGenome ();
  63.         }
  64.  
  65.         private function initGenome(): void {
  66.             _genome = Genome2D.getInstance ();
  67.  
  68.             var config: GConfig = new GConfig ( null );
  69.             config.enableStats = true;
  70.             config.separateAlphaShaders = true;
  71.             config.viewRect = new Rectangle ( 0, 0, stage.stageWidth, stage.stageHeight )
  72.             _genome.onInitialized.add ( onInit )
  73.             _genome.init ( stage, config );
  74.         }
  75.  
  76.         private function onInit(): void {
  77.             start ();
  78.         }
  79.  
  80.         private function start(): void {
  81.             initCamera ();
  82.  
  83.             // displayList
  84.             var char1: ZombieWalkVector = new ZombieWalkVector ();
  85.             addChild ( char1 );
  86.             char1.x = 100, char1.y = 100;
  87.  
  88.             // toggle button align
  89.             var btn:MovieClip = new align_btn()
  90.             addChild(btn).addEventListener(MouseEvent.CLICK, onAlignClick )
  91.             btn.x = 30, btn.y = 200 ;
  92.  
  93.             addMovieChar(new ZombieWalkVector(), "char1_vector", 0x5500FF00, 200, 100 );
  94.             addMovieChar(new ZombieWalkRaster(), "char1_raster", 0x55FF0000, 300, 100 );
  95.         }
  96.  
  97.         private function onAlignClick( event: MouseEvent ): void {
  98.             _isAlign = !_isAlign;
  99.             TweenLite.to(_chars[0].node.transform, 0.5, { x: _isAlign ? 100 : 200, ease: Expo.easeOut })
  100. //          TweenLite.to(_chars[1].node.transform, 0.5, { x: _isAlign ? 100 : 300 })
  101.         }
  102.  
  103.         private function addMovieChar( pAsset:MovieClip, pId:String, pDebugColor:uint=0x0, pX:int = 0, pY:int=0): GNode {
  104.             // atlasXML to fill
  105.             var zombieXML:XML = <node/>;
  106.             var zombieAtlas:GTextureAtlas = GAtlasUtils.createAtlasFromMovie(pId, pAsset, null, false, pDebugColor, zombieXML );
  107.             // adjust TRIM! to center the textures in the GMovieclip
  108.             GAtlasUtils.adjustTrim(zombieAtlas, zombieXML, GTextureAlignType.CENTER);
  109.             // dispose bitmapDatas from Texture
  110.             GAtlasUtils.disposeBitmapsAtlas(zombieAtlas, GAU.getIdsFromXML(zombieXML));
  111.  
  112.             var char:GMovieClip = GNodeFactory.createNodeWithComponent(GMovieClip) as GMovieClip;
  113.             char.setTextureAtlas(zombieAtlas);
  114.             // no frame labels in movie's timeline, use the frame numbers
  115.             char.frames = GAtlasUtils.buildFrames("", 1, 21 );
  116.             char.play();
  117.             char.frameRate = stage.frameRate ;
  118.             char.node.transform.setPosition(pX, pY)
  119.             _chars.push(char);
  120.             _rootNode.addChild(char.node);
  121.             return char.node ;
  122.         }
  123.  
  124.         private function initCamera(): void {
  125.             _rootNode = GNodeFactory.createNode ( "rootNode" );
  126.             _genome.root.addChild ( _rootNode );
  127.             _camera = GNodeFactory.createNodeWithComponent ( GCamera ) as GCamera;
  128.             _camera.backgroundAlpha = _camera.backgroundBlue = _camera.backgroundGreen = _camera.backgroundRed = 1;
  129.             _camera.node.transform.setPosition ( 400, 300 );
  130.             _rootNode.addChild ( _camera.node );
  131.         }
  132.  
  133.         private function initStage(): void {
  134.             stage.scaleMode = StageScaleMode.NO_SCALE;
  135.             stage.align = StageAlign.TOP_LEFT;
  136.             stage.quality = StageQuality.HIGH;
  137.         }
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment