How to add a button in as - actionscript-3

hi I am just starting to learn flex and action script. can someone tell me I doing wrong in this code.
public function createBoxes():void
{
//create a Panel
var colorsPanel:Panel = new Panel(); colorsPanel.layout = "absolute"; colorsPanel.width = 250; colorsPanel.height = 250;
//add the Panel to the Application
addElement(colorsPanel);
//create a red box
var redBox:Canvas = new Canvas(); redBox.x = 70; redBox.y = 70; redBox.width = 50; redBox.height = 50; redBox.setStyle("backgroundColor", 0xFF0000);
//create a green box
var greenBox:Canvas = new Canvas(); greenBox.x = 90; greenBox.y = 90; greenBox.width = 50; greenBox.height = 50; greenBox.setStyle("backgroundColor", 0x00FF00);
//create a blue box
var blueBox:Canvas = new Canvas(); blueBox.x = 100; blueBox.y = 60; blueBox.width = 50; blueBox.height = 50; blueBox.setStyle("backgroundColor", 0x0000FF);
//add the boxes to the Panel
var Button:spark.components.Button = new spark.components.Button(); Button.x = 120; Button.y = 60; Button.label ="removeG";
colorsPanel.addElement(redBox);
colorsPanel.addElement(greenBox);
colorsPanel.addElement(blueBox);
colorsPanel.addElement(Button);
}
thanks

I'm going to take a stab at it here...
Are you using a Spark panel, or a Halo panel?
The way you can tell is by looking at your import statements.
If you see the line:
import spark.components.Panel;
Then you're using a Spark Panel. In this case, you cannot set the layout property using a string. Instead you have to use an instance of a class that extends LayoutBase, such as BasicLayout. If, however, you see this line instead:
import mx.containers.Panel;
Then you're using a Halo Panel and I'm not sure what the problem is - you'll need to better describe the difference between what you expect to happen and what is actually happening. If you're getting some kind of error, post the text of the error.

Related

AS3 Why does editing a Textfield inside of a MovieClip throw all the sizes off?

I'm pretty frustrated with AS3 right now. It seems like there must be serious issues with how things scale inside of movieclips. From the stage everything seems to work fine.
On one of my projects I try to set a textfield.width equal to its container(movieclip)
tf.width = mc.width; Of course it says everything is even but when I look at the border of the textfield it's nowhere near the size of the movie clip it's contained within.
While trying to make a much smaller sample to share with you guys for help, that part worked, but trying to resize text did something completely different. Can anyone help me make sense of all this? The code below seemingly randomly starts changing the size of everything.
Also, I was trying to follow this code from here but just kept getting an error when trying to change the format size in a while loop: Autosize text to fit the width of a button
var square:MovieClip = new MovieClip();
addChild(square);
square.graphics.lineStyle(3,0x00ff00);
square.graphics.beginFill(0x0000FF);
square.graphics.drawRect(0,0,100,100);
square.graphics.endFill();
square.x = 0;
square.y = 0;
square.width = 200;
square.height = 200;
var tffSize = 400;
var tffOr:TextFormat = new TextFormat();
tffOr.size = tffSize;
tffOr.align = TextFormatAlign.CENTER;
var tf:TextField = new TextField();
square.addChild(tf);
tf.defaultTextFormat = tffOr;
tf.text = "Hello";
tf.border = true;
tf.width = square.width;
tf.height = square.height;
trace(tf.textWidth);
trace(square.width);
while (tf.textWidth > square.width || tf.textHeight > square.height)
{
trace("too Big");
newTFF();
trace(tf.textWidth + " vs " + square.width);
square.width = 200;
trace(tf.textWidth + " vs " + square.width);
}
function newTFF()
{
tffSize = tffSize - 1;
var tff:TextFormat = new TextFormat();
tff.size = tffSize;
tff.align = TextFormatAlign.CENTER;
//tf.defaultTextFormat = tff;
tf.setTextFormat(tff);
tf.autoSize = "left";
}

AS3 removing dynamically created child movieclips

I'm fairly new to AS3. Anyways, I'm try to remove a dynamically created child movieclip when clicked on. When a dirt block is clicked on, which is a child movieclip of 'world' I want to remove it.
I've tried various ways of removing it using removeChild. I've also tried moving the function inside/outside of the for loop that creates the movieclips.
var blockCount:Number = 0;
var blockArray:Array = [];
var world:MovieClip = new World();
world.x = 50;
world.y = 50;
world.name = "world";
addChild(world);
for(var i:Number=1;i<=100;i++){
blockCount++;
var tempGrassBlock:MovieClip = new GrassBlock();
tempGrassBlock.x = i*16;
tempGrassBlock.y = 256;
tempGrassBlock.name = "b"+blockCount;
world.addChild(tempGrassBlock);
tempGrassBlock.addEventListener(MouseEvent.CLICK, removeBlock);
function removeBlock(event:Event){
world.removeChild(getChildByName(event.target.name));
}
}
Thanks for the help.
Try this
function removeBlock(event:Event){
world.removeChild(event.currentTarget as DisplayObject);
}
No function definition should be inside a for. I changed that in your code and rewrited a little below:
var blockCount:Number = 0;
var blockArray:Array = [];
var world:MovieClip = new World();
world.x = 50;
world.y = 50;
world.name = "world";
addChild(world);
for(var i:Number=1;i<=100;i++){
blockCount++;
var tempGrassBlock:MovieClip = new GrassBlock();
tempGrassBlock.x = i*16;
tempGrassBlock.y = 256;
tempGrassBlock.name = "b"+blockCount;
world.addChild(tempGrassBlock);
tempGrassBlock.addEventListener(MouseEvent.CLICK, removeBlock);
}
function removeBlock(event:MouseEvent){
trace("Is click really working? This target name is " + event.currentTarget.name);
world.removeChild(event.currentTarget));
}

AS3 - Adjust image colors

I'm adjusting image colors through the function below. The problem is that if I need to switch a colorFilter value to 0 it's not working but if I enter 0.1 instead of 0 it works.
How to make it work without that workaround?
import fl.motion.AdjustColor;
import flash.filters.ColorMatrixFilter;
var colorFilter:AdjustColor = new AdjustColor();
var mColorMatrix:ColorMatrixFilter;
var mMatrix:Array = [];
var MC:MovieClip = new MovieClip();
function adjustColors():void
{
colorFilter.hue = 50;
colorFilter.saturation = 50;
colorFilter.brightness = 50;
colorFilter.contrast = 12;
mMatrix = colorFilter.CalculateFinalFlatArray();
mColorMatrix = new ColorMatrixFilter(mMatrix);
MC.filters = [mColorMatrix];
}
I tested this by adding an argument to adjustColors() and calling it twice, and I see the same problem. I think it's just a bug where it ignores zero values.
It's not much of a better workaround, but if you just create a new AdjustColor each time, it should work correctly:
import fl.motion.AdjustColor;
import flash.filters.ColorMatrixFilter;
var colorFilter:AdjustColor = new AdjustColor();
var mColorMatrix:ColorMatrixFilter;
var mMatrix:Array = [];
var MC:MovieClip = new MovieClip();
function adjustColors():void
{
colorFilter = new AdjustColor();
colorFilter.hue = 50;
colorFilter.saturation = 50;
colorFilter.brightness = 50;
colorFilter.contrast = 12;
mMatrix = colorFilter.CalculateFinalFlatArray();
mColorMatrix = new ColorMatrixFilter(mMatrix);
MC.filters = [mColorMatrix];
}
Here is my workaround for reference:
Just use the logical OR assignment when you set each property.
That way if a value is 0 it will evaluate to false and .1 will be assigned instead:
var colorMat:ColorMatrixFilter = new ColorMatrixFilter();
var colorAdjust:AdjustColor = new AdjustColor();
const colorsAdj:Array =
[
// BRIGHTNESS, CONTRAST, SATURATION, HUE
[-20,0,20,-50],
[0,0,0,0],
[0,0,0,17]
];
function setColorMat(colorID:int):void
{
colorAdjust.brightness = colorsAdj[colorID][0] ||= .1;
colorAdjust.contrast = colorsAdj[colorID][1] ||= .1;
colorAdjust.saturation = colorsAdj[colorID][2] ||= .1;
colorAdjust.hue = colorsAdj[colorID][3] ||= .1;
colorMat.matrix = colorAdjust.CalculateFinalFlatArray();
}
That way you avoid recreating a new ColorMatrixFilter each time, in case it really change something...
And you keep a nice clean array... ;-)

actionscrip3 textfield sizeing end centering

I am trying to get some words from xml and put them an the stage side by side in the center of the stage. I achieved this by the code below. I auto resize textfield according to text inside. But this time there comes space between words. What I accomplish is to have autoresized and adjacent words without space between them. But I could not solve the problem.
Could you please help me to solve it.
Thanks in advance
var partsWidth=100;
var wordTf = new TextField();
wordTf.name =thispart;
wordTf.text =thispart;
wordTf.width=partsWidth;
xStartPoint=stage.stageWidth / 2 - (numberOfWords * partsWidth )/2;
wordTf.height=partsHeight;
wordTf.x= xStartPoint + (index * partsWidth) ;
wordTf.y=150;
wordTf.background=true;
wordTf.backgroundColor = 0xe3e3e3;
wordTf.border = true;
var myFormat:TextFormat = new TextFormat();
myFormat.size = 16;
myFormat.align = TextFormatAlign.CENTER;
wordTf.setTextFormat(myFormat);
wordTf.autoSize=TextFieldAutoSize.CENTER;
addChild(wordTf);
you are setting the width explicit with wordTf.width=partsWidth;. this will override the autosize option. I would use the following code.
var container:Sprite = new Sprite();
var myFormat:TextFormat = new TextFormat();
myFormat.size = 16;
myFormat.align = TextFormatAlign.CENTER;
for each( var thispart:String in parts )
{
var wordTf = new TextField();
wordTf.defaultTextFormat = myFormat;
wordTf.name = thispart;
wordTf.text = thispart;
wordTf.height=partsHeight;
wordTf.background=true;
wordTf.backgroundColor = 0xe3e3e3;
wordTf.border = true;
wordTf.width = wordTf.textWidth + 4;
wordTf.y=150;
wordTf.x = container.width;
container.addChild(wordTf);
}
container.x = (stage.stageWidth - container.width) / 2;
addChild(container);
add your words to a separate sprite, and after all words added, add this sprite to the stage and center it.
The line
wordTf.width = wordTf.textWidth + 4;
is the important one. After setting the text, flash can calculate the width of the text. now set this text width (+4 is a fixed padding around the text in a text field you can't modify) as width of your textfield.

as3 text-wrap problems, firefox, mac

So, I've built a little site for a client that doesn't seem to have any problems for me. I post it, and my client reports to me that some of the text is being clipped off in the text fields. I try to recreate the problem on my pc with chrome and firefox, but everything works fine. But he sends me screenshots from his mac with firefox, and sure enough they are being clipped. He claims its only fire-fox, and it is not a factor of window size. so here are the images to show whats going on:
Further most right words should be "Alabama" & "growing"
and here is the code that produces that text (descTextNew is the culprit here):
nameText.text = names[elementNumber];
nameText.autoSize = "center";
nameText.x = object.x + (.5 * videoWidth) - (.5 * nameText.width);
nameText.y = object.y + videoHeight + 8;
nameText.background = true;
nameText.backgroundColor = 0x000000;
textStyling.color = 0xFFFFFF;
textStyling.size = 20;
textStyling.font = myFont.fontName;
textStyling.letterSpacing = 3;
textStyling.align = "center";
nameText.embedFonts = true;
nameText.setTextFormat(textStyling);
container.addChild(nameText);
descTextNew.text = descArrayContent[elementNumber];
descTextNew.y = object.y + videoHeight + nameText.textHeight + margin - 15;
descTextNew.width = nameText.textWidth + 30;
descTextNew.x = object.x + ( (videoWidth - descTextNew.width) / 2 );
descTextNew.wordWrap = true;
descTextNew.autoSize = "left";
descTextNew.background = true;
descTextNew.backgroundColor = 0x000000;
descTextStyling.color = 0xFFFFFF;
descTextStyling.size = Math.round(11 * 1000/stage.stageWidth);
descTextStyling.font = myFont.fontName;
descTextStyling.letterSpacing = .5;
descTextStyling.align = "left";
descTextNew.setTextFormat(descTextStyling);
container.addChild(descTextNew);
Some of these variables are declared as instance variables prior like this:
var names:Array = new Array();
var nameText:TextField = new TextField();
var textStyling:TextFormat = new TextFormat();
var descTextNew:TextField = new TextField();
var descTextStyling:TextFormat = new TextFormat();
var margin:int = 28;
var videoWidth:int = 267;
var videoHeight:int = 150;
arrays names and descArrayContent have the strings "Paul Rollins, Sr." and "Paul Rollins runs a funeral... " added to them at an index number determined by the variable elementNumber. Also object is the image you see in the pic. don't think its necessary to show you that code.
Here's a link if you want to see it in action:
www.footsoldiers.org/test2/
Any ideas what the hell is going on? I don't know where to start.
Have you tried invalidating the control for redraw manually?
this.invalidateDisplayList();
this.invalidateLayering();
this.invalidateLayoutDirection();
this.invalidateProperties();
this.invalidateSize();
I would try to set the .text property after you have set all the other properties.
So try putting the descTextNew.text = descArrayContent[elementNumber] action on the last line.
If this still doesn't fix your problem, I would recommend to also include the descTextNew.multiline = true property.
Good luck!