2015-03-19 89 views
0

我正在画布上进行游戏(可能更简单的方法来做到这一点,但我想学习HTML5等)。无论如何,我有一个积分计数器,我正在努力,其中一件事发生。清除画布中的文本不会让我重新绘制

  1. 如果我不写他们所有的(显然)重叠值到什么可读

  2. 但是,如果我先清除上下文之前清除画布,然后写我最终什么也没有。

    PointsScored.prototype.writePointsToCanvas = function(){ 
        var canvas = document.getElementById("pointsScoredCanvas") 
        var context = canvas.getContext("2d") 
        context.clearRect(0, 0, canvasElement.width, canvasElement.height); 
    
        context.fillStyle("black") 
        context.font="30px Verdana" 
        context.fillText("Score " + this.points, window.innerWidth * 0.8, window.innerHeight * 0.1) 
    
    } 
    

如果你不再需要的信息还是让我知道。我把所有东西都分散到文件和对象中(它使我把所有东西都扔在一个脚本之下)。

回答

1

该代码引用了一个不存在的元素,这会在调用fillText之前破坏代码。

这条线:

context.clearRect(0, 0, canvasElement.width, canvasElement.height); 

应该是:

context.clearRect(0, 0, canvas.width, canvas.height); 

此外,fillStyle是一个属性,应该这样写:

context.fillStyle = "black"; // use as property, not method 

希望这有助于!