为什么这只会在第一次调用时增加分数?

时间:2015-08-03 02:42:38

标签: javascript jquery asp.net ajax asp.net-mvc

复制步骤:

  1. 打开http://playclassicsnake.com/play
  2. 单击向左,向上或向下箭头键使蛇(绿色方块)移动
  3. 将蛇指向食物(红色圆圈)
  4. 总得分从0到10
  5. 再次将蛇指向食物
  6. 总分仍为10
  7. 预期行为:

    每次蛇的头撞到食物时,分数应增加10。

    相关的Javascript:

    function StatHandler ( totalScoreSelector, fruitEatenSelector )
    {
        this.scoreElem = $(totalScoreSelector);
        this.fruitEeatenElem = $(fruitEatenSelector);
    
        this.incrementScore = function ( incAmount )
        {
            var that = this;
            $.ajax({
                type: "POST",
                url: "Play/IncrementScore?amount=" + incAmount,
                success: function (newScore)
                {
                    that.scoreElem.text(newScore);
                    console.log(newScore); // TEST
                },
                error: function ( ) 
                {
                    alert("error occured!");
                }
            });
        }
    }
    
            if (game.food.pos.x == newHeadX && game.food.pos.y == newHeadY) // if new head hit food
            {
                this.links.push(new game.elementOnGrid(game.board.rect(oldTailX * game.blockWidth, oldTailY * game.blockWidth, game.blockWidth, game.blockWidth).attr('fill', '#19FF19'),
                                oldTailX,
                                oldTailY
                                ));       // fill space left by trail before it was translated
                // i.e. lengthen the snake
                game.coords[oldTailX][oldTailY] = true; // update coordinate list
                game.stats.incrementScore(10);
                game.placeFood(); // replace food
            }
    

    相关C#:

    public class PlayController : Controller
    {
    
        private ushort score;
    
        // GET: Play
        public ActionResult Index ( )
        {
            ResetScore();
            return View ( );
        }
    
        [HttpPost]
        public ushort IncrementScore ( ushort amount )
        {
            score += amount;
            return score;
        }
    
        [HttpPost]
        public void ResetScore ( )
        {
            score = 0;
        }
    }
    

    关于发生了什么的任何想法?

0 个答案:

没有答案