【问题标题】:How do I extract the width and height of an embedded image in AS3?如何在 AS3 中提取嵌入图像的宽度和高度?
【发布时间】:2012-07-08 17:05:32
【问题描述】:

如何在 AS3 中提取嵌入图像的宽度和高度,而不是明确说明尺寸? 这是我正在尝试做的事情:

    [Embed(source="../../lib/spaceship.png")]
    private var ShipImage:Class;
    private var ship_image:BitmapData;

    public function Ship(x:int, y:int, width:Number, height:Number) 
    {
        super(x, y, 36, 64);
        ship_image = new ShipImage().bitmapData;
        speed = new Point(0, 0);
    }

由于 super 应该在构造函数中的所有其他内容之前调用,我如何事先了解尺寸?我使用 FlashDevelop 作为我的 IDE。

【问题讨论】:

    标签: actionscript-3 flash apache-flex flashdevelop


    【解决方案1】:

    您可以通过缩放提取/调整图像大小。您可以调整具有相同纵横比或不同纵横比的图像大小。如果您不想重新调整大小,则 scaleX && scaleY 的值为 1.0 , 如果您想以原始大小的一半重新调整大小,则两个因子的值均为0.5 如果要旋转图像,请使用平移。

    var matrix:Matrix = new Matrix();
    matrix.scale(scalex, scaley);
    matrix.translate(translatex, translatey);
    
    var resizableImage:BitmapData = new BitmapData(size, size, true);
    resizableImage.draw(data, matrix, null, null, null, true);
    

    这个可调整大小的图像返回位图数据。

    愿这对你有用!

    【讨论】:

      【解决方案2】:

      您可以通过BitmapData#rect阅读这些属性:

      public function Ship(x:int, y:int, width:Number, height:Number) 
      {
          // Would be better if you haven't to pass width and height
          super(x, y, 0, 0);
      
          // Get the bitmap data
          ship_image = new ShipImage().bitmapData;
      
          // Set width and height
          width  = ship_image.rect.width;
          height = ship_image.rect.height;
      
          // ...
      }
      

      其他静态解决方案:

      [Embed(source="../../lib/spaceship.png")]
      private static const ShipImage:Class;
      
      private static var spriteWidth:int;
      private static var spriteHeight:int;
      
      private static function calculateSpriteSize():void
      {
          // Get the sprite "rectangle"
          var rect:Rectangle = new ShipImage().bitmapData.rect;
      
          // Set width and height
          spriteWidth  = ship_image.rect.width;
          spriteHeight = ship_image.rect.height;
      }
      
      // Call the method into the class body
      // (yes you can do that!)
      calculateSpriteSize();
      
      public function Ship(x:int, y:int, width:Number, height:Number) 
      {
          // Retrieve the sprite size
          super(x, y, spriteWidth, spriteHeight);
      
          // ...
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多