Category Archives: Code Lib

Code Lib

Casa Lib

昨天在Vin那里看到一个SWFLibrary类,觉得挺有用。搜索了一下,发现Casa Lib这个类库也有相似的功能(LibraryLoad & LibraryManager),顺便一起介绍一下。

使用ActionScript 3开发很通常的一个工作是加载外部SWF文件做为运行时共享。一般是调用ApplicationDomain类的getDefinition()来得到类定义,再去实例化。但是对一般开发者来说ApplicationDomain类和应用程序域等问题较难理解,所以我们可以用上面提到的两个类简化这些重复的操作。

除此之外,Casa Lib还有很多实用的功能,涵盖了ActionScript 3开发的方方面面,推荐大家用一下。

Casa Lib svn|文档

Quaternion Rotation Demo

昨天发布的RubiksCube控制旋转部分的代码是操作根据物体自身的坐标,觉得还是有点怪,所以今天花了点时间改写了一下:实现了世界坐标的旋转。主要是解决了坐标系换算的问题,方法是用Quaternion生成旋转矩阵。代码贴出来,给有需要的朋友。

  1. package {
  2. import org.papervision3d.core.math.Quaternion;
  3. import org.papervision3d.core.proto.LightObject3D;
  4. import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
  5. import org.papervision3d.materials.utils.MaterialsList;
  6. import org.papervision3d.objects.DisplayObject3D;
  7. import org.papervision3d.objects.primitives.Cube;
  8. import org.papervision3d.view.BasicView;
  9.  
  10. import flash.events.*;
  11.  
  12. /**
  13. * @author kevincao
  14. */
  15. public class RotateControlDemo extends BasicView {
  16.  
  17. protected var cube : Cube;
  18.  
  19. private var isDown : Boolean = false;
  20.  
  21. private var _x : Number;
  22. private var _y : Number;
  23.  
  24. private var _vx : Number = 0;
  25. private var _vy : Number = 0;
  26.  
  27. public function RotateControlDemo() {
  28. init();
  29. }
  30.  
  31. protected function init() : void {
  32. var light : LightObject3D = new LightObject3D();
  33. light.x = 1000;
  34. light.y = 1000;
  35. light.z = -1000;
  36. cube = new Cube(new MaterialsList({all : new FlatShadeMaterial(light)}), 300, 300, 300);
  37. scene.addChild(cube);
  38.  
  39. addEventListener(Event.ENTER_FRAME, tick);
  40. stage.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);
  41. stage.addEventListener(MouseEvent.MOUSE_UP, upHandler);
  42. }
  43.  
  44. private function downHandler(event : MouseEvent) : void {
  45. _x = mouseX;
  46. _y = mouseY;
  47. isDown = true;
  48. stage.addEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
  49. }
  50.  
  51. private function upHandler(event : MouseEvent) : void {
  52. isDown = false;
  53. stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
  54. }
  55.  
  56. private function moveHandler(event : MouseEvent) : void {
  57. _vx = (mouseX - _x) * .4;
  58. _vy = (mouseY - _y) * .4;
  59. _x = mouseX;
  60. _y = mouseY;
  61.  
  62. var q : Quaternion = Quaternion.createFromEuler(-_vx, 0, -_vy, true);
  63. cube.transform.calculateMultiply3x3(q.matrix, cube.transform);
  64. }
  65.  
  66. private function tick(event : Event) : void {
  67. if(!isDown) {
  68. if(_vx != 0 || _vy != 0) {
  69. _vx *= .9;
  70. _vy *= .9;
  71. if(Math.abs(_vx) < .5) {
  72. _vx = 0;
  73. }
  74. if(Math.abs(_vy) < .5) {
  75. _vy = 0;
  76. }
  77. var q : Quaternion = Quaternion.createFromEuler(-_vx, 0, -_vy, true);
  78. cube.transform.calculateMultiply3x3(q.matrix, cube.transform);
  79. }
  80. }
  81. singleRender();
  82. }
  83. }
  84. }

Embedding Assets with AS3

在AS3中使用元数据嵌入资源的例子:

  1. package
  2. {
  3. import flash.display.Bitmap;
  4. import flash.display.MovieClip;
  5. import flash.display.Sprite;
  6. import flash.media.Sound;
  7. import flash.text.Font;
  8. import flash.text.TextField;
  9. import flash.text.TextFormat;
  10. import flash.text.TextFieldAutoSize;
  11.  
  12. public class Main extends Sprite {
  13.  
  14. [Embed(source = '../assets/fd-logo.jpg')]
  15. private var EmbeddedImage:Class;
  16. private var image:Bitmap;
  17.  
  18. [Embed(source = '../assets/clip.swf', symbol = 'EmbeddedClipExample')]
  19. private var EmbeddedClip:Class;
  20. private var clip:MovieClip;
  21.  
  22. [Embed(source = '../assets/bird-sound.mp3')]
  23. private var EmbeddedSound:Class;
  24. private var sound:Sound;
  25.  
  26. [Embed(source='../assets/Arborcrest.ttf', fontName='Arborcrest')]
  27. public var EmbeddedFont:Class;
  28. private var field:TextField;
  29.  
  30. public function Main():void {
  31. image = new EmbeddedImage();
  32. image.x = 50;
  33. image.y = 50;
  34. addChild(image);
  35.  
  36. clip = new EmbeddedClip();
  37. clip.x = 200;
  38. clip.y = 50;
  39. addChild(clip);
  40.  
  41. var s:Sound = new EmbeddedSound();
  42. s.play(0, 1000);
  43.  
  44. Font.registerFont(EmbeddedFont);
  45. var style:TextFormat = new TextFormat();
  46. style.font = "Arborcrest";
  47. style.size = 45;
  48. field = new TextField();
  49. field.autoSize = TextFieldAutoSize.LEFT;
  50. field.embedFonts = true;
  51. field.defaultTextFormat = style;
  52. field.text = "Embedded Font Example";
  53. field.x = 50;
  54. field.y = 150;
  55. addChild(field);
  56. }
  57. }
  58. }

注意必须用Flex编译,Flash的编译器自动忽略元数据。

其他参考:

Bit-101 Embedding Resources with AS3