Simplebutton image is larger than it actually is - actionscript-3

I'm creating a button I use beginBitmapFill method to add image to it. Everything works normally, the problem is that the image loaded by a Loader () is greater than it actually is
Class that creates the button
package mode {
import flash.display.Sprite;
import org.osmf.net.StreamingURLResource;
public class LoadButton extends Sprite
{
public var Save;
public function LoadButton(x:uint,save:String,url:String)
{
var button:CustomSimpleButton = new CustomSimpleButton(url);
button.x = x;
Save = save;
addChild(button);
}
}
}
import flash.display.*;
import flash.display.Bitmap;
import flash.display.DisplayObject;
import flash.display.Shape;
import flash.display.SimpleButton;
import flash.events.*;
import flash.events.EventDispatcher;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.geom.Matrix;
class CustomSimpleButton extends SimpleButton
{
private var upColor:uint = 0xFFCC00;
private var overColor:uint = 0xCCFF00;
private var downColor:uint = 0x00CCFF;
private var sizew:uint = 100;
private var sizeh:uint = 88;
public function CustomSimpleButton(url:String)
{
downState = new ButtonDisplayState(downColor, sizew,sizeh,url);
overState = new ButtonDisplayState(overColor, sizew,sizeh,url);
upState = new ButtonDisplayState(upColor, sizew,sizeh,url);
hitTestState = new ButtonDisplayState(upColor, sizew * 2,sizeh,url);
hitTestState.x = -(sizew / 4);
hitTestState.y = hitTestState.x;
useHandCursor = true;
}
}
class ButtonDisplayState extends Shape
{
private var bgColor:uint;
private var size:uint;
private var sizeh:uint;
public function ButtonDisplayState(bgColor:uint, sizew:uint,sizeh:uint,url:String)
{
this.bgColor = bgColor;
this.size = sizew;
this.sizeh = sizeh;
draw(url);
}
private function draw(url:String):void
{
var myLoader:Loader = new Loader();
var image:Bitmap;
var uri = new URLRequest(url);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event)
{
image = new Bitmap(e.target.content.bitmapData);
graphics.beginBitmapFill(image.bitmapData);
graphics.drawRect(0, 0, 100, 88);
graphics.endFill();
});
myLoader.load(uri);
}
}
How the image is 100x88
as it is

The reason it is showing up cropped like that, is because the BitmapData is larger than the area you are filling.
Before filling the shape with the loaded BitmapData, you need to sale it down to the appropriate size.
Based on the answer to this question, you could do the following:
var scaleAmount:Number;
if(e.target.content.width >= e.target.content.height){
scaleAmount = size / e.target.content.width;
}else{
scaleAmount = sizeh / e.target.content.height;
}
var scaledBitmapData:BitmapData = scaleBitmapData(e.target.content.bitmapData, scaleAmount);
graphics.beginBitmapFill(scaledBitmapData);
graphics.drawRect(0, 0, size, sizeh);
graphics.endFill();
function scaleBitmapData(bitmapData:BitmapData, scale:Number):BitmapData {
scale = Math.abs(scale);
var width:int = (bitmapData.width * scale) || 1;
var height:int = (bitmapData.height * scale) || 1;
var transparent:Boolean = bitmapData.transparent;
var result:BitmapData = new BitmapData(width, height, transparent);
var matrix:Matrix = new Matrix();
matrix.scale(scale, scale);
result.draw(bitmapData, matrix);
return result;
}
It might be easier though, to just make your button state a Sprite and add the loaded Bitmap object and scale it directly:
class ButtonDisplayState extends Sprite
{
private var bgColor:uint;
private var image:Bitmap;
private var size:uint;
private var sizeh:uint;
public function ButtonDisplayState(bgColor:uint, sizew:uint,sizeh:uint,url:String)
{
this.bgColor = bgColor;
this.size = sizew;
this.sizeh = sizeh;
loadImage(url);
}
private function loadImage(url:String):void
{
var myLoader:Loader = new Loader();
var uri = new URLRequest(url);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event)
{
image = new Bitmap(e.target.content.bitmapData);
//scale the bitmap while retaining aspect ratio
//scale based off which dimension (width or height) is bigger
if(image.width >= image.height){
image.scaleX= size / image.width; //scale it to the width specified
image.scaleY = image.scaleX; //make the height aspect ratio match the widths
}else{
image.scaleY = sizeh /image.height;
image.scaleX = image.scaleY;
}
addChild(image); //add it to the display list of this object
});
myLoader.load(uri);
}
}

Related

Stage dimensions in a new NativeWindow

i'm building a flash desktop App where the user clicks on a button and opens a SWF file (a game) in a new window, i used a NativeWindow to achieve it, i did the folowing:
var windowOptions:NativeWindowInitOptions = new NativeWindowInitOptions();
windowOptions.systemChrome = NativeWindowSystemChrome.STANDARD;
windowOptions.type = NativeWindowType.NORMAL;
var newWindow:NativeWindow = new NativeWindow(windowOptions);
newWindow.stage.scaleMode = StageScaleMode.NO_SCALE;
newWindow.stage.align = StageAlign.TOP_LEFT;
newWindow.bounds = new Rectangle(100, 100, 2000, 800);
newWindow.activate();
var aLoader:Loader = new Loader;
aLoader.load(new URLRequest(path));
newWindow.stage.addChild(aLoader);
the result is this:
the game doesn't take the whole space available. When i change the "NO_SCALE" to "EXACT_FIT":
newWindow.stage.scaleMode = StageScaleMode.EXACT_FIT;
i got this:
it only shows the top-left corner of the game. I also tried "SHOW_ALL" and "NO_BORDER". I got the same result as "EXACT_FIT".
When i open the SWF file of the game separatly it's displayed normally:
any idea how can i display the SWF game as the image above?
The best solution for that is to have the dimensions of your external game in pixel. you have to stretch the loader to fit it in your stage. if you are try to load different games with different sizes to your stage, save the swf dimensions with their URL addresses.
var extSWFW:Number = 550;
var extSWFH:Number = 400;
var extURL:String = "./Data/someSwf.swf";
var mainStageWidth:Number = 1024;
var mainStageHeight:Nubmer = 768;
var aLoader:Loader = new Loader;
aLoader.load(new URLRequest(extURL));
newWindow.stage.addChild(aLoader);
aLoader.scaleY = aLoader.scaleX = Math.min(mainStageWidth/extSWFW,mainStageHeight/extSWFH);
It is not very difficult to figure dimensions of a loaded SWF because it is engraved into it's header, you can read and parse it upon loading. Here's the working sample:
package assortie
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.net.URLRequest;
import flash.utils.ByteArray;
public class Dimensions extends Sprite
{
private var loader:Loader;
public function Dimensions()
{
super();
loader = new Loader;
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
loader.load(new URLRequest("outer.swf"));
}
private function onLoaded(e:Event):void
{
var aBytes:ByteArray = loader.contentLoaderInfo.bytes;
var aRect:Rectangle = new Rectangle;
var aRead:BitReader = new BitReader(aBytes, 8);
var perEntry:uint = aRead.readUnsigned(5);
aRect.left = aRead.readSigned(perEntry) / 20;
aRect.right = aRead.readSigned(perEntry) / 20;
aRect.top = aRead.readSigned(perEntry) / 20;
aRect.bottom = aRead.readSigned(perEntry) / 20;
aRead.dispose();
aRead = null;
trace(aRect);
}
}
}
import flash.utils.ByteArray;
internal class BitReader
{
private var bytes:ByteArray;
private var position:int;
private var bits:String;
public function BitReader(source:ByteArray, start:int)
{
bits = "";
bytes = source;
position = start;
}
public function readSigned(length:int):int
{
var aSign:int = (readBits(1) == "1")? -1: 1;
return aSign * int(readUnsigned(length - 1));
}
public function readUnsigned(length:int):uint
{
return parseInt(readBits(length), 2);
}
private function readBits(length:int):String
{
while (length > bits.length) readAhead();
var result:String = bits.substr(0, length);
bits = bits.substr(length);
return result;
}
static private const Z:String = "00000000";
private function readAhead():void
{
var wasBits:String = bits;
var aByte:String = bytes[position].toString(2);
bits += Z.substr(aByte.length) + aByte;
position++;
}
public function dispose():void
{
bits = null;
bytes = null;
}
}

Change the color of object and link the choosen color to the next scene not all the color button

Here is my coding using AS3:
import flash.events.MouseEvent;
import flash.display.MovieClip;
var btts:Array = [red,yellow,blue];
var set_colors:Object = {'red':0xff0000,'yellow':0xffff00,'blue':0x0000ff};
var obj_color:ColorTransform = new ColorTransform();
for(var i:int=0; i<btts.length; i++) {
obj_color.color = set_colors[btts[i].name];
btts[i].transform.colorTransform = obj_color;
btts[i].addEventListener(MouseEvent.CLICK, changeColor);
}
function changeColor(evt:Event):void
{
var b_name = evt.target.name;
obj_color.color = set_colors[b_name];
rec.transform.colorTransform = obj_color;
}
go.addEventListener(MouseEvent.CLICK, fl_ClickToGoToScene_6);
function fl_ClickToGoToScene_6(event:MouseEvent):void
{
if(obj_color.color == set_colors[red])
{
gotoAndPlay(1, "Scene 2");
}
}
stop();
Hopefully someone can help me to repair this code.This code shown that when i click all the color button it will go to Scene 2 but in my condition,I want only one choosen button will go to Scene 2.
This may help you to deal with the background color and Stage.Resize, but you will need to adapt the code for the Color change...
package com
{
import flash.display.Graphics;
import flash.display.MovieClip;
import flash.display.Stage;
import flash.display.StageAlign;
import flash.display.StageQuality;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.text.TextField;
public class Main extends MovieClip
{
private static const ORANGE:int = 0xff9900;
var bg:MovieClip;
var bg_mask:MovieClip;
var marginx:int = 10;
var marginy:int = 10;
var display:MovieClip;
var displayTextField:TextField;
var ellipseWidth:int = 90;
var ellipseHeight:int = 90;
public function Main()
{
super();
bg = new MovieClip();
bg_mask = new MovieClip();
display = new MovieClip();
displayTextField = new TextField();
this.addChild(bg);
this.addChild(bg_mask);
this.addChild(display);
this.display.addChild(displayTextField);
displayTextField.text = "";
displayTextField.x = ellipseWidth/Math.PI;
displayTextField.y = ellipseHeight/Math.PI;
stage.align = StageAlign.TOP_LEFT; // or StageAlign.TOP
stage.scaleMode = StageScaleMode.NO_SCALE;
drawBackground();
display.mask = bg_mask;
addListeners();
}
private function drawBackground(thickness:int=1,lineColor:int=0x000000,lineAlpha:Number=0.5,fillColor:int=ORANGE,fillAlpha:Number=0.5):void{
var g:Graphics = bg.graphics;
g.clear();
g.lineStyle(thickness,lineColor,lineAlpha);
g.beginFill(fillColor,fillAlpha);
g.drawRoundRect(marginx,marginy,this.stage.stageWidth-marginx*2,this.stage.stageHeight-marginy*2,ellipseWidth,ellipseHeight);
g.endFill();
}
private function drawMask():void{
var g:Graphics = bg_mask.graphics;
g.clear();
g.lineStyle(1,0x000000,0.3);
g.beginFill(0xcccccc,0.1);
g.drawRoundRect(marginx,marginy,this.stage.stageWidth-marginx*2,this.stage.stageHeight-marginy*2,90,90);
g.endFill();
}
private function addListeners():void{
stage.addEventListener(Event.ADDED,updateLabel);
stage.addEventListener(Event.RESIZE,updateLabel);
}
private function updateLabel(e:Event):void{
updateDisplay();
drawBackground();
drawMask();
}
private function updateDisplay():void{
var tf:TextField = displayTextField;
tf.text = ("{" + this.stage.stageWidth + ";" + this.stage.stageHeight + "}");
}
}
}
Just add a function to change the Color, but this is a simple way to do this...
In the method drawBackground, just add the Color that You want...
Just edit the fillColor in drawBackground method...
This will be the same as on http://tatactic.be/stackOverflow/ChangeBG/StageResizeSimple.swf
But You can easiely change the Color too.
[EDIT]
here You may add a function thath will change Your BG color
So this should be a piece of cake... ;)
[/EDIT]
Best regards.
Nicolas.

Duplicating video Stream Actionscript 3

Good Morning,
i am working on a video class, using a CRTMP Server for streaming. This works fine, but for my solution i need to duplicate the video stream (for some effects).
I googled for duplicate MovieClips and tried to duplicate the video like this.
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.media.*;
import flash.system.*;
import flash.utils.ByteArray;
public class Main extends MovieClip
{
public var netStreamObj:NetStream;
public var nc:NetConnection;
public var vid:Video;
public var vid2:Video;
public var streamID:String;
public var videoURL:String;
public var metaListener:Object;
public function Main()
{
init_RTMP();
}
private function init_RTMP():void
{
streamID = "szene3.f4v";
videoURL = "rtmp://213.136.73.230/maya";
vid = new Video(); //typo! was "vid = new video();"
vid2 = new Video();
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, onConnectionStatus);
nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
nc.client = {onBWDone: function():void
{
}};
nc.connect(videoURL);
}
private function onConnectionStatus(e:NetStatusEvent):void
{
if (e.info.code == "NetConnection.Connect.Success")
{
trace("Creating NetStream");
netStreamObj = new NetStream(nc);
metaListener = new Object();
metaListener.onMetaData = received_Meta;
netStreamObj.client = metaListener;
netStreamObj.play(streamID);
vid.attachNetStream(netStreamObj);
//vid2.attachNetStream(netStreamObj); // wont work
addChild(vid);
// addChild(vid2); // wont work either
//intervalID = setInterval(playback, 1000);
}
}
private function asyncErrorHandler(event:AsyncErrorEvent):void
{
trace("asyncErrorHandler.." + "\r");
}
private function received_Meta(data:Object):void
{
var _stageW:int = stage.stageWidth;
var _stageH:int = stage.stageHeight;
var _videoW:int;
var _videoH:int;
var _aspectH:int;
var Aspect_num:Number; //should be an "int" but that gives blank picture with sound
Aspect_num = data.width / data.height;
//Aspect ratio calculated here..
_videoW = _stageW;
_videoH = _videoW / Aspect_num;
_aspectH = (_stageH - _videoH) / 2;
vid.x = 0;
vid.y = _aspectH;
vid.width = _videoW;
vid.height = _videoH;
vid2.x = 0;
vid2.y = _aspectH ;
}
}
It should be possible to duplicate the video stream. 2 Instance of the same videoStream. What am i doing wrong ?
Thanks for help.
"This means that i have to double the netstream. This is not what i want."
"I tried to duplicate the video per Bitmap.clone. But i got an sandbox violation."
You can try the workaround suggested here: Netstream Play(null) Bitmapdata Workaround
I'll show a quick demo of how it can be applied to your code.
This demo code assumes your canvas is width=550 & height=400. Keep that ratio if scaling up.
package
{
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.media.*;
import flash.system.*;
import flash.utils.ByteArray;
import flash.geom.*;
import flash.filters.ColorMatrixFilter;
public class Main extends MovieClip
{
public var netStreamObj:NetStream;
public var nc:NetConnection;
public var vid:Video;
public var streamID:String;
public var videoURL:String;
public var metaListener:Object;
public var vid1_sprite : Sprite = new Sprite();
public var vid2_sprite : Sprite = new Sprite();
public var vid2_BMP : Bitmap;
public var vid2_BMD : BitmapData;
public var colMtx:Array = new Array(); //for ColorMatrix effects
public var CMfilter:ColorMatrixFilter;
public function Main()
{
init_RTMP();
}
private function init_RTMP():void
{
streamID = "szene3.f4v";
videoURL = "rtmp://213.136.73.230/maya";
vid = new Video();
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, onConnectionStatus);
nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
nc.client = { onBWDone: function():void { } };
//nc.connect(null); //for file playback
nc.connect(videoURL); //for RTMP streams
}
private function onConnectionStatus(e:NetStatusEvent):void
{
if (e.info.code == "NetConnection.Connect.Success")
{
trace("Creating NetStream");
netStreamObj = new NetStream(nc);
metaListener = new Object();
metaListener.onMetaData = received_Meta;
netStreamObj.client = metaListener;
//netStreamObj.play("vid.flv"); //if File
netStreamObj.play(streamID); //if RTMP
vid.attachNetStream(netStreamObj);
}
}
private function asyncErrorHandler(event:AsyncErrorEvent):void
{
trace("asyncErrorHandler.." + "\r");
}
private function received_Meta(data:Object):void
{
trace("Getting metadata");
var _stageW:int = stage.stageWidth;
var _stageH:int = stage.stageHeight;
var _videoW:int = data.width;
var _videoH:int = data.height;
var _aspectH:int = 0;
var Aspect_num:Number;
Aspect_num = data.width / data.height;
//Aspect ratio calculated here..
_videoW = _stageW;
_videoH = _videoW / Aspect_num;
_aspectH = (_stageH - _videoH) / 2;
trace("_videoW : " + _videoW);
trace("_videoW : " + _videoH);
trace("_aspectH : " + _aspectH);
vid.x = 0;
vid.y = 0;
vid.width = _videoW;
vid.height = _videoH;
setup_Copy(); //# Do this after video resize
}
public function setup_Copy () : void
{
vid2_BMD = new BitmapData(vid.width, vid.height, false, 0);
vid2_BMP = new Bitmap( vid2_BMD );
vid1_sprite.addChild(vid);
vid1_sprite.x = 0;
vid1_sprite.y = 0;
addChild( vid1_sprite );
vid2_sprite.addChild( vid2_BMP );
vid2_sprite.x = 0;
vid2_sprite.y = vid.height + 5;
addChild( vid2_sprite );
stage.addEventListener(Event.ENTER_FRAME, draw_Video);
}
public function draw_Video (evt:Event) : void
{
if ( netStreamObj.client.decodedFrames == netStreamObj.decodedFrames ) { return; } // Here we skip multiple readings
//# Get bitmapdata directly from container of video
if ( vid1_sprite.graphics.readGraphicsData().length > 0 )
{
vid2_BMD = GraphicsBitmapFill(vid1_sprite.graphics.readGraphicsData()[0]).bitmapData;
}
effect_BitmapData(); //# Do an effect to bitmapdata
}
public function effect_BitmapData ( ) : void
{
//# Matrix for Black & White effect
colMtx = colMtx.concat([1/3, 1/3, 1/3, 0, 0]); // red
colMtx = colMtx.concat([1/3, 1/3, 1/3, 0, 0]); // green
colMtx = colMtx.concat([1/3, 1/3, 1/3, 0, 0]); // blue
colMtx = colMtx.concat([0, 0, 0, 1, 0]); // alpha
CMfilter = new ColorMatrixFilter(colMtx);
vid2_BMP.bitmapData.applyFilter(vid2_BMD, new Rectangle(0, 0, vid.width, vid.height), new Point(0, 0), CMfilter);
}
}
}

Do you know if i can integrate with as3isolib some physic engine like nape or box2d?

i just searching for any resource that explain or teach how integrate if it possible as3isolib with some physics engine like, nape or box2d. It is possible?
Sure you can do that.
For example Box2D Body has member userData, which type is *, so you can assign any data there.
As far as Box2D and nape are 2D physics engines, you can make 2d phys modifications on x and y axis.
Here is an example:
package
{
import as3isolib.display.primitive.IsoBox;
import as3isolib.display.scene.IsoScene;
import Box2D.Dynamics.Contacts.*;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
import Box2D.Dynamics.Joints.*;
import flash.events.Event;
import flash.display.Sprite;
public class Main extends Sprite
{
private var scene:IsoScene;
private var world:b2World;
private var gravity:b2Vec2;
private var body:b2Body;
private const worldScale:int = 30;
private const doSleep:Boolean = true;
private static const m_iterations:int = 10;
private static const m_timeStep:Number = 1.0/30.0;
public function Main ()
{
scene = new IsoScene();
scene.hostContainer = this;
var bodyDef:b2BodyDef;
var fixtureDef:b2FixtureDef = new b2FixtureDef();
gravity = new b2Vec2(0.0, 4.0);
world = new b2World(gravity, doSleep);
bodyDef = new b2BodyDef();
bodyDef.type=b2Body.b2_dynamicBody;
bodyDef.allowSleep = false;
bodyDef.linearDamping = 1;
bodyDef.angularDamping = 1;
bodyDef.position.x = 200/worldScale; //position
bodyDef.position.y = 100/worldScale; //position
bodyDef.userData = new IsoBox();
bodyDef.userData.setSize(25, 25, 25);
bodyDef.fixedRotation = true;
fixtureDef.restitution = 0.5;
fixtureDef.shape = new b2CircleShape(0.3);
fixtureDef.density = 1;
fixtureDef.friction = 5;
fixtureDef.restitution = 0.5;
scene.addChild(bodyDef.userData);
body = world.CreateBody(bodyDef);
body.CreateFixture(fixtureDef);
addEventListener(Event.ENTER_FRAME, Update);
}
private function Update(e:Event):void
{
world.Step(m_timeStep, m_iterations, m_iterations);
world.ClearForces();
body.GetUserData().moveTo(body.GetPosition().x * worldScale, body.GetPosition().y * worldScale, 0);
scene.render();
}
}
}

A problem with DrawRoundRect size

I'm trying to create a custom button with ActionScript 3.0. I'm suing a round rect as background, but I have a problem with it size.
This is my custom button class:
package
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.display.Shape;
public class customButton extends Sprite
{
private var background:Shape;
public var bgColor:uint;
public var borderColor:uint;
public var borderSize:uint;
public var cornerRadius:uint;
private var label:TextField;
public function customButton(text:String)
{
super();
this.opaqueBackground = 0xFF0000;
background = new Shape();
borderSize = 1;
borderColor = 0x666666;
bgColor = 0xFFCC00;
cornerRadius = 9;
label = new TextField();
label.text = text;
var format:TextFormat = new TextFormat();
format.font = "Verdana";
format.color = 0;
format.size = 38;
format.underline = true;
label.defaultTextFormat = format;
addChild(background);
addChild(label);
buttonMode = true;
mouseChildren = false;
}
public function draw():void
{
background.graphics.lineStyle(borderSize, borderColor);
background.graphics.beginFill(bgColor);
background.graphics.drawRoundRect(0, 0, this.width, this.height cornerRadius);
background.graphics.endFill();
}
}
}
And this is the code used to show the button:
public function Test01()
{
super();
// support autoOrients
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
button = new customButton("Button");
button.x = 200;
button.y = 300;
button.width = 200;
button.height = 100;
button.draw();
addChild(button);
}
If I set this size to the button:
button.width = 200;
button.height = 100;
I get the following:
But I set it to button size:
button.width = 40;
button.height = 20;
(This size is the same used in customButton class). I get:
I don't know why when I use a size of (40, 20) I get a smaller rectangle than that size.
Any advice?
it is happening because you are setting the width directly to the Sprite, it changes the size of the sprite no the size of the background you are drawing.
in your customButton class add some code:
private var _width:Number = 10;
private var _height:Number = 10;
override public function get width():Number { return _width; }
override public function set width(value:Number):void
{
_width = value;
draw ();
}
override public function get height():Number { return _height; }
override public function set height(value:Number):void
{
_height = value;
draw ();
}
private function draw():void
{
background.graphics.clear ()
background.graphics.lineStyle(borderSize, borderColor);
background.graphics.beginFill(bgColor);
background.graphics.drawRoundRect(0, 0, _width, _height, cornerRadius);
background.graphics.endFill();
}
with this code you will be able to chnage the background size everytime, and you will not be affecting the other components.
Your roundRect class size has been fixed, so that it was making no changes when u r increasing the width.
Make two public parameters for both width(Bwidth) and height(Bheight) and access it.
like
button.Bwidth = 100;
package
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.display.Shape;
public class customButton extends Sprite
{
private var background:Shape;
public var bgColor:uint;
public var borderColor:uint;
public var borderSize:uint;
public var cornerRadius:uint;
public var bWidth:Number;
public var bHeight:Number;
private var label:TextField;
public function customButton(text:String, bW:Number, bH:Number)
{
super();
this.bWidth = bW;
this.bHeight = bH;
background = new Shape();
borderSize = 1;
borderColor = 0x666666;
bgColor = 0xFFCC00;
cornerRadius = 9;
label = new TextField();
label.text = text;
var format:TextFormat = new TextFormat();
format.font = "Verdana";
format.color = 0;
format.size = 38;
format.underline = true;
label.defaultTextFormat = format;
addChild(background);
addChild(label);
buttonMode = true;
mouseChildren = false;
background.graphics.lineStyle(borderSize, borderColor);
background.graphics.beginFill(bgColor);
background.graphics.drawRoundRect(0, 0, bWidth, bHeight, cornerRadius);
background.graphics.endFill();
}
}
}
TRy this