AS3 Class Extending Sprite
I am new to ActionScript so excuse me if this is a stupid question.
So, I created a class, extended the sprite class, and now in the constructor I am trying to set the Sprite's width and height properties which are inherited from the DisplayObject. However, after I set this.width an开发者_运维技巧d this.height, and print the values, I get 0 for both.
What the heck is going on? When I view the livedocs I see that DisplayObject has width and height listed as public properties. I have been able to instantiate a Sprite directly, and set the width and height after it's been instantiated, so I don't get it.
package {
import flash.display.*;
public class ScrollBar extends Sprite {
public function ScrollBar(width:Number, height:Number) {
trace(width + "x" + height);
this.width = width;
this.height = height;
trace(this.width + "x" + this.height);
}
}
Output:
20x400
0x0
From the docs:
The width is calculated based on the bounds of the content of the display object. When you set the width property, the scaleX property is adjusted accordingly ... Except for TextField and Video objects, a display object with no content (such as an empty sprite) has a width of 0, even if you try to set width to a different value.
You'll need to render something as part of the sprite to get a valid w/h.
精彩评论