Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么这只会在第一次调用时增加分数?_Javascript_Jquery_Asp.net_Ajax_Asp.net Mvc - Fatal编程技术网

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

Javascript 为什么这只会在第一次调用时增加分数?,javascript,jquery,asp.net,ajax,asp.net-mvc,Javascript,Jquery,Asp.net,Ajax,Asp.net Mvc,复制步骤: function StatHandler ( totalScoreSelector, fruitEatenSelector ) { this.scoreElem = $(totalScoreSelector); this.fruitEeatenElem = $(fruitEatenSelector); this.incrementScore = function ( incAmount ) { var that = this;

复制步骤:

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!");
            }
        });
    }
}
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到10
  • 再把蛇放在食物上
  • 总分还是10分
  • 预期行为:

    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!");
                }
            });
        }
    }
    
    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;
        }
    }
    
    每次蛇头撞击食物时,分数应增加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!");
                }
            });
        }
    }
    
    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;
        }
    }
    


    相关C:

    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!");
                }
            });
        }
    }
    
    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;
        }
    }
    

    有什么想法吗?

    每次调用
    IncrementScore()
    方法(值为零)时,都会在控制器中初始化
    分数<代码>返回分数
    将只返回
    amount
    @StephenMuecke的值,那么我应该将分数放入会话存储中吗?这是一个选项。你需要在某个地方保存它:)当然,如果你不想永久保存该值(例如,以后能够恢复游戏),你可以在客户端上进行计算。john skeet什么时候玩的,得到这么多分数。。开玩笑……)