/Examples/Flash files/robot/src/com/grapefrukt/exporter/debug/AnimationExtractorDebug.as

https://github.com/mefistody/FlashToCocos2D · ActionScript · 111 lines · 67 code · 13 blank · 31 comment · 8 complexity · 5776f5a2362f6c3c8159fc04171afd71 MD5 · raw file

  1. /*
  2. Copyright 2011 Martin Jonasson, grapefrukt games. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without modification, are
  4. permitted provided that the following conditions are met:
  5. 1. Redistributions of source code must retain the above copyright notice, this list of
  6. conditions and the following disclaimer.
  7. 2. Redistributions in binary form must reproduce the above copyright notice, this list
  8. of conditions and the following disclaimer in the documentation and/or other materials
  9. provided with the distribution.
  10. THIS SOFTWARE IS PROVIDED BY grapefrukt games "AS IS" AND ANY EXPRESS OR IMPLIED
  11. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL grapefrukt games OR
  13. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  14. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  15. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  16. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  17. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  18. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19. The views and conclusions contained in the software and documentation are those of the
  20. authors and should not be interpreted as representing official policies, either expressed
  21. or implied, of grapefrukt games.
  22. */
  23. package com.grapefrukt.exporter.debug {
  24. import com.grapefrukt.exporter.animations.Animation;
  25. import com.grapefrukt.exporter.animations.AnimationFrame;
  26. import com.grapefrukt.exporter.textures.BitmapTexture;
  27. import com.grapefrukt.exporter.textures.MultiframeBitmapTexture;
  28. import com.grapefrukt.exporter.textures.TextureSheet;
  29. import flash.display.Bitmap;
  30. import flash.display.BitmapData;
  31. import flash.display.DisplayObject;
  32. import flash.display.Shape;
  33. import flash.display.Sprite;
  34. /**
  35. * ...
  36. * @author Martin Jonasson, m@grapefrukt.com
  37. */
  38. public class AnimationExtractorDebug {
  39. public static function createSkeleton(animation:Animation, sheet:TextureSheet):Sprite {
  40. var skeleton:Sprite = new Sprite;
  41. for each (var partname:String in animation.partNames) {
  42. var texture:BitmapTexture = sheet.getTexture(partname) as BitmapTexture;
  43. var part:Bitmap;
  44. if(texture){
  45. part = new Bitmap(texture.bitmap, "auto", true);
  46. } else {
  47. part = new Bitmap(new BitmapData(44, 142, false, 0xff00ff));
  48. part.x = -23;
  49. part.y = -100;
  50. }
  51. var multiframe:MultiframeBitmapTexture = texture as MultiframeBitmapTexture;
  52. if (multiframe) {
  53. var mask:Shape = new Shape;
  54. mask.graphics.beginFill(0);
  55. mask.graphics.drawRect(multiframe.bounds.x, multiframe.bounds.y, multiframe.frameBounds.width, multiframe.frameBounds.height);
  56. bone.addChild(mask);
  57. part.mask = mask;
  58. }
  59. if(texture){
  60. part.x = -texture.registrationPoint.x;
  61. part.y = -texture.registrationPoint.y;
  62. }
  63. var bone:Sprite = new Sprite;
  64. bone.addChild(part);
  65. bone.name = partname;
  66. skeleton.addChild(bone);
  67. }
  68. var zarray:Array = [];
  69. for each (partname in animation.partNames) {
  70. var t:BitmapTexture = sheet.getTexture(partname) as BitmapTexture;
  71. zarray.push( { part : skeleton.getChildByName(partname), z : t ? t.zIndex : 0 } );
  72. }
  73. zarray.sortOn("z", Array.NUMERIC);
  74. for (var i:int = 0; i < zarray.length; i++) {
  75. skeleton.setChildIndex(zarray[i].part, i);
  76. }
  77. poseSkeleton(skeleton, animation, 0);
  78. return skeleton;
  79. }
  80. public static function poseSkeleton(skeleton:Sprite, animation:Animation, frame:uint = 0):void {
  81. for each (var partname:String in animation.partNames) {
  82. var bone:DisplayObject = skeleton.getChildByName(partname);
  83. var animationframe:AnimationFrame = animation.getFrame(partname, frame);
  84. bone.x = animationframe.x;
  85. bone.y = animationframe.y;
  86. bone.scaleX = animationframe.scaleX;
  87. bone.scaleY = animationframe.scaleY;
  88. bone.alpha = animationframe.alpha;
  89. bone.rotation = animationframe.rotation;
  90. }
  91. }
  92. }
  93. }