Forcing size of a complex Flash object
As I've found recently, setting width
/height
properties on a Sprite
only forces the Sprite
to fit the given dimensions by scaling the actual size, which is开发者_StackOverflow中文版 calculated by Flash based on the rendered content.
This leaves me confused. If I have a custom Sprite
subclass which draws using Graphics
, how can I do layout before an initial render - the size will be zero until it is first drawn?
For a more complex issue, let's say I have a 2D game world with objects spread over a wide area with world coordinates from (0,0) to (5000,5000), where each object has a size of maybe up to 100x100. I want to have a Flash component which is the "game window", and has a fixed size like 400x300, rendering part of the game world. So how do I force the game window size to 400x300 pixels? I can draw a 400x300 rectangle to get the size correct but then if I draw any objects which are partly in-view, they can screw this up.
Is the right approach to provide a custom setSize(w,h)
method which is used rather than width
& height
setters? But even so, is there no way to make a Sprite
force to the size I want? Do you really have to catch it every render and re-scale it?
If I understand correctly, your game world is a child object of your window, and its size is always 5000x5000, but you need width
and height
of the window to refer only to the window itself? Use a mask, as the other comment suggests, then override the width
and height
getters and setters:
private var _width: Number = 400;
private var _height : Number = 300;
private var _mask : Sprite;
// some code in between
override public function get width () : Number {
return _width;
}
override public function get height () : Number {
return _height;
}
override public function set width ( wid:Number ) : void {
_width = wid;
if(_mask != null) _mask.width = wid;
}
override public function set height ( hei : Number ) : void {
_height = hei;
if (_mask != null) _mask.height = hei;
}
The best way to do it in my opinion is to use a mask. You have one game container where all game DisplayObjects are added to, and on top of that container you'd have a nice user interface around the game container with a mask in the center. That way the game container will only be drawn within the boundary of the mask.
Another option to force a size is to create new Sprites inside the thing you want to have width/height, placing them at the four corners. It is simple to do this with code, or via the authoring environment.
精彩评论