飞镖游戏 - 检查AngularJS中的问题

时间:2014-11-11 23:42:37

标签: javascript angularjs svg

我的AngularJS(JavaScript)文件的内容是:



// define new Angular module
var myApp = angular.module('myApp',[]);

myApp.controller('MyAppController',function($scope){
    $scope.player = '';
    $scope.players = [];
    $scope.totalPoints = [];
    $scope.flag = 1;
    $scope.myNumberRegex = /[0-9]/;
    $scope.last = {};
    $scope.score = {};
    $scope.winner = 0;
    $scope.nextPlayer = '';
    $scope.nextRound = false;

    var actualPlayer = 0;
    var pointsValue,round = 0;
    var internalRoundCounter = 1;
    var shootsString = '';

    $scope.$watch('roundCount',function(value){
        console.log('new value',value);
    });

    $scope.add = function(){
        $scope.players.push({name: $scope.player, totalPoints: 0, fields:[]});
        
        $scope.player = '';
    };

    $scope.checkForm = function() {

        var valid = true;

        if ($scope.players.length == 0) {
            alert('There is no player added!');
            valid = false;
        }

        if ($scope.roundCount < 3 || $scope.roundCount > 10) {
            alert('Incorrect round count!');
            valid = false;
        }

        if ($scope.players.length == 1) {
            console.log('Tömb hossza: ', $scope.players.length);
            alert('One player is not enough!');
            valid = false;
        }

        if (valid) {
            $scope.flag = 2;
            var startingPlayer = $scope.players[actualPlayer].name;
            $scope.nextPlayer = startingPlayer;
        }
    };

    function showResults(){
        $scope.flag = 3;
    }

    // watching changed value
    $scope.$watch('last.id',function(newValue){
        console.log('last.id changed',newValue);

        pointsValue = 0;

        //-----------------------------------------------------------
        // checking target to calculate earned points
        //-----------------------------------------------------------
        if (newValue == "Bull") {
            pointsValue += 50;
            console.log(pointsValue);
        }
        else if (newValue == "Outer") {
            pointsValue += 25;
            console.log(pointsValue);
        }
        else if (['s','d','t'].indexOf(newValue[0]) != -1) {
            var multiplier = 1;

            if (newValue[0] == 'd')
                multiplier = 2;
            else if (newValue[0] == 't')
                multiplier = 3;

            var tmp = newValue.split("").splice(1,2).join("");
            pointsValue += (tmp * multiplier);                
        }
        //-----------------------------------------------------------

        // while playing darts
        if (round < $scope.roundCount) {

            if (internalRoundCounter < 4){

                shootsString += newValue+' ';
                $scope.players[actualPlayer].totalPoints += pointsValue;
                internalRoundCounter++;
            }
            else{
                $scope.players[actualPlayer].fields.push({fieldId : round+1, fieldName : shootsString});
                shootsString = '';
                internalRoundCounter = 1;
                actualPlayer++;


                if (actualPlayer == $scope.players.length) {
                    actualPlayer = 0;
                    round++;
                }

                $scope.nextPlayer = $scope.players[actualPlayer].name;
            }
        }
        // when game finished
        else{
            showResults();

            $scope.winner = $scope.players[0].name;

            // find winner in array
            for (var i=1; i<$scope.players.length; i++){
                if ($scope.players[i].totalPoints > $scope.players[i-1].totalPoints){
                    $scope.winner = $scope.players[i].name;
                }
            }

            console.log($scope.players);
        }

    });
});

myApp.directive('dartClickListener', function() {
    return {
        restrict: 'A',
        scope: false,
        link: function(scope,element,attrs) {

            console.log(angular.element(element));

            angular.element(element).find('g').children().bind('click',function(){
                console.log(angular.element(this).attr('id'));
                scope.last.id = angular.element(this).attr('id');
                scope.$apply();
            });
        }
    }
});
&#13;
<!DOCTYPE html>
<html ng-app="myApp" ng-init="roundCount=3">

    <head lang="en">
        <meta charset="UTF-8">
        <title>.:: Darts ::.</title>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
        <script src="myscript.js"></script>
        <link rel="stylesheet" href="style.css">
    </head>

    <body ng-controller="MyAppController">

        <div id="start" ng-show="flag == 1">
            <div>
                <h1 class="stdCell gameTitle">Darts</h1>
            </div>

            <div>
                <table>
                    <tr>
                        <td><label class="stdCell" for="playerName">Add player</label></td>
                        <td><input type="text" id="playerName" ng-model="player"></td>
                        <td><input type="button" value="Add" ng-click="add()" ng-disabled="!(!!player)"></td>
                    </tr>
                    <tr>
                        <td><label class="stdCell" for="rounds">Rounds (3-10)&nbsp;&nbsp;&nbsp;</label></td>
                        <td colspan="2"><input type="text" id="rounds" ng-model="roundCount" ng-pattern="myNumberRegex" ng-required="true"></td>
                    </tr>
                </table>
            </div>

            <div>
                <button ng-disabled="!(!!roundCount)" ng-click="checkForm()">Start game</button>
            </div>
        </div>

        <div ng-show="flag == 2">

            <div id="gameState">
                <div>
                    <table>
                        <tr><td class="stdCell borderedCell tableHeader">Player</td><td class="stdCell borderedCell tableHeader">Points</td></tr>
                        <tr ng-repeat="player in players"><td class="stdCell borderedCell">{{player.name}}</td><td class="stdCell borderedCell">{{player.totalPoints}}</td></tr>
                    </table>
                </div>

                <div>
                    <h2 class="stdCell" ng-model="nextPlayer">{{nextPlayer}} is next</h2>
                </div>

            </div>

            <div id="darts">
                <svg id="dartboard" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" x="0px" y="0px" width="630px" height="630px" viewBox="0 0 787 774" enable-background="new 0 0 787 774" xml:space="preserve">
                    <g id="areas" dart-click-listener="last.id">
                        <!-- svg file content -->
            <div>
                <h3 ng-model="winner" ng-repeat="player in players | filter:winner" class="stdCell">The winner: {{winner}} @  points: {{player.totalPoints}}</h3>
            </div>

            <div>
                <table>
                    <tr>
                        <td class="stdCell borderedCell tableHeader" rowspan="2">Player</td>
                        <td class="stdCell borderedCell tableHeader" colspan="{{players[0].fields.length}}">Round</td>
                        <td class="stdCell borderedCell tableHeader totalPointCell" rowspan="2">Total points</td>
                    </tr>
                    <tr>
                        <td class="stdCell borderedCell resultCell" ng-repeat="field in players[0].fields">{{field.fieldId}}</td>
                    </tr>
                    <tr ng-repeat="player in players">
                        <td class="stdCell borderedCell">{{player.name}}</td>
                        <td class="stdCell borderedCell resultCell" ng-repeat="field in player.fields">{{field.fieldName}}</td>
                        <td class="stdCell borderedCell resultCell">{{player.totalPoints}}</td>
                    </tr>
                </table>
            </div>
        </div>

    </body>

</html>
&#13;
&#13;
&#13;

一切都与AngularJS中的飞镖游戏有关,但我有两个问题:

1。)每3次射击后,玩家就会被改变。拍摄正在检查$ scope。$ watch(&#39; last.id&#39; ...)部分。我的问题是,在更换播放器之前,我必须再次点击飞镖表,因为代码只有在点击表格的任一字段后才能运行。我怎样才能消除它?

2。)第二个问题是,我必须计算射击次数,而不是击中桌面。我该怎么办?

飞镖靶是一个SVG文件,插入到HTML代码中,来源于:link

非常感谢您的回答! :)

0 个答案:

没有答案