簡體   English   中英

如何在AS3中提取嵌入式圖像的寬度和高度?

[英]How do I extract the width and height of an embedded image in AS3?

如何在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。

您可以通過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);

    // ...
}

您可以按比例提取/調整圖像大小。 您可以使用相同的縱橫比或不同的縱橫比來調整圖像尺寸。 如果您不想調整大小,那么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);

此可調整大小的圖像返回位圖數據。

可能對您有用!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM