1

In my ActionScript 3 project, FXG graphics are imported as instances of SpriteVisualElement as follows:

// with the FXG file myPackage/myFXGFile.fxg
import myPackage.myFXGFile;
var myFXG:SpriteVisualElement = new myPackage.myFXGFile();
addChild( myFXG );

The FXG files have "frames" defined using the viewWidth and viewHeight properties of <Graphic>. An example of a simple image follows. It consists of a rectangle centered on a larger rectangle, with only the former being part of the view "frame".

<?xml version="1.0" encoding="utf-8"?>
<Graphic viewWidth="100" viewHeight="200" xmlns="http://ns.adobe.com/fxg/2008" version="2">    
    <Rect id="rect1" width="120" height="240" x="-10" y="-20">
         <fill><SolidColor color="#FF0000"/></fill>
    </Rect>
    <Rect id="rect1" width="100" height="200" x="0" y="0">
         <fill><SolidColor color="#0000FF"/></fill>
    </Rect>
</Graphic>

In the code, I need to know the dimensions of this frame, i.e. the values of viewWidth and viewHeight. Is there any way to obtain them from the imported files? Is there a solution without parsing the FXG file manually?

Update: I just looked through all methods and properties available to SpriteVisualElement, and getPreferredBoundsWidth() and -Height() seem to do what I'm looking for. I'm not confident enough with the Flex source to find the origin of those values, though, and a couple of conditions make me unsure. Is this the right and recommended way?

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
Tim
  • 13,904
  • 10
  • 69
  • 101
  • FXG files are not subclasses of Sprite so far as I knew; they exist outside of the Flash object hierarchy. I'm a bit confused as to what you're actually importing; but if it is an actual FXG element you should be able to do mySprite.viewWidth and mySprite.viewHeight . Of course the naming convention is confusing since a Sprite instance != FXG. – JeffryHouser Aug 08 '11 at 18:20
  • They seem to be nicely casted to `Sprite` at least, as `mySprite:Sprite` above works as expected. Using the instance as a `spark.core::SpriteVisualElement` (as suggested by `getQualifiedSuperclassName()`) or `myPackage.myFXGFile` doesn't give it any `viewWidth` property. – Tim Aug 08 '11 at 18:29
  • Without seeing more code; it is hard to comment further. Sprites do not have a viewWidth property. Are you sure that you're using a FXG graphic and not a sprite? Can you show us your myFXGFile code? The way I've used FXG elements in a Flex app is to create a class variable pointed to the FXG file; then I use that instance of Class to create an instance of the FXG asset. This is consistent w/ how the Flex Framework does it. – JeffryHouser Aug 08 '11 at 19:48
  • Thanks for your comments. I've edited the post with more info, and removed the parts about casting to `Sprite`. Is the posted code enough? With "a class variable pointed to the FXG file", do you mean using [Embed]? – Tim Aug 08 '11 at 21:57
  • No; not with embed. Something like this: var myFXGInstanceClass : Class = com.myFXGFile; myfXGInstance : DisplayObject = new myFXGInstanceClass(); – JeffryHouser Aug 09 '11 at 00:02
  • Pop open some of the Flex Framework code and find some samples. I'm blanking on a component that does this, though. – JeffryHouser Aug 09 '11 at 00:12

1 Answers1

0

SpriteVisualElement is a class that inherits from FlexSprite and flash.display.Sprite. FXG is an instance of this:

If you use ActionScript to add an FXG component to an application, it should be of type SpriteVisualElement.

There are two other methods outside of the aforementioned getPreferredBoundsWidth() and getPreferredBoundsHeight():

getLayoutBoundsHeight   ()  method   
public function getLayoutBoundsHeight(postLayoutTransform:Boolean = true):Number

Language Version :  ActionScript 3.0
Product Version :   Flex 4
Runtime Versions :  Flash Player 10, AIR 1.5

Returns the element's layout height. This is the size that the element uses to draw on screen.

Parameters
    postLayoutTransform:Boolean (default = true) — When postLayoutTransform is true, the method returns the element's bounding box width. The bounding box is in the element's parent coordinate space and is calculated from the element's layout size and layout transform matrix.

Returns
    Number — The element's layout height.


getLayoutBoundsWidth    ()  method   
public function getLayoutBoundsWidth(postLayoutTransform:Boolean = true):Number

Language Version :  ActionScript 3.0
Product Version :   Flex 4
Runtime Versions :  Flash Player 10, AIR 1.5

Returns the element's layout width. This is the size that the element uses to draw on screen.

Parameters
    postLayoutTransform:Boolean (default = true) — When postLayoutTransform is true, the method returns the element's bounding box width. The bounding box is in element's parent coordinate space and is calculated from the element's layout size and layout transform matrix.

Returns
    Number — The element's layout width. 

Is there a solution without parsing the FXG file manually?

Use an FXG Editor as an alternative.

References

Community
  • 1
  • 1
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265