2012-07-09 125 views
0

如何提取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。

回答

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); 

    // ... 
} 
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返回bitmapdata。

這可能適合你!