如何提取AS3中嵌入图像的宽度和高度?

问题描述:

如何提取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); 
    } 

由于超级应该构造我怎么了解事前尺寸范围内的一切之前,叫什么名字?我使用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); 

这个resizableimage返回bitmapdata。

这可能适合你!