Alright so I’m fairly new to AS3 and xml, so when answering please explain fully if you can.
I’m trying to create this…nutritional counter? where you type in the name of a food and it returns calorie, fat, carb etc. values. Eventually I want the values to be recorded/saved based on date and be able to recall them and compare the different values.
So far I have this xml file:
<?xml version="1.0" encoding="utf-8"?> <food_base>
<food types="fruit">
<Name>Apple</Name>
<amount servingtype="grams">100</amount>
<calories>59.1</calories>
<fat>0.1</fat>
<carbohydrate>15.3</carbohydrate>
<fiber>2.8</fiber>
<sugars>10</sugars>
<protein>0.1</protein>
</food>
<food types="vegetable">
<Name>Celery</Name>
<amount servingtype="grams">100</amount>
<calories>16</calories>
<fat>0.1</fat>
<carbohydrate>3.7</carbohydrate>
<fiber>1.7</fiber>
<sugars>0</sugars>
<protein>0.8</protein>
</food>
</food_base>
I have on stage a button, an input text field and then various dynamic text fields that correspond to the xml nodes.
I was able to get it so that when I type in “Apple” and press the button I get the nodes and attributes to return values to those text fields. Here is that code:
import flash.events.MouseEvent;import flash.events.OutputProgressEvent;
import flashx.textLayout.accessibility.TextAccImpl;
var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("food.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML) ;
function processXML (e:Event):void {
myXML = new XML(e.target.data);
trace(myXML.food.Name);
parseFood(myXML);
}
function parseFood(foodInput:XML):void {
trace(foodInput);
var foodList:XMLList = foodInput.food;
var foodLa:XMLList = foodInput.food.attributes();
trace(foodInput.food.attributes());
button.addEventListener(MouseEvent.CLICK,buttonEnter);
function buttonEnter(event:MouseEvent):void {
for (var i:int =0; i<foodList.length(); i++)
{
var foodElement:XML=foodList*;
if (input1.text == foodInput.food.Name.text()*){
amount.appendText(foodInput.food.amount.text()* + "
")
amount.scrollV = amount.maxScrollV;
calories.appendText(foodInput.food.calories.text()* + "
")
calories.scrollV = calories.maxScrollV;
fat.appendText(foodInput.food.fat.text()* + "
")
fat.scrollV = fat.maxScrollV;
carbohydrates.appendText(foodInput.food.carbohydrate.text()* + "
")
carbohydrates.scrollV = carbohydrates.maxScrollV;
fiber.appendText(foodInput.food.fiber.text()* + "
")
fiber.scrollV = fiber.maxScrollV;
sugars.appendText(foodInput.food.sugars.text()* + "
")
sugars.scrollV = sugars.maxScrollV;
protein.appendText(foodInput.food.protein.text()* + "
")
protein.scrollV = protein.maxScrollV;
trace(foodInput.food.@types *)
if (foodInput.food.@types * == "fruit") {type.text = "fruit"};
if (foodInput.food.@types * == "vegetable") {type.text = "vegetable"};
}
}
So what I want to do next is have another button (we can call it button2) that when I click will save the data in all of those dynamic text boxes, maybe under a date node/heading. I don’t think the shared object thing is my best bet. I’m thinking something like what is described here http://stackoverflow.com/questions/6481243/saving-flash-as3-data-to-xml is probably a better idea.
I’m just not sure how to apply it. So any help would be appreciated!
If you’re wondering about the kind of thing I’m trying to make it is similar to the food tracker available at http://www.sparkpeople.com just made in flash instead.
Thank you!