0

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

4

2 回答 2

1

您可以通过以下方式阅读这些属性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);

    // ...
}
于 2012-07-09T10:04:17.063 回答
0

您可以使用缩放提取/调整图像大小。您可以调整具有相同纵横比或不同纵横比的图像大小。如果您不想重新调整大小,则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);

此 resizableimage 返回位图数据。

愿这对你有用!

于 2012-07-09T13:49:06.050 回答