I have my main state which contains a canvas. It loads functions and everything is added to the canvas.
now when I change the state and go back to main state I get that error.
I've entered my whole app. So main is the start state. I then switch states to contactform. NOw after when i switch back to main from contactform i get that error.
I know my code is probably messy. I think im looking for tips on how to properly addChildren so i dont get this error anymore.
<mx:Application xmlns:custom="*" xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()" xmlns:local="*" backgroundColor="#FFFFFF" width="800" height="500" currentState="main">
<mx:states>
<mx:State name="contactform">
<mx:AddChild position="lastChild">
<custom:email_cmp x="150" y="140"/>
</mx:AddChild>
</mx:State>
<mx:State name="main">
<mx:AddChild position="lastChild">
<mx:Canvas id="myCanvas" backgroundColor="0xEEEEEE" height="300" y="200" width="600" fontWeight="normal" fontSize="12" backgroundAlpha="0.02" x="0">
</mx:Canvas>
</mx:AddChild>
</mx:State>
</mx:states>
<mx:TextInput y="50" id="search_text" width="699" horizontalCenter="-41" fontSize="20" color="#4C4C4C" fontWeight="normal" height="36"/>
<mx:Button x="723" y="50" label="Search" id="search_btn" click="search(search_text.text)" height="36" width="67" borderColor="#3A5262"/>
<mx:Style>
<mx:Script>
<![CDATA[
import mx.controls.LinkBar;
import mx.controls.LinkButton;
import mx.core.UIComponent;
import mx.controls.Text;
import flash.text.TextFieldAutoSize;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.net.Responder;
import mx.controls.Alert;
import mx.collections.ArrayCollection;
import flash.display.Sprite;
import flash.display.MovieClip;
import caurina.transitions.Tweener;
public var gateway:RemotingConnection;
public var res:Responder = new Responder(onResult,onFault);
public var amt_cnt:Dictionary = new Dictionary(); // keeps track of how many questions there are and how many to show on a single page.
public var mainDict:Dictionary = new Dictionary();
public var full_array:Array = new Array();
public var mainQsprite:Sprite = new Sprite();
private function init():void{
gateway = new RemotingConnection("url");
this.rawChildren.addChild(mainQsprite);
mainQsprite.y = 100;
mainQsprite.x = 50;
}
private function search(s:String):void{
gateway.call("SearchIt.search", res, s);
}
public function moreHelp(e:MouseEvent):void{
currentState = "contactform";
}
public function moreQuestions(e:MouseEvent):void{
amt_cnt.startCache = amt_cnt.endCache;
amt_cnt.count = amt_cnt.count - 1;
if(amt_cnt.count == 1){
amt_cnt.endCache = amt_cnt.total;
}else{
amt_cnt.endCache = amt_cnt.startCache + 5;
}
mainFunction();
}
public function clicked(e:MouseEvent):void{
while(mainQsprite.numChildren>0){
mainQsprite.removeChildAt(0);
}
var mainFontSize:int = 20;
var theArray:Object = mainDict[e.currentTarget];
var mainFormat:TextFormat = new TextFormat();
mainFormat.font = "Verdana";
mainFormat.color = 0x000000;
mainFormat.size = mainFontSize;
var mainQ:TextField = new TextField();
mainQ.defaultTextFormat = mainFormat;
mainQ.text = theArray.question;
mainQ.width = 700;
mainQ.border = true;
mainQ.selectable = false;
var mainnum_Lines:Number = Math.floor(mainQ.textWidth/mainQ.width)+1;
mainQ.height = ((mainFontSize+8)*mainnum_Lines);
mainQ.wordWrap = true;
mainQsprite.addChild(mainQ);
}
public function over(e:MouseEvent):void{
//Tweener.addTween(e.currentTarget, {scaleX:1.5, scaleY:1.5, time:0.8, transition:"easeoutelastic"});
}
public function out(e:MouseEvent):void{
//Tweener.addTween(e.currentTarget, {scaleX:1, scaleY:1, time:1, transition:"easeoutexpo"});
}
public function onResult(responds:Object):void{ //after search complete build an array with all questions
full_array = [];
var top:Boolean;
for(var s:uint = 0; s<responds.length; s++){
if(s == 0){ // gives the highest count question the "top" attribute.
top = true;
}else{
top = false;
}
full_array[s] = new Array(responds[s].question, responds[s].answer, responds[s].count, top);
}
var total_amt:Number = full_array.length;
var num_Times:Number = Math.floor(total_amt/5)+1;
amt_cnt.num_Times = num_Times;
amt_cnt.total = total_amt;
amt_cnt.count = amt_cnt.num_Times;
if(amt_cnt.count == 1){
amt_cnt.startCache = 0;
amt_cnt.endCache = amt_cnt.total;
}else{
amt_cnt.startCache = 0;
amt_cnt.endCache = 5;
}
mainFunction();
}
public function onFault(responds:Object):void{
}
public function mainFunction():void{
var cache:int = 0;
while(myCanvas.numChildren > 0){
myCanvas.removeChildAt(0);
}
while(myCanvas.rawChildren.numChildren > 0){
myCanvas.rawChildren.removeChildAt(0);
}
//text format for links
var hFormat:TextFormat = new TextFormat();
hFormat.font = "Verdana";
hFormat.color = 0x000000;
hFormat.size = 15;
//text format end
var sprite1:Sprite = new Sprite();
var textQ:TextField = new TextField();
textQ.mouseEnabled = false;
if(amt_cnt.count == 1){
amt_cnt.end = amt_cnt.endCache;
amt_cnt.start = amt_cnt.startCache;
//set the text
textQ.defaultTextFormat = hFormat;
textQ.text = "Can't find your answer? Need more help?";
textQ.x = 275;
textQ.y = 200;
textQ.width = textQ.textWidth +20;
textQ.selectable = false;
sprite1.addChild(textQ);
sprite1.buttonMode = true;
myCanvas.rawChildren.addChild(sprite1);
sprite1.addEventListener(MouseEvent.CLICK, moreHelp);
}else{
amt_cnt.end = amt_cnt.endCache;
amt_cnt.start = amt_cnt.startCache;
textQ.defaultTextFormat = hFormat;
textQ.text = "More Questions...";
textQ.x = 275;
textQ.y = 200;
textQ.width = textQ.textWidth +20;
textQ.selectable = false;
sprite1.addChild(textQ);
sprite1.buttonMode = true;
myCanvas.rawChildren.addChild(sprite1);
sprite1.addEventListener(MouseEvent.CLICK, moreQuestions);
}
var fontSize:int = 12;
//text formatting for the displayed question list Begin
var qFormat:TextFormat = new TextFormat();
qFormat.font = "Verdana";
qFormat.color = 0x000000;
qFormat.size = fontSize;
//ending text format
for(var t:uint = amt_cnt.start; t<amt_cnt.end; t++){
/*if(t == 0){
var topQ:TextField = new TextField();
topQ.text = full_array[t][1];
mainQsprite.addChild(topQ);
}*/
var qSprite:Sprite = new Sprite();
var txt:TextField = new TextField();
txt.defaultTextFormat = qFormat;
txt.text = full_array[t][0];
txt.selectable = false;
txt.mouseEnabled = false;
txt.border = false;
txt.width = 500; // how wide you want the text to go.
var numLines:Number = Math.floor(txt.textWidth/txt.width)+1; //calculates number of lines of the textfield.
txt.height = ((fontSize+8)*numLines);
txt.wordWrap = true;
qSprite.x = 30;
qSprite.y = 500;
var temp_a:Number = cache+10; //20 is the padding between questions displayed
Tweener.addTween(qSprite, {y:temp_a, time:3, delay:t*0.7, transition:"easeoutexpo"}); //tweener INNNNN!
cache = txt.height + temp_a;
qSprite.buttonMode = true;
mainDict[qSprite] = new Object();
mainDict[qSprite].question = full_array[t][0];
mainDict[qSprite].answer = full_array[t][1];
mainDict[qSprite].count = full_array[t][2];
qSprite.addChild(txt);
qSprite.addEventListener(MouseEvent.CLICK, clicked);
qSprite.addEventListener(MouseEvent.MOUSE_OVER, over);
qSprite.addEventListener(MouseEvent.MOUSE_OUT, out);
myCanvas.rawChildren.addChild(qSprite);
}
}
]]>
</mx:Script>