-1

我正在构建一个应用程序,它允许用户将冰箱中的成分添加到列表中,然后根据他们列出的成分搜索所有潜在的食谱。

我正在使用 XML 数据,该数据以相当简单的模式列出,如下所示:

<items>
<item category="meat">chicken</item>
<item category="meat">beef</item>
<item category="vegetable">green beans</item>
</items>

我的应用程序的滚动列表部分基于 NetTuts 教程(http://active.tutsplus.com/tutorials/actionscript/create-a-smooth-rolling-scroller-with-as3/)。我希望能够通过 XML 属性(即上面的“类别”属性)以编程方式过滤我的列表。

这是我下面的完整文档类(我不确定要排除哪些功能,因为它们都可能很重要)。您应该了解的函数是dataLoaded()解析 XML 数据并将其放入 a中TextField,以及dataLoad()实例化 URLLoader 对象以加载 XML。我的文档类的名称是RecipeMatcher,它的构造函数也是一样的。

我认为我已经对它进行了很好的评论,因此对于变量和函数的用途不应该有太多问号。

package 
{
    ' Imports (All- Unsorted) '
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.net.URLRequest;
    import flash.net.URLLoader;
    import flash.text.TextFormat;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.Font;
    import flash.display.Shape;
    import gs.*;

    public class RecipeMatcher extends MovieClip
    {
        'Variable declaration'
        // ------------------- //
        'Main Structural elements'
        private var _pad:MovieClip;
        public var _addIngredient:MovieClip;
        private var _container:MovieClip;
        'Ingredient Wizard Structural Elements'
        public var _ingredientWizard:MovieClip;
        private var _CollapsePanelMeat:MovieClip;
        private var _CollapsePanelVeg:MovieClip;
        private var _CollapsePanelCarbs:MovieClip;
        private var _CollapsePanelCuisine:MovieClip;
        private var _CollapseMealType:MovieClip;
        private var _CollapseOther:MovieClip;
        'Data (XML) Elements'
        public var loader:URLLoader;
        public var data:XML;
        public var items:XMLList;
        'Positioning of xml data in container - ("Text")'
        private var _item:Item;
        private var _itemTextField:TextField;
        private var _defaultFormat:TextFormat = new TextFormat();
        private var _arialRounded:Font = new ArialRounded();
        private var _textFieldXPosition:uint = 10;
        private var _textFieldYPosition:uint = 13; 
        private var _textFieldWidth:uint = 540;
        private var _textFieldPaddingLeft:uint = 20;
        private var _textFieldHeight:uint = 150;
        private var _itemPosition:uint = 49;
        'newIngredientsWizard positioning'
        private var _addNewIngredientsXPosition:uint = 322;
        private var _addNewIngredientsYPosition:uint = 500;
        'input mask - container'
        private var _mask:Shape;
        private var _maskWidth:uint = 580;
        private var _maskHeight:uint = 540;
        private var _paddingTop:uint = 220;
        'background for container '
        private var _background:Shape;
        'scroll speed properties'
        private var _maxSpeed:uint = 15;
        private var _speed:Number;
        'End of variable declarations'
        //----------------------------------//

        //---------Constructor Function---------//
        public function RecipeMatcher()
        {
            'Constructor for the function RecipeMatcher '
            _addIngredient = new addIngredient();
            _ingredientWizard = new ingredientDialog();
            _pad = new Pad();
            addChild(_pad); 
            _ingredientWizard.visible = false;
            _pad.x = stage.stageWidth / 2 - _pad.width / 2;
            _pad.y = stage.stageHeight / 2 - _pad.height / 2;
            'Event listener for add ingredient button inside of pad movieclip '
            _pad.btn_add.addEventListener(MouseEvent.CLICK, addNewIngredient);
            _container = new MovieClip();
            addChild(_container);
            dataLoad();
        }

        //-- BEGINNING OF LISTENER FUNCTIONS TO FOOD CATEGORY PAGES - PART OF INGREDIENT WIZARD --//

        function displayMeat(event:MouseEvent):void {
            _ingredientWizard.visible = false;
        }
        function displayVeg(event:MouseEvent):void {
            _ingredientWizard.visible = false;
        }
        function displayCarbs(event:MouseEvent):void {
            _ingredientWizard.visible = false;
        }
        function displayCuisine(event:MouseEvent):void {
            _ingredientWizard.visible = false;
        }
        function displayMealType(event:MouseEvent):void {
            _ingredientWizard.visible = false;
        }
        function displayOther(event:MouseEvent):void {
            _ingredientWizard.visible = false;
        }

        //-- This function is the accompanying function for the event listener in the constructor that listens for clicks on the ingredient items --//

        function itemclicked(event:MouseEvent):void {
            _itemTextField = new TextField();
            trace(_itemTextField.text);
        }
        //-- This function makes all of the elements of the scrolling list invisible, whilst the Add New Ingredients Wizard is displayed to the user //
        function addNewIngredient(event:MouseEvent):void
        {
            _pad.visible = false;
            _container.visible = false;
            trace("screen wipe");
            _ingredientWizard = new ingredientDialog();
            addChild(_ingredientWizard);
            _ingredientWizard.x = stage.stageWidth / 2 - _ingredientWizard.width / 2;
            _ingredientWizard.y = stage.stageHeight / 2 - _ingredientWizard.height / 2;

            _ingredientWizard.collapse_meat.btn_submit.addEventListener(MouseEvent.CLICK, displayMeat);
            _ingredientWizard.collapse_veg.btn_submit.addEventListener(MouseEvent.CLICK, displayVeg);
            _ingredientWizard.collapse_carbs.btn_submit.addEventListener(MouseEvent.CLICK, displayCarbs);
            _ingredientWizard.collapse_cuisine.btn_submit.addEventListener(MouseEvent.CLICK, displayCuisine);
            _ingredientWizard.collapse_mealtype.btn_submit.addEventListener(MouseEvent.CLICK, displayMealType);
            _ingredientWizard.collapse_otherdietarypreferences.btn_submit.addEventListener(MouseEvent.CLICK, displayOther);

            'Submit all ingredient choices button listener'
            _ingredientWizard.btn_submit.addEventListener(MouseEvent.CLICK, submitIngredientChoices);
        }

        function submitIngredientChoices(event:MouseEvent):void {
            trace("submitted ingredients");

        }
        private function dataLoad():void {
            'instantiates URLLoader object'
            loader = new URLLoader();
            ' listens to see if data has fully loaded'
            loader.addEventListener(Event.COMPLETE, dataLoaded);
            'loads local xml file with URLrequest method'
            loader.load(new URLRequest("data.xml"));
        }

        private function dataLoaded(event:Event):void {
            ' this holds the loaded xml data '
            data = new XML(event.target.data);
            'items properties call - add other calls to master properties later on'
            items = data.item;
            ' tracing name of all categories in xml doc'
            trace(data.item.@category);
            ' parsing of each ingredient'
            for (var i = 0; i < items.length(); i++) {
                // instantiation of mcItem (the stage for each item)
                _item = new Item();
                // sets 'over' layer to invisible / transparent //
                _item.item_btn_over.alpha = 0;
                // creates the var itemTextField //
                _itemTextField = new TextField();
                // _itemTextField visual attributes //
                _itemTextField.x = _textFieldXPosition + _textFieldPaddingLeft;
                _itemTextField.y = _textFieldYPosition;
                _itemTextField.selectable = true;
                _itemTextField.wordWrap = true;
                _itemTextField.width = _textFieldWidth;
                _itemTextField.height = _textFieldHeight;
                _itemTextField.embedFonts = true;
                _defaultFormat.color = 0x111112;
                _defaultFormat.font = _arialRounded.fontName;
                _defaultFormat.size = 18;
                _itemTextField.defaultTextFormat = _defaultFormat;
                _itemTextField.text = items[i].toString();
                //adds textfield to displaylist//
                _item.addChild(_itemTextField);
                //vertical positioning//
                _item.y = i * _itemPosition;
                _item.buttonMode = true;
                _item.mouseChildren = false;
                //adds items to container displaylist//
                _container.addChild(_item);
                _item.addEventListener(MouseEvent.CLICK, itemclicked);
            }

                ' Input Mask'
                _mask = new Shape();
                _mask.graphics.beginFill(0xFF0000);
                _mask.graphics.drawRect(0, 0, _maskWidth, _maskHeight);
                _mask.graphics.endFill();
                ' Positioning of input mask'
                // horizontal centering of input mask//
                _mask.x = stage.stageWidth / 2 - _container.width / 2;
                _mask.y = _paddingTop;
                // adds the mask onto the stage//
                addChild(_mask);
                // assigns the above mask to the container //
                _container.mask = _mask;
                ' Positioning of container with the mask'
                // horizontal centering of container //
                _container.x = stage.stageWidth / 2 - _container.width / 2;
                // vertical position of container //
                _container.y = _paddingTop;

                'Container background stylings'
                _background = new Shape();
                _background.graphics.beginFill(0xFFFFFF);
                _background.graphics.drawRect(0, 0, _container.width, _container.height);
                _background.graphics.endFill();
                _container.addChildAt(_background, 0);
                'End of container background stylings'

                _container.addEventListener(MouseEvent.MOUSE_OVER, movingOver);
                _container.addEventListener(MouseEvent.MOUSE_OUT, movingOut);
            }

            'function: movingOver (MOUSE_OVER) EVENT '
            private function movingOver (event:MouseEvent):void {
                _container.removeEventListener(MouseEvent.MOUSE_OVER, movingOver);
                addEventListener(Event.ENTER_FRAME, enterFrame);
                if (event.target is Item)
                TweenMax.to(Item(event.target).item_btn_over, .2, {alpha:1});
            }
            'function: movingOut (MOUSE_OUT) EVENT '
            private function movingOut (event:MouseEvent):void {
                removeEventListener(Event.ENTER_FRAME, enterFrame);
                _container.addEventListener(MouseEvent.MOUSE_OVER, movingOver);
                if (event.target is Item)
                TweenMax.to(Item(event.target).item_btn_over, .2, {alpha:0});
            }
            'ENTER_FRAME listener function to gauge scrolling speed based on touch / mouse position + events'
            function enterFrame(event:Event):void {
                _speed = (_mask.height / 2 - _mask.mouseY) / (_mask.height / 2) * _maxSpeed;
                _container.y += _speed;
                if (_container.y >= _paddingTop) {
                    removeEventListener(Event.ENTER_FRAME, enterFrame);
                    _container.y = _paddingTop;
                }
                if (_container.y <= _mask.height - _container.height + _paddingTop) {
                    removeEventListener(Event.ENTER_FRAME, enterFrame);
                    _container.y = _mask.height - _container.height + _paddingTop;
                }
            }
        }
}

回答者,一旦您突出显示了必要的功能,请随时将我的代码编辑到可管理的大小。

编辑:正如您在我的代码中看到的那样,我设法在互联网上找到了这段代码,但它只列出了实际类别本身,而不是类别中的所有项目。我在 AS3 方面还不够好,无法再进一步。

 ' tracing name of all categories in xml doc'
            trace(data.item.@category);
4

1 回答 1

1

您必须按下一行搜索:

const lookingCategoryName : String = "meat";
const findedItemsXMLList : XMLList = data.item.(@category == lookingCategoryName);
trace(findedItemsXMLList.toXMLString());
于 2013-04-18T10:10:41.213 回答