如何在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。
答案 0 :(得分: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);
// ...
}
答案 1 :(得分: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。
这可能适合你!