1
votes

If I have a custom class subclassing Sprite and it draws some simple objects, how does this work with respect to the Sprite.width & Sprite.height properties? It seems I can draw (for example) a rectangle of any size, bigger than the Sprite size. Similarly if I set the width/height properties, what happens to the content already drawn?

As a use-case, I might have a stick-man which is drawn as a set of lines, I want to set the man's height and the rendering is scaled to this.

Are there any issues with width/height being auto-calculated or am I misunderstanding what these properties actually mean?

2

2 Answers

3
votes

width and height make much more sense as getters. They take the internal content of the clip, multiply it by scaleX/scaleY and return it.

Internally the clip's scaling is stored as a matrix, and you can access individual elements of the matrix with the scaleX / scaleY properties (as well as x, y, and rotation).

You can also set width and height and there's some slightly awkward code that runs behind the scenes to make it work. Basically, whatever width you set is immediately turned into a scaleX value. For example if you have a 100x100 movieclip, and call width = 200, Flash will translate that to scaleX = 2.

If then, in the course of its animation, the movieclip grows to 120x100, then it will appear on the stage with a width of 240.

In other words the width and height that you set are applied as scaling factors. It has nothing to do with clipping the content.

For maximum predictability, always set to the scaleX / scaleY values.

1
votes

I think you are bit confused. Whatever is drawn in the Sprite will be resize too when you change the width, height, scaleX or scaleY.

There is an important concept of nesting of DisplayObjectContainers:

Let's say you have a parent Sprite that contains child Sprite that has a stick-man drawing 100 pixels high and wide. The width, height of both parent and child are 100 (while scaleX and scaleY are 1). Now if you set the width of parent to 50 px, the drawing on stage will be visually distorted, it will look like 50 px wide, the width of parent will be 50 px but width of the nested child will be still 100 px. Similarly the scaleX of parent will be 0.5 while scaleX of child will stay 1. Makes sense?