addchild with multiple movieclip in as3 - actionscript-3

var txt_mc:movieClip=new movieClip();
createTxt(3)
function createTxt(_no):void
{
var _sy = 0;
for (var i=0; i<_no; i++)
{
var txt:TextField = new TextField();
txt_fmt.size = _size;
txt.defaultTextFormat = txt_fmt;
//txt.autoSize = TextFieldAutoSize.CENTER;
txt.autoSize = TextFieldAutoSize.LEFT;
txt.selectable = false;
txt.embedFonts = true;
txt.x = 0;
txt.y = _sy;
_sy = _sy + 25;
//txt.border = true
txt.text = "Enter your text here";
txt_mc.addChild(txt);
}
mc1.addChild(txt_mc);
mc2.addChild(txt_mc);
}
How can i addchild with multiple movieclip.
I was create a movieclip and wants to addchild in two movieclips which are located on stage.
please help me out.
I want to txt_mc will be add in mc1 and mc2 and from that code txt_mc is add in mc2 only.

You can't add the same instance of one movie clip in two different containers. But you can create two similar txt_mc instances and add them to mc1 and mc2.
UPDATE: So, you can modify your createTxt function, so it will return new instance of txt_mc each time you call it. And add it to every container you want:
function createTxt(_no):MovieClip
{
var txt_mc:movieClip=new MovieClip();
var _sy = 0;
for (var i=0; i<_no; i++)
{
var txt:TextField = new TextField();
txt_fmt.size = _size;
txt.defaultTextFormat = txt_fmt;
//txt.autoSize = TextFieldAutoSize.CENTER;
txt.autoSize = TextFieldAutoSize.LEFT;
txt.selectable = false;
txt.embedFonts = true;
txt.x = 0;
txt.y = _sy;
_sy = _sy + 25;
//txt.border = true
txt.text = "Enter your text here";
txt_mc.addChild(txt);
}
return txt_mc;
}
mc1.addChild(createTxt(3));
mc2.addChild(createTxt(3));

Related

Images all added to last sprite, but it is already in a loop

What I am trying to do is:
1.) I parse vectors into the class. It contains simple text,
2.) In a loop it adds the text and (load then add 1 image) into a sprite.
3.) It loops, and then creates a list of boxes where each box has some text and 1 image.
The problem:
I am having a problem. The text adds into the sprite with no problem. It adds all the textfields to it, but all the images are added to the last sprite.
Thanks for your time!
My Code:
for (var k:int = 0; k < test.length; k++)
{
var Bolder:Listing8 = new Listing8();
Bolder.x=20;
if(test[k].pic1){
var loader:Loader = new Loader();
var loading111:loading1 = new loading1;
Bolder.addChild(loading111);
loader.load(new
URLRequest("http://www.rentaid.info/rent/"+test[k].pic1));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
Bolder.addChild(loader);
}
function onImageLoaded(e:Event):void {
removeChild(loading111);
e.currentTarget.loader.content.height =50;
e.target.content.x = 130;
e.target.content.y = 15;
e.target.content.height = 70;
e.target.content.width = 140;
var bf:TextField = new TextField();
var bf1:TextField = new TextField();
var bf2:TextField = new TextField();
var bf3:TextField = new TextField();
bf3.width = 100;
bf.defaultTextFormat = new TextFormat("Arial", 12, 0, null, null, null, null, null, TextFormatAlign.CENTER);
bf.width = 100;
bf.autoSize = TextFieldAutoSize.CENTER;
bf1.width = 100;
bf1.autoSize = TextFieldAutoSize.CENTER;
bf2.autoSize = TextFieldAutoSize.CENTER;
bf3.autoSize = TextFieldAutoSize.CENTER;
bf3.width = 100;
bf1.y= bf.height+5;
// Pulling the textfields content out from the current bookVO
bf.text = test[k].nobed;
bf1.text = test[k].zip;
bf2.text = test[k].Location;
bf3.text = test[k].price;
bf.x = (Bolder.height-bf.height)*.2
Bolder.addChild(bf);
Bolder.addChild(bf1);
Bolder.addChild(bf2);
Bolder.addChild(bf3);
// position the object based on the accumulating variable.
Bolder.y = currentY;
Bolder.mouseChildren = false; // ignore children mouseEvents
Bolder.mouseEnabled = true; // enable mouse on the object - normally set to true by default
Bolder.useHandCursor = true; // add hand cursor on mouse over
Bolder.buttonMode = true;
trace ('add image with index', k);
image[k]= new Sprite();
image[k].addChild(Bolder);
image.mouseChildren = true; // ignore children mouseEvents
image.mouseEnabled = true; // enable mouse on the object - normally set to true by default
image.useHandCursor = true; // add hand cursor on mouse over
image.buttonMode = true;
image[k].addEventListener(MouseEvent.CLICK, gotoscene);
currentY += Bolder.height + 10;
}
if( test.length > 0 )
{
_contentHolder = new Sprite();
addChild(_contentHolder);
for (var j:int = 0; j < test.length; j++) {
_contentHolder.addChild(image[j]);
}
I do not recommend you to put function declaration into a function.
This is the reason of your confusion.
Your program at first executes all code(for example 10 times) EXCEPT this part:
function onImageLoaded(e:Event):void {
e.currentTarget.loader.content.height =50;
var sprite:Sprite = new Sprite;
sprite.y += sprite.height + 10;
sprite.addChild(e.target.content as Bitmap);
Bolder.addChild(sprite);
}
And then executes this part 10 times. And in this situation Bolder is always Bolder == “your_10th_Bolder”
This how events work.
Adding loader as child may help:
loader.load(new URLRequest("http://www.rentaid.info/rent/"+test[k].pic1));
Bolder.addChild(loader);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
and in handler doing this:
function onImageLoaded(e:Event):void {
e.target.content.x = 123;
e.target.content.y = 456;
e.target.content.height = 789;
e.target.content.width = 123;
}

How to add Looped URLRequest image and add them to 1 viewport?

I am trying to loop (add images from Loader thru URLRequest), And the images are added to contentHolder, and then i place all these images which are inside contentHolder into a viewport. But at the moment I have to put the add images to viewport step inside the loop, so it creates a problem, which is each loop, a viewport is added. So 10 viewport overlaps each other. I tested it in debug mode and when the first image is loaded i can quickly slide it, because the viewport has a scrollpane, then second image is added, and i can slide that one, and third one is added on top etc.
But if i put the add Holder to viewport step outside the loop, it gives me the error Error #2007: Parameter child must be non-null, i dont know what to do. Please help, thanks for your time! I had been trying this for 6 hrs already!
And the error is on the line viewport.addChild(_contentHolder1); which is the last part of the code, if you scroll to the bottom.
for (var j:int = 5; j < somedata.length; j++)
{
if(somedata[j]){
var myLoader:Loader = new Loader();
var image:Bitmap;
var url:URLRequest = new URLRequest("http://www.rentaid.info/rent/"+somedata[j]);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
myLoader.load(url);
function onImageLoaded(e:Event):void {
image = new Bitmap(e.target.content.bitmapData);
var currentY:int = 10;
var h = image.height;
var k=image.width/image.height;
_contentHolder = new Sprite();
_contentHolder.y = currentY;
currentY += _contentHolder.height + 10;
addChild(_contentHolder);
_contentHolder.addChild(image);
for (var j:int = 5; j <somedata.length; j++)
{
_contentHolder1 = new Sprite();
addChild(_contentHolder1);
_contentHolder1.addChild(_contentHolder);
var viewport:Viewport = new Viewport();
viewport.y = 0;
viewport.addChild(_contentHolder1);
var scroller:TouchScroller = new TouchScroller();
scroller.width = 300;
scroller.height = 265;
scroller.x = 10;
scroller.y = 100;
scroller.viewport = viewport;
addChild(scroller);
}
}}
Edit:
var loadedArray:Array = new Array();
var counter:int=0;
function loadImage():void{
for (var j:int = 5; j < somedata.length; j++)
{
if(somedata[j]){
var loader:Loader = new Loader();
var image:Bitmap;
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
loader.load(new URLRequest("http://www.rentaid.info/rent/"+somedata[j][counter]));
}
}
}
function onImageLoaded(e:Event):void {
loadedArray.push(e.currentTarget.loader.content as Bitmap);
if(counter == somedata.length-1){
var _contentHolder: Sprite = new Sprite;
addChild(_contentHolder);
for(var i:uint = 5; i < loadedArray.length; i++){
_contentHolder.addChild(loadedArray[i]);
currentY += _contentHolder.height + 10;
}
}
else{
counter++;
loadImage();
}
var viewport:Viewport = new Viewport();
viewport.y = 0;
viewport.addChild(_contentHolder);
var scroller:TouchScroller = new TouchScroller();
scroller.width = 300;
scroller.height = 265;
scroller.x = 10;
scroller.y = 100;
scroller.viewport = viewport;
addChild(scroller);
}
you can use Starling Framework and a loader loop such as:
private var count:uint = 0; //add this in your class
private var imagesVector:Vector.<Image> = new Vector.<Image>(); //add this in your class
private var imagesUrlVector:Vector.<String> = new <String>["url1","url2","url3","etc"]; //add this in your class
private function loadImages():void
{
//create the loader
var loader:Loader = new Loader();
//when texture is loaded
loader.contentLoaderInfo.addEventListener ( Event.COMPLETE, onComplete );
//load the texture
loader.load( new URLRequest (imagesUrlVector[count]));
}
private function onComplete ( e : Event ):void
{
// grab the loaded bitmap
var loadedBitmap:Bitmap = e.currentTarget.loader.content as Bitmap;
// create a texture from the loaded bitmap
var texture:Texture = Texture.fromBitmap ( loadedBitmap );
var card:CustomImage = new CustomImage(texture);
imagesVector.push(card);
trace("load image number" + count);
count++;
if (count < imagesUrlVector.length) {
loadImages();
} else displayImages();
}
private function displayImages():void {
var x:uint = 150;
var i:Number;
for (i = 0; i < cardVector.length; i++ )
{
x += 100;
imagesVector[i].x = x;
addChild(imagesVector[i]);
}
}

Gradient TextField ActionScript 3

I have been searching for a way to put gradient colors on TextField component for the past few hours with no luck. One method is to create a gradient rectangle and set the TextField as a mask for it but I can't make it work:
var m:MovieClip = new MovieClip();
var mTxt:Sprite = new Sprite();
var txt:TextField = new TextField();
var tf:TextFormat = new TextFormat();
var dropShadow:DropShadowFilter = new DropShadowFilter();
dropShadow.distance = 0;
dropShadow.angle = 120;
dropShadow.color = 0x000000;
dropShadow.alpha = 1;
dropShadow.blurX = 2;
dropShadow.blurY = 2;
dropShadow.strength = 1;
dropShadow.quality = 80;
dropShadow.inner = false;
dropShadow.knockout = false;
dropShadow.hideObject = false;
txt.filters = new Array(dropShadow);
tf.font = "Ethnocentric Rg";
tf.color = 0xffde00;
tf.size = 72;
txt.defaultTextFormat = tf;
txt.text = "1756.25";
mTxt.addChild(txt);
var fillType:String = GradientType.LINEAR;
var colors:Array = [0xFF0000, 0x0000FF];
var alphas:Array = [1, 1];
var ratios:Array = [0x00, 0xFF];
var matr:Matrix = new Matrix();
matr.createGradientBox(400, 100, 0, 0, 0);
var spreadMethod:String = SpreadMethod.REFLECT;
m.graphics.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod);
m.graphics.drawRect(0,0,400,120);
m.mask = mTxt;
addChild( m );
This code literally crashes the AIR debugger and exits itself.
What am I doing wrong here?
Set cacheAsBitmap to true and make sure you're adding the mask to the stage.
m.cacheAsBitmap = mTxt.cacheAsBitmap = true;
m.mask = mTxt;
addChild( m );
addChild( mTxt );
I had to set both layers' cacheAsBitmap option to true:
txt.cacheAsBitmap = true;
m.cacheAsBitmap = true;
addChild( m );
addChild(txt);
m.mask = txt;

Why is this method causing Flash to crash without even being called?

I have a class called OESDatePicker, as the name suggests it's a date picker.
There's this method in my class that returns a sprite containing week day names:
private function DrawWeekDays():Sprite
{
var temp:Sprite = new Sprite();
var wds:Array = new Array();
var format:TextFormat = new TextFormat();
format.font = "Tahoma";
format.align = TextFormatAlign.CENTER;
format.size = 11;
format.color = 0xffffff;
trace("HERE");
/*for( var i:int = 0; i < 7; i++ )
{
trace(i);
wds[i] = new TLFTextField();
/*wds[i].width = CELL_SIZE;
wds[i].defaultTextFormat = format;
if( "en" == lang )
{
wds[i].text = day_names_en[i];
}
else
{
wds[i].text = day_names_fa[i];
}
//wds[i].y = 0;*/
//wds[i].x = MARGIN + i * CELL_SIZE;
//wds[i].text = "kkk";
//temp.addChild(wds[i]);
//}
return temp;
}
as you can see I have commented out the for loop. If I don't flash will crash upon running the movie. The thing is that not event the trace("HERE") directive gets executed. And the weird thing is that I have never called that method. But if I remove the for loop comments, flash will crash without printing anything in the output. Any ideas????
EDIT: The following code has the same problem too:
private function DrawWeekDays():Sprite
{
var temp:Sprite = new Sprite();
var wds:Array = new Array();
var format:TextFormat = new TextFormat();
format.font = "Tahoma";
format.align = TextFormatAlign.CENTER;
format.size = 11;
format.color = 0xffffff;
trace("HERE");
//for( var i:int = 0; i < 7; i++ )
//{
//trace(i);
wds[0] = new TLFTextField();
wds[0].width = CELL_SIZE;
wds[0].defaultTextFormat = format;
if( "en" == lang )
{
wds[0].text = day_names_en[0];
}
else
{
wds[0].text = day_names_fa[0];
}
wds[0].y = 0;
wds[0].x = MARGIN + 0 * CELL_SIZE;
temp.addChild(wds[0]);
//}
return temp;
}
What the heck man!!! I was using the class property in Project Properties to use the class. I moved the class to a place where I could import it, and all the problems just faded away!
Thanks for all your help Sunil

How to get dynamic TextInput field values in Actionscript 3

Hi creating dynamic TextInput fields in a function. Need to get the values of those fields in another function. Can anyone throw some light on this.
for(var i:int=0;i<answers.length;i++)
{
txtbox = new spark.components.TextInput();
var lblBox:spark.components.Label = new spark.components.Label();
lblBox.id = "lbl"+i.toString();
lblBox.text = String(answersLabel.getItemAt(i) );
lblBox.width = 10
lblBox.x = xPos-15;
lblBox.y = yPos;
QuestionAnswer.addElement(lblBox);
txtbox.id = "text"+i.toString();
txtbox.x = xPos;
txtbox.y = yPos;
QuestionAnswer.addElement(txtbox);
xPos += 200;
}
var txt:TextField;
var i:uint;
var ary:Array = new Array();
function txtCreation ():void {
for( i=0;i<5;i++)
{
txt = new TextField();
txt.text = "txt"+i;
addChild(txt);
txt.x = 50 + txt.width *i;
txt.y = 20;
ary.push(txt);
}
}
txtCreation();
for(i=0;i<ary.length;i++)
{
trace("array values : " +ary[i].text);
}
Just look at the text variable for the textfield.
var textFromField : String = myInputText.text;