开发者

AS3: Resizing a sprite without resizing its BitmapFill (or its contents for that matter)?

Edit #2 I've found the solution, it was more simple than I could have hoped. Thanks for your time. =) (Answered my own question with full solution)

Edit: My apologies that my post was not transparent enough.. I will try to iterate for a little more clarity. I am not simply trying to contain things and have the container resize without the contents resizing. I would just use a simple MovieClip for that.. My main focus is on the use of BitmapFill. I'm trying to make a repeating background image for a container; thus far I'm able to draw my BitmapFill sprite to the size I want. (Which is the stage's height) but when the stage is resized, I want to re-size my container but have开发者_运维知识库 my containers BitmapFill crop or add more as the window is resized bigger or smaller. Allow me to illustrate what I mean:

AS3: Resizing a sprite without resizing its BitmapFill (or its contents for that matter)?

I hope this clears up any confusion and helps with any straighter answers.

------- Original post:

I've got a simple rectangle sprite that fills with a bitmap from the library and a simple resize handler function that detects when the window has been resized.

What I would like to do is have my rectangle resize to the height of the stage.stageHeight maintaining its bitmap fill, but not resizing the bitmap fill or any children within the sprite.

From what I've read, this is a little tricky because the sprite resizes in relation to what it contains.. or.. something like that.. I really don't know any more.. I saw one example where a person extended the sprite class but I don't know why or how..

in my library there is an image called 'pattern' and my code looks like this:

stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;

stage.addEventListener(Event.RESIZE, resizeHandler, false, 0, true);

var bgData:pattern = new pattern(0,0);
var bg:Sprite = new Sprite();

bg.graphics.beginBitmapFill(bgData, null, true, false);
bg.graphics.drawRect( 0, 0, 80, stage.stageHeight);
bg.graphics.endFill();
addChild(bg);

function resizeHandler(e:Event=null):void 
{
 trace("dimensions are: " + stage.stageWidth + " x " + stage.stageHeight);
 bg.height = stage.stageHeight;  //simply makes the fill resize also
}


Oh, well I'm dull. The answer was quite simple.

All I needed to do was clear the graphics then redraw them..

I wasn't aware of the clear(); function..

the full revised code is as follows:

stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;

stage.addEventListener(Event.RESIZE, resizeHandler, false, 0, true);

var bgData:pattern = new pattern(0,0);
var bg:Sprite = new Sprite();

bg.graphics.beginBitmapFill(bgData, null, true, false);
bg.graphics.drawRect( 0, 0, 80, stage.stageHeight);
bg.graphics.endFill();
addChild(bg);

function resizeHandler(e:Event=null):void 
{
 trace("dimensions are: " + stage.stageWidth + " x " + stage.stageHeight);
 bg.graphics.clear();

 bg.graphics.beginBitmapFill(bgData, null, true, false);
 bg.graphics.drawRect( 0, 0, 80, stage.stageHeight);

 bg.graphics.endFill();
}

thanks all, sorry for this potential waste of space.


Since a Sprite will resize to its content, you can use a Shape to resize your Sprite. Changing the Shape size will effectively resize the Sprite without resizing the Sprite's other children.


var bg:Sprite = new Sprite();
addChild( bg );

var fill:Shape = new Shape();
//you can make the fill transparent
with( fill )
{
   graphics.beginFill( 0 , 0 );
   graphics.drawRect( 0 , 0 , anyWidth  , anhyHeight );
   graphics.endFill();
}
bg.addChild(fill);

//then add the fill that you want visible
var bgFill:Sprite = new Sprite();
//add the graphics here
bg.addChild(bgFill );


function resizeSprite(event:Event ):void
{
    // bgFill will not be resized
    fill.width = stage.stageWidth;
    fill.height = stage.stageHeight;
}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜