2012-07-09 3 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); 
    } 

은 내가 미리 치수에 대해 배울 어떻게 생성자 내에서 다른 모든 전에 호출해야합니다 는 여기에 내가 할 노력하고있어 무엇인가? 내 IDE로 FlashDevelop을 사용하고 있습니다.

답변

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를 반환합니다.

5 월이 작동합니다!

관련 문제