通过Flex处理XML数据

时间:2013-03-12 04:10:51

标签: xml actionscript-3 flex

我正在制作一个程序,可以将玩家添加到舞台上,我可以添加或减少点数。我认为我接近完成除了一些事情,我无法在屏幕上看到我的播放器名称,我在显示分数时遇到问题。单击按钮时,分数会递增和递减,但显示标签不会显示它们。这是我现在的整个计划。这有点乱,所以我提前道歉。

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                   xmlns:s="library://ns.adobe.com/flex/spark" 
                   xmlns:mx="library://ns.adobe.com/flex/mx"       applicationComplete="init()">
<fx:Style source="style.css.css"/>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
    <fx:XML id="playerData" source="data/data.xml" />
    <s:XMLListCollection id="playerList" source="{playerData.player}" />
</fx:Declarations>

<fx:Style>
    @namespace s "library://ns.adobe.com/flex/spark";
    @namespace mx "library://ns.adobe.com/flex/mx";

    s|Button { color:#F2E9E9; }

    s|Label { color:#f2e9e9; }

    #outterBorder { backgroundColor:F7FEF9; }
</fx:Style>
<s:BorderContainer id="outterBorder" x="328" y="48" width="307" height="547">
    <s:Image x="269" y="10"/>
    <s:Label x="25" y="19" width="107" height="21" text="Score Keeper" color="#000000"/>
    <s:BorderContainer id="innerBorder" x="10" y="58" width="285" height="477">
        <s:TextInput id="myTextBox" x="10" y="425" width="138" />
        <s:Button id="add" x="176" y="425" width="97" label="Add Player" click="addPlayer()" />
        <s:HGroup id="group1" x="10" y="10" width="263" height="92" >
        </s:HGroup>
        <s:HGroup id="group2" x="10" y="114" width="264" height="92">
        </s:HGroup>
        <s:HGroup id="group3" x="10" y="214" width="263" height="92">
        </s:HGroup>
        <s:HGroup id="group4" x="10" y="313" width="263" height="92">
        </s:HGroup>
    </s:BorderContainer>

</s:BorderContainer>

<fx:Script>
    <![CDATA[

        //Gabe Dougherty
        //n222
        //Midterm
        //3-8-13


        public var myPlayerLabel:Label = new Label;
        public var myScoreLabel:Label = new Label;
        public var myIncScore:Button = new Button;
        public var myDecScore:Button = new Button;
        public var myPlayerDelete:Button = new Button;
        public var myScore:Number = 0;
        public var numPlayers:Number = 0;

        public function init():void {
            var file:File = File.desktopDirectory.resolvePath("newPlayer.xml");
            var stream:FileStream = new FileStream()
            stream.open(file, FileMode.READ);

            var str:String = stream.readUTFBytes(stream.bytesAvailable);
            stream.close();

            playerData = XML(str);
            myPlayerLabel.text = playerData;

            //re-enable myDecScore
            myDecScore.enabled = true;

        }

        public function addPlayer():void {
            var file:File = File.desktopDirectory.resolvePath("newPlayer.xml");

            var stream:FileStream = new FileStream();
            stream.open(file, FileMode.WRITE);


            var str:String = playerData.toString();

            stream.writeUTFBytes(str);
            stream.close(); 

            //set all player info
            //make playerLabel
            var playerLabel:Label = new Label();

            //make scoreLabel
            var scoreLabel:Label = new Label();
            scoreLabel.text = String(myScore);

            //make decScore
            var decScore:Button = new Button();
            decScore.label = "-";
            decScore.addEventListener(MouseEvent.CLICK, decrementScore);

            //make incScore
            var incScore:Button = new Button();
            incScore.label = "+"
            incScore.addEventListener(MouseEvent.CLICK, incrementScore);

            //make playerDelete
            var playerDelete:Button = new Button();
            playerDelete.label = "Delete";
            playerDelete.addEventListener(MouseEvent.CLICK, deletePlayer);


            numPlayers++;
            if(numPlayers == 1) {

                group1.addElement(playerLabel);
                group1.addElement(scoreLabel);
                group1.addElement(decScore);
                group1.addElement(incScore);
                group1.addElement(playerDelete);
            } else if(numPlayers == 2) {

                group2.addElement(playerLabel);
                group2.addElement(scoreLabel);
                group2.addElement(decScore);
                group2.addElement(incScore);
                group2.addElement(playerDelete);
            } else if(numPlayers == 3) {

                group3.addElement(playerLabel);
                group3.addElement(scoreLabel);
                group3.addElement(decScore);
                group3.addElement(incScore);
                group3.addElement(playerDelete);;
            } else if(numPlayers == 4) {

                group4.addElement(playerLabel);
                group4.addElement(scoreLabel);
                group4.addElement(decScore);
                group4.addElement(incScore);
                group4.addElement(playerDelete);
            }

            //add a new player from text input
            var newPlayer:XML = <player></player>;

            newPlayer.@name = myTextBox.text;

            playerData.appendChild(newPlayer);

            myPlayerLabel.text = newPlayer;

        }

        public function deletePlayer():void {
            delete playerData.players.player;

            //not sure of the right syntax for deleting a node but I think this is right?
        }

        public function decrementScore(e:MouseEvent):void {
            myScore--;

            myScoreLabel.text = String(myScore);
            if(myScore == 0) {

                myDecScore.enabled = false;
                myDecScore.alpha = .5;
            }
            trace(myScore);
        }

        public function incrementScore(e:MouseEvent):void {
            myScore++;

            myScoreLabel.text = String(myScore);
            trace(myScore);

        }
    ]]>
</fx:Script>
</s:WindowedApplication>

1 个答案:

答案 0 :(得分:0)

确实有点难以理解。

我看到以下可能的错误:

  • 一个似乎永远不会被添加到displaylist的myPlayerLabel。 (没有addElement(myPlayerLabel)
  • 一个playerLabel,它被添加到displaylist,但它的text永远不会被设置。 (没有playerLabel.text = "a string"
  • 您要将整个XML对象分配给myPlayerLabel.text,而不是name属性。

除了这些错误,我认为没有充分的理由为什么所有这些应该用ActionScript编写。如果这些按钮和标签只是在MXML中实例化,那么您的代码将更加透明。