I am setting up a textbook type app where I can grab culinary recipes from an xml file. I am working with a mc labeled “Cookies” where I dynamically make a certain amount of buttons depending on how many cookie recipes I have in my xml file. My URL loader and XML data is on the main stage and I grab it from inside a movie clip.
I am getting TypeError: Error #2007: Parameter text must be non-null.
at flash.text::TextField/set text()
However, I am never setting targetBtn:string to null…
function initializeCookieRecipes():void
{
for (var i:int = 0; i < MovieClip(root).XMLRecipes.cookie.@name.length(); i++)
{
var cookieTypeBtn:btn_cookieType = new btn_cookieType;
cookieTypeBtn.name = “cookie” + i;
cookieTypeBtn.cookieTypeName.text = MovieClip(root).XMLRecipes.cookie.@name[i];
cookieTypeBtn.x = xCoord;
cookieTypeBtn.y = yCoord;
cookieBtnArray.push(cookieTypeBtn);
cookieTypeBtn.addEventListener(MouseEvent.CLICK, chooseCookieRecipe);
cookieTypeBtn.buttonMode = true;
addChild(cookieTypeBtn);
if (yCoord < 100)
{
yCoord = yCoord + 65.75;
}else
{
xCoord = xCoord + 280.8;
yCoord = -221.60;
}
}
isInitialized = true;
xCoord = -270.45;
yCoord = -221.60;
}
The event listener connected to the button, removes all the buttons I just created, opens a folder type animation, and shows all of the ingredients and amounts on another mc. I am grabbing the cookie recipe I need by creating a string called targetBtn and assigning it the textfield of the button I just clicked.
function chooseCookieRecipe(event:MouseEvent):void
{
//Sets the button text to a string for xml lookup
targetBtn = event.target.text;
//Removes cookie buttons and clears cookieBtnArray
removeCookieRecipes();
//sets off an animation that looks like a folder opening and then triggers a function that adds
ingredients to the mc from the xml
folderCover.gotoAndPlay(2);
}
function addRecipeCardTxt():void
{
textContainer.x = textContainerX;
textContainer.y = textContainerY;
sp.width = 447;
sp.height = 290;
showCupCakes();
//recipeTitle.text = targetBtn;
recipeYield.text = "Yield: " + MovieClip(root).XMLRecipes.cookie.(@name==targetBtn).@yield;
recipeType.text = "Type: " + MovieClip(root).XMLRecipes.cookie.(@name==targetBtn).@type;
recipeTimeTemp.text = "Bake @ " + MovieClip(root).XMLRecipes.cookie.(@name==targetBtn).@temp + " Degrees, for " + MovieClip(root).XMLRecipes.cookie.(@name==targetBtn).@time;
cardTitle.text = “Ingredients”;
for (var i:int = 0; i < MovieClip(root).XMLRecipes.cookie.(@name==targetBtn).ingredient.length(); i++)
{
if (isNaN(MovieClip(root).XMLRecipes.cookie.(@name==targetBtn).ingredient[i].@amount))
{
var amountString:String = MovieClip(root).XMLRecipes.cookie.(@name==targetBtn).ingredient[i].@amount;
amountIntArray.push(amountString)
}else
{
var amountInt:Number = MovieClip(root).XMLRecipes.cookie.(@name==targetBtn).ingredient[i].@amount;
amountIntArray.push(amountInt);
}
var amountTxtBox:TextField = new TextField(); amountTxtBox.name = "amount" + i; amountTxtBox.x = amountX; amountTxtBox.y = amountY; amountTxtBox.defaultTextFormat = recipeFormat;
if (amountIntArray[i] is Number) { amountTxtBox.text = (amountIntArray[i] * recipeSize) + " " + MovieClip(root).XMLRecipes.cookie.(@name==targetBtn).ingredient[i].@measurement; }else { amountTxtBox.text = (amountIntArray[i]) + " " + MovieClip(root).XMLRecipes.cookie.(@name==targetBtn).ingredient[i].@measurement; } txtBoxArray.push(amountTxtBox); textContainer.addChild(amountTxtBox); amountY = amountY + 25;
var ingredientTxtBox:TextField = new TextField(); ingredientTxtBox.name = "ingredient" + i; ingredientTxtBox.x = ingredientX; ingredientTxtBox.y = ingredientY; ingredientTxtBox.width = 500; ingredientTxtBox.defaultTextFormat = recipeFormat; ingredientTxtBox.text = MovieClip(root).XMLRecipes.cookie.(@name==targetBtn).ingredient[i]; ingredientBoxArray.push(ingredientTxtBox); textContainer.addChild(ingredientTxtBox); ingredientY = ingredientY + 25;
}
sp.update();
amountX = -190.65
amountY = -40.60
ingredientX = -80.80
ingredientY = -40.60
}
I have a back button, that removes all ingredient data, and shows the cookie recipes again on the cover of the folder. Sometimes it works and I can select another cookie, and sometimes it doesn’t work and targetBtn returns a null. I am never setting it to a null, so i don’t know why it works sometimes and not others. Is there a delay that I’m missing. The buttons are there to click so I don’t know why targetBtn would not get assigned the textfield box and return a null. Is there a better way maybe to grab the selected button and get the ingredient info from the xmldata object instead of getting it from a textfield?
function goBack(event:MouseEvent):void
{
targetBtn = “”;
clearTitleTxtBoxes();
clearTxtBoxes();
folderCover.gotoAndPlay(10);
}
function clearTxtBoxes():void
{
while (textContainer.numChildren > 0)
{
textContainer.removeChildAt(0);
}
txtBoxArray.length = 0;
ingredientBoxArray.length = 0;
amountIntArray.length = 0;
}