これらのプロパティは、次の方法で読み取ることができます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);
// ...
}