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

従うのは確かに少し難しいです。

次の可能性のあるエラーが表示されます。

  • myPlayerLabelディスプレイリストに追加されることはないようです。(ありませんaddElement(myPlayerLabel)
  • playerLabelディスプレイリストに追加されますが、設定textされません。(ありませんplayerLabel.text = "a string"
  • myPlayerLabel.text属性ではなく、XML オブジェクト全体を に割り当てていnameます。

これらの間違い以外に、これらすべてを ActionScript で記述しなければならない正当な理由がわかりません。これらのボタンとラベルが MXML で単純にインスタンス化されている場合、コードはより透過的になります。

于 2013-03-12T08:29:43.810 に答える