AS3 object inexplicably disappears after 1.5 - 2 minutes - actionscript-3

I'm new to as3 but have been learning as I go.
I am creating a container which appears with text field inputs so the user can create a 'post'. This is triggered to occur when an animation is removed from the stage which it does automatically when it has completed.
However after about 1.5-2 minutes the container inexplicably disappears. This is a problem as a user may well want to take more than 2 minutes to make a post.
I cannot for the life of me figure out why this is happening, could it possible be a garbage collection issue?
Any help is greatly appreciated thank you,
Dan
var titleText:Title = new Title();
var titleInput:TextField = new TextField();
var categoryText:Category = new Category();
var categoryInput:TextField = new TextField();
var postText:Text = new Text();
var postInput:TextField = new TextField();
//setup text formats to be used
var postCreationTextFormat:TextFormat = new TextFormat();
postCreationTextFormat.font = "arial";
postCreationTextFormat.size = 20;
postCreationTextFormat.align = "left";
postCreationTextFormat.leftMargin = 2;
//Fade In Post Creation Box after Seed to Bud Animation.
seedToBud.addEventListener(Event.REMOVED_FROM_STAGE, createPostInput);
//Some variables to declare for the createPostInput function.
var postCreationContainer:PostCreationContainer = new PostCreationContainer;
var contentWindow:ContentWindow = new ContentWindow;
var postCreationInputBoxes:PostCreationInputBoxes = new PostCreationInputBoxes;
var thumb:Thumb = new Thumb;
var track:Track = new Track;
var scrollDownArrow:ScrollDownArrow = new ScrollDownArrow;
var scrollUpArrow:ScrollUpArrow = new ScrollUpArrow;
function createPostInput(event:Event)
{
addChild(postCreationContainer);
postCreationContainer.x = 230;
postCreationContainer.y = 400;
postCreationContainer.addChild(track);
track.x = 428;
track.y = 25;
postCreationContainer.addChild(thumb);
thumb.x = 418;
thumb.y = 25;
postCreationContainer.addChild(scrollDownArrow);
scrollDownArrow.x = 418;
scrollDownArrow.y = 269;
postCreationContainer.addChild(scrollUpArrow);
scrollUpArrow.x = 418;
scrollUpArrow.y = 6;
postCreationContainer.addChild(contentWindow);
contentWindow.x = 6;
contentWindow.y = 6;
postCreationContainer.addChild(postCreationInputBoxes);
postCreationInputBoxes.x = 6;
postCreationInputBoxes.y = 6;
postCreationContainer.alpha = 0;
postCreationContainer.addEventListener(Event.ENTER_FRAME, fadeInCreatePostInput);
postCreationInputBoxes.addChild(titleText);
titleText.x = 0;
titleText.y = 0;
postCreationInputBoxes.addChild(titleInput);
postCreationInputBoxes.addChild(categoryText);
categoryText.x = 0;
categoryText.y = 60;
postCreationInputBoxes.addChild(categoryInput);
postCreationInputBoxes.addChild(postText);
postText.x = 0;
postText.y = 124;
postCreationInputBoxes.addChild(postInput);
titleInput.defaultTextFormat = postCreationTextFormat;
titleInput.type = "input";
titleInput.multiline = false;
titleInput.wordWrap = false;
titleInput.maxChars = 150;
titleInput.border = true;
titleInput.width = 403;
titleInput.height = 30;
titleInput.x = 0;
titleInput.y = 32;
titleInput.background = true;
titleInput.backgroundColor = 0xFFFFFF;
categoryInput.defaultTextFormat = postCreationTextFormat;
categoryInput.type = "input";
categoryInput.multiline = false;
categoryInput.wordWrap = false;
categoryInput.maxChars = 150;
categoryInput.border = true;
categoryInput.width = 403;
categoryInput.height = 30;
categoryInput.x = 0;
categoryInput.y = 96;
categoryInput.background = true;
categoryInput.backgroundColor = 0xFFFFFF;
postInput.defaultTextFormat = postCreationTextFormat;
postInput.type = "input";
postInput.multiline = true;
postInput.wordWrap = true;
postInput.maxChars = 500;
postInput.border = true;
postInput.width = 403;
postInput.height = 361.5;
postInput.x = 0;
postInput.y = 156;
postInput.background = true;
postInput.backgroundColor = 0xFFFFFF;
stage.focus = titleInput;
seedToBud.removeEventListener(Event.REMOVED_FROM_STAGE, createPostInput);
}
function fadeInCreatePostInput(event:Event):void
{
postCreationContainer.alpha += 0.05;
if (postCreationContainer.alpha == 1)
{
postCreationContainer.removeEventListener(Event.ENTER_FRAME, fadeInCreatePostInput);
}
}

Do you know for a fact that there are no rounding issues with your alpha incrementer? I'd be checking the integer value of alpha and not the real, floating point value. Or, do a fuzz comparison (alpha >= 1-FUZZ && alpha <= 1+FUZZ) for some small value of FUZZ.

Related

AS3 set (maximum-)height of movieclip without scaling content

I am trying to set a maximum height of a Textfield, after which the Text will only be displayed if you scroll.
I know, that you cannot set the height of a textfield as it is protected. Therefore I put the Textfield inside a movieclip and wanted to set a height for that. However, if I do so, it rescales the text. Depending on the amount of text it not only gets ugly, but plainly unreadable.
function createInfoBox():void
{
for (var i:uint = 0; i < level_200_menu_NEW.numChildren; i++)
{
var button200 = level_200_menu_NEW.getChildAt(i);
button200.addEventListener(MouseEvent.CLICK, goBack);
}
while (infobox_MC.numChildren)
{
infobox_MC.removeChildAt(0);
}
// Create a new instance of the _agilita_str symbol from the document's library.
var myFont:Font = new HelveticaNeueLight();
var nextSprite:MovieClip = new MovieClip();
var prevSprite:MovieClip = new MovieClip();
var infobox_title:TextField = new TextField();
var infobox_text:TextField = new TextField();
var infobox_facts:TextField = new TextField();
var infobox_next:TextField = new TextField();
var infobox_prev:TextField = new TextField();
var format_title:TextFormat = new TextFormat();
format_title.font = myFont.fontName;
format_title.size = 26;
format_title.color = 0x2d4275;
var format_text:TextFormat = new TextFormat();
format_text.font = myFont.fontName;
format_text.size = 20;
format_text.color = 0x000000;
format_text.leading = 6;
format_text.indent = - 17;
format_text.blockIndent = 17;
var format_bullet:TextFormat = new TextFormat();
format_bullet.font = myFont.fontName;
format_bullet.size = 20;
format_bullet.color = 0x000000;
format_bullet.indent = - 17;
format_bullet.blockIndent = 17;
format_bullet.leading = 6;
format_bullet.kerning = true;
var format_btn:TextFormat = new TextFormat();
format_btn.font = myFont.fontName;
format_btn.size = 12;
format_btn.color = 0x2d4275;
var thisItem = xml.item.(#LEVEL == activeLevel);
var nextItem = xml.item.(#LEVEL == thisItem.NEXT_LEVEL);
var prevItem = xml.item.(#LEVEL == thisItem.PREV_LEVEL);
infobox_prev.text = prevItem.NAME;
infobox_prev.autoSize = TextFieldAutoSize.LEFT;
infobox_prev.selectable = false;
infobox_prev.embedFonts = true; // very important to set
infobox_prev.setTextFormat(format_btn);
prevSprite.x = 50;
prevSprite.y = 720-50;
prevSprite.buttonMode = true;
prevSprite.mouseChildren = false;
prevSprite.addEventListener(MouseEvent.CLICK, infoboxPrev);
prevSprite.addChild(infobox_prev);
infobox_next.text = nextItem.NAME;
infobox_next.multiline = false;
infobox_next.autoSize = TextFieldAutoSize.RIGHT;
infobox_next.selectable = false;
infobox_next.embedFonts = true; // very important to set
infobox_next.setTextFormat(format_btn);
nextSprite.x = 250;
nextSprite.y = 720-50;
nextSprite.buttonMode = true;
nextSprite.mouseChildren = false;
nextSprite.addEventListener(MouseEvent.CLICK, infoboxNext);
nextSprite.addChild(infobox_next);
var line:Shape = new Shape();
var line2:Shape = new Shape();
var box:Shape = new Shape();
box.name = "box";
infobox_MC.addChild(box);
infobox_MC.setChildIndex(box,0);
infobox_title.text = xml.item.(#LEVEL == activeLevel).NAME;
infobox_title.autoSize = "left";
infobox_title.selectable = false;
infobox_title.embedFonts = true; // very important to set
infobox_text.name = "infobox_text";
infobox_text.autoSize = TextFieldAutoSize.LEFT;
infobox_text.multiline = false;
infobox_text.selectable = false;
infobox_text.wordWrap = false;
infobox_text.embedFonts = true; // very important to set
infobox_text.text = xml.item.(#LEVEL == activeLevel).INFOTEXT;
//infobox_text.mouseWheelEnabled = false;
//infobox_text.appendText("\n aus de.wikipedia.org");
var temp:String = xml.item.(#LEVEL == activeLevel).FACTS;
var FACTS = temp.split("| ").join("\n");
infobox_facts.name = ("infobox_facts")
infobox_facts.multiline = true;
infobox_facts.selectable = false;
infobox_facts.embedFonts = true; // very important to set
infobox_facts.autoSize = TextFieldAutoSize.LEFT;
//infobox_facts.multiline = false;
infobox_facts.wordWrap = false;
infobox_facts.text = FACTS;
infobox_facts.mouseWheelEnabled = false;
infobox_title.defaultTextFormat = format_title;
infobox_text.defaultTextFormat = format_text;
infobox_facts.defaultTextFormat = format_bullet;
infobox_title.setTextFormat(format_title);
infobox_text.setTextFormat(format_text);
infobox_facts.setTextFormat(format_bullet);
//infobox_text.styleSheet = style_text;
box.graphics.beginFill(0xFFFFFF); // choosing the colour for the fill, here it is red
box.graphics.drawRect(0, 0, 400,720); // (x spacing, y spacing, width, height)
box.graphics.endFill(); // not always needed but I like to put it in to end the fill
box.alpha = .85;
infobox_title.x = 30;
infobox_title.y = 50;
infobox_text.x = (1280-700);
infobox_text.y = infobox_title.y + infobox_title.height + 10;
infobox_text.alpha = 0;
var maxWidthFacts:Number = 300;
var maxWidthText:Number = 600;
if (infobox_text.width > maxWidthText)
{
infobox_text.multiline = true;
infobox_text.wordWrap = true;
infobox_text.width = maxWidthText;
}
infobox_facts.x = 50;
infobox_facts.y = infobox_title.y + infobox_title.height + 10;
if (infobox_facts.width > maxWidthFacts)
{
infobox_facts.multiline = true;
infobox_facts.wordWrap = true;
infobox_facts.width = maxWidthFacts;
}
line.graphics.lineStyle(2.5, 0x3F3F3F, 1);
line.graphics.moveTo(infobox_title.x, infobox_title.y + infobox_title.height -2);
line.graphics.lineTo(box.width - infobox_title.x, infobox_title.y + infobox_title.height-2);
line2.graphics.lineStyle(1, 0x3F3F3F, 1);
line2.graphics.moveTo(infobox_text.x, infobox_text.y + infobox_text.height);
line2.graphics.lineTo(box.width - infobox_title.x, infobox_text.y + infobox_text.height);
var back_BTN = new Back_BTN();
back_BTN.addEventListener(MouseEvent.CLICK, playBackwards);
back_BTN.x = 1150;
back_BTN.y = 25;
back_BTN.name = "back_BTN";
for each(var item in xml.item.(#LEVEL == activeLevel))
{
if(item.REFERENZ != "NONE")
{
var testSprite:MovieClip = new MovieClip();
var infobox_more:TextField = new TextField();
var moreSprite:MovieClip = new MovieClip();
testSprite.name = "REFERENZ";
testSprite.x = 50;
testSprite.y = infobox_facts.y + infobox_facts.height + 25;
imageLoad(item.REFERENZ,testSprite);
moreSprite.addChild(infobox_more);
infobox_MC.addChild(testSprite);
infobox_MC.addChild(moreSprite);
moreSprite.x = testSprite.x;
moreSprite.y = testSprite.y;
moreSprite.buttonMode = true;
moreSprite.mouseChildren = false;
moreSprite.name = "infobox_more";
infobox_more.text = "Referenzbeispiele";
infobox_more.multiline = false;
infobox_more.autoSize = TextFieldAutoSize.RIGHT;
infobox_more.selectable = false;
infobox_more.embedFonts = true; // very important to set
infobox_more.setTextFormat(format_btn);
//var image = testSprite.getChildAt(0);
var spriteWidth = testSprite.width * .035;
var spriteHeight = testSprite.height * .035;
testSprite.alpha = 0;
/*
testSprite.buttonMode = true;
testSprite.mouseChildren = true;
testSprite.addEventListener(MouseEvent.ROLL_OVER, iconHover);
testSprite.addEventListener(MouseEvent.ROLL_OUT, iconOut);
testSprite.addEventListener(MouseEvent.CLICK, iconBig);
*/
//moreSprite.buttonMode = true;
//moreSprite.mouseChildren = true;
moreSprite.addEventListener(MouseEvent.CLICK, iconBig);
infobox_MC.setChildIndex(testSprite,infobox_MC.numChildren-1);
infobox_MC.addChild(moreSprite);
}
}
var infobox_text_MC:MovieClip = new MovieClip();
infobox_text_MC.name = "infobox_text_MC";
infobox_text_MC.addChild(infobox_text);
infobox_MC.addChild(infobox_text_MC);
infobox_text_MC.scaleMode = StageScaleMode.NO_SCALE;
infobox_text_MC.height = 600;
trace(infobox_text_MC.x);
trace(infobox_text_MC.y);
trace(infobox_text_MC.width);
trace(infobox_text_MC.height);
infobox_text_MC.visible = true;
infobox_facts.text = FACTS;
infobox_MC.addChild(infobox_title);
infobox_MC.addChild(infobox_facts);
//infobox_MC.addChild(nextSprite);
//infobox_MC.addChild(prevSprite);
infobox_MC.addChild(back_BTN);
einblenden(infobox_MC);
}
With some other function the alpha of the textfields is toogled to one. So once I execute the right function it is visible but it's squished.
Any help would be appreciated
Chris
You should be using numLines to determine the number of lines in your textfield.
var maxlines:Number = 5;
if( infobox_facts.numlines > maxLines )
{
// scroll it
}
okay, so I found where I was going wrong.
the line "*infobox_text.autoSize = TextFieldAutoSize.LEFT;*" prevents me from setting the width and hight of a textField. Turning that off will let you then specify height as well as width and the texfield doesn't need to be nested within a movieclip.
Sometimes one doesn't see the wood for the trees...

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

Create multiple shapes dynamically in ActionScript 3

This is code that I use. I am trying to dynamically create multiple shapes and create animation (function drawLine) with them. I have result shows me only one animation(first one) and then nothing. It should be 6 animation. I think that addChild() function is not creating multiple shapes as I wanted.
import flash.display.Shape;
var startPoint:Point = new Point();
var endPoint:Point = new Point();
var prog:Number = 0;
var bonus:Number = 1;
var frames:int = 150;
var numbers:Array = new Array();
numbers[0] = new Array();
numbers[0][0] = 460;
numbers[0][1] = 100;
numbers[1] = new Array();
numbers[1][0] = 10;
numbers[1][1] = 20;
numbers[2] = new Array();
numbers[2][0] = 10;
numbers[2][1] = 100;
numbers[3] = new Array();
numbers[3][0] = 10;
numbers[3][1] = 180;
numbers[4] = new Array();
numbers[4][0] = 160;
numbers[4][1] = 20;
numbers[5] = new Array();
numbers[5][0] = 160;
numbers[5][1] = 100;
numbers[6] = new Array();
numbers[6][0] = 160;
numbers[6][1] = 180;
numbers[7] = new Array();
numbers[7][0] = 310;
numbers[7][1] = 20;
numbers[8] = new Array();
numbers[8][0] = 310;
numbers[8][1] = 100;
numbers[9] = new Array();
numbers[9][0] = 310;
numbers[9][1] = 180;
var fullDate:String = "271524";
var i:Number;
var pom:Number;
var shapes:Vector.<Shape> = new Vector.<Shape>();
for (i=0; i < fullDate.length; i++){
pom = int(fullDate.charAt(i));
shapes[i] = new Shape();
addChild(shapes[i]);
trace(numbers[pom+1][0]);
drawLine(numbers[pom][0], numbers[pom][1], numbers[pom+1][0], numbers[pom+1][1], 120);
}
function drawLine(startX:Number, startY:Number, endX:Number, endY:Number, time:Number = 120):void {
startPoint.x = startX;
startPoint.y = startY;
endPoint.x = endX;
endPoint.y = endY;
frames = time;
prog = 0;
this.addEventListener(Event.ENTER_FRAME, tick);
}
function drawLineTick(progress:Number):void{
for each(var shape:Shape in shapes)
{
shape.graphics.clear();
shape.graphics.lineStyle(14,0x2dfdfd);
shape.graphics.moveTo(startPoint.x,startPoint.y);
shape.graphics.lineTo(startPoint.x + ((endPoint.x - startPoint.x) * progress), startPoint.y + ((endPoint.y - startPoint.y) * progress));
}
}
function tick(e:Event):void {
if(prog >= frames){
this.removeEventListener(Event.ENTER_FRAME, tick);
}
drawLineTick(prog / frames);
prog += bonus;
}
The problem is, s only refers to the last Shape you created. Try something like this: (at the moment, all the shapes are on top of each, so you won't see multiple animations, but you can adjust that)
fullDate = "174689";
var shapes:Vector.<Shape> = new Vector.<Shape>();
for (i=0; i < fullDate.length; i++){
pom = int(fullDate.charAt(i));
shapes[i] = new Shape();
addChild(shapes[i]);
drawLine(numbers[pom][0], numbers[pom][1], numbers[pom+1][0], numbers[pom+1][1], 50);
}
function drawLine(startX:Number, startY:Number, endX:Number, endY:Number, time:Number = 120):void {
startPoint.x = startX;
startPoint.y = startY;
endPoint.x = endX;
endPoint.y = endY;
frames = time;
prog = 0;
this.addEventListener(Event.ENTER_FRAME, tick);
}
function drawLineTick(progress:Number):void{
for each(var shape:Shape in shapes)
{
shape.graphics.clear();
shape.graphics.lineStyle(14,0x2dfdfd);
shape.graphics.moveTo(startPoint.x,startPoint.y);
shape.graphics.lineTo(startPoint.x + ((endPoint.x - startPoint.x) * progress), startPoint.y + ((endPoint.y - startPoint.y) * progress));
}
}
function tick(e:Event):void {
if(prog >= frames){
this.removeEventListener(Event.ENTER_FRAME, tick);
}
drawLineTick(prog / frames);
prog += bonus;
}

Two colors in one text field using Actionscript 3

Is it possible to have two text colors in one text field using Actionscript 3.0?
ex: how can i make like the first string black and the second string red?
Here is my code when using a single color:
public function logs(txt)
{
if (txt == '')
{
textLog.text = "Let's Open up our treasure boxes !!!";
}
else
{
textLog.text = '' + txt + '';
}
textLog.x = 38.60;
textLog.y = 60.45;
textLog.width = 354.50;
textLog.height = 31.35;
textLog.selectable = false;
textLog.border = false;
var format:TextFormat = new TextFormat();
var myFont:Font = new Font1();
format.color = 0x000000;
format.font = myFont.fontName;
format.size = 18;
format.align = TextFormatAlign.CENTER;
format.bold = false;
textLog.embedFonts = true;
textLog.setTextFormat(format);
this.addChild(textLog);
}
In setTextFormat you can specify start index and end index. You can also render text as html using textLog.htmlText.
First set the text
var t:TextField = new TextField();
t.text = "BLUEGREEN";
addChild(t);
Then method 1
var format1:TextFormat = t.getTextFormat(0, 4);
format1.color = 0x0000ff;
t.setTextFormat(format1, 0, 4);
var format2:TextFormat = t.getTextFormat(5, t.length);
format2.color = 0x00ff00;
t.setTextFormat(format2, 5, t.length);
Or method 2
t.htmlText = '<font color="#0000ff">BLUE</font><font color="#00ff00">GREEN</font>';
If you want to do like that, you need create a function to control. charAt(DEFINE INDEX OF STRING HERE).
var format2:TextFormat = textbox.defaultTextFormat;
format2.color = 0x000000;
textbox.defaultTextFormat = format2;
if((textbox.text.charAt(3) == "d") && ( textbox.text.charAt(4) == "i")){
var format1:TextFormat = textbox.defaultTextFormat;
format1.color = 0xFF0000;
textbox.setTextFormat(format1, 3, 5);}
else{
textbox.setTextFormat(textbox.defaultTextFormat);}

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;