unable to set width/height on FlexSprite
I'm trying to set width/height of a FlexSprite object in ActionScript but it doesn't seem to work at all. I'm still a rookie when it comes to ActionScript and I don't understand why I can't set the dimensions of that object. I know I could use a UIComponent but I need something more lightweight since I'm going to add hundreds of them.
public class MyComp extends UIComponent
{
public function MyComp()
{
super();
}
private var _sprite:FlexSprite;
private var _button:Button;
override protected function createChildren():void {
if (!_sprite) {
_sprite = new FlexSprite();
_sprite.width = 200;
_sprite.height = 200;
addChild(_sprite);
}
if (!_button) {
_button = new Button();
_button.label = "Click me";
_button.width = 150;
_button.height = 40;
addChild(_button);
}
super.createChildren();
}
override protected function measure():void {
开发者_开发知识库 measuredHeight = measuredMinHeight = 200;
measuredWidth = measuredMinWidth = 200;
}
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
super.updateDisplayList(unscaledWidth,unscaledHeight);
trace('dimz of _sprite', _sprite.width, _sprite.height);
_sprite.width = 100; // doesn't work either
trace('dimz of _sprite again', _sprite.width, _sprite.height);
trace('dimz of _button', _button.width, _button.height);
}
}
The output I get is:
dimz of _sprite 0 0
dimz of _sprite again 0 0
dimz of _button 150 40
I plan to add other objects inside the FlexSprite and I need to set their dimensions explicitly as well so I really need to figure out how to do that.
A few things that may be issues:
- A Sprite, as best I understand it, is kind of like a container. By itself it has no visual elements; so nothing will be displayed on the screen just by using an instance of a FlexSprite.
- You do not position your elements in the component; and UIComponent does not have any code to automatically position its children; therefore the button would show up on top of the sprite anyway. You can change that by adding some positioning code in updateDisplayList(). Something like this:
'
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
super.updateDisplayList(unscaledWidth,unscaledHeight);
_sprite.x = 10;
_sprite.y = 10;
_button.x = 400;
_button.y = 400;
}
I'm guessing--but don't know b/c source is not available--that the Sprite (which FlexSprite extends) is re-sizing itself to it's Children; which is currently non-existent.
精彩评论