1

我正在制作一个程序,将玩家添加到舞台上,我可以添加或减去分数。我想我已经接近完成了,除了一些事情,我在屏幕上看不到我的球员姓名,而且我在显示分数时遇到了问题。单击按钮时分数会增加和减少,但显示标签不显示它们。这是我现在的整个程序。有点乱,所以我提前道歉。

<?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>
4

1 回答 1

0

确实有点难以理解。

我看到以下可能的错误:

  • amyPlayerLabel似乎永远不会添加到显示列表中。(没有addElement(myPlayerLabel)
  • aplayerLabel添加到显示列表但text从未设置。(没有playerLabel.text = "a string"
  • 您将整个 XML 对象分配给myPlayerLabel.text,而不是其name属性。

除了这些错误之外,我看不出为什么所有这些都应该用 ActionScript 编写。如果这些按钮和标签只是在 MXML 中实例化,您的代码会更加透明。

于 2013-03-12T08:29:43.810 回答