动画画布背景闪烁?

时间:2020-01-24 11:53:52

标签: javascript

我正在尝试使用在画布上移动的单词来制作打字游戏,这些单词从左到右移动,如果它们离开画布会丧生,但是我正在尝试为画布创建一个简单的循环背景图像动画,我已经使它工作并且循环并向左移动,但是由于某种原因,当我使用它时,我的整个画布闪烁着,好像它不断地以一种奇怪的方式重新绘制,或者是我不真正知道的东西。

var canvas;
 var canvasContext;
let x=20;
var string;
let score = 0;
let highscore = 0;
var lives = 3;
var WordID;
var difficultyWordID
var bg = new Image();
bg.src = "client/img/stars.jpg";



window.onload = function(){

canvas = document.getElementById('typingCanvas');
var typeval = document.getElementById("typingValue");       //user typed value.
canvasContext = canvas.getContext('2d');
document.getElementById("Button2").style.display = "none";
document.getElementById("gameOver").style.display = "none";
document.getElementById("scoreText").style.display = "none";
let fps=40;
setInterval(function(){
    if(x==20){
        string = getWord()
    }
    moveEverything()
    drawEverything(x,string);

    if (x>900) {
        lives--;
    } else if (lives<1) {
          canvas.style.display="none";
        document.getElementById("Button2").style.display = "block";
        document.getElementById("GameTable").style.display = "none";
        document.getElementById("gameHR2").style.display = "none";
        document.getElementById("gameHR3").style.display = "none";
        document.getElementById("gameOver").style.display = "block";
        document.getElementById("scoreText").style.display = "block";
        document.getElementById("GameHeading").textContent = "Results below: ";
        document.getElementById("scoreText").textContent = "Your Score: " + score;


        function updateUserScore() {

            fetch("/leaderboard/update", {method: 'post', body: formData}
            ).then(response => response.json()
            ).then(responseData => {

                if (responseData.hasOwnProperty('error')) {
                    alert(responseData.error);
                } else {


                }
            });
        }

    }


    if(x>900 || check()){
        x=20;
        document.getElementById("val").value = '';      //if inputed value get match then blank the input box.


    }
},1000/fps)
}

function drawEverything(x,string ){ // draws text etc
canvasContext.fillStyle="rgba(0,0,200,0)";      //  background colour
canvasContext.border="white"
canvasContext.fillRect(20,20,canvas.width,canvas.height);
drawString(x,string);
scoreBoard(score);
highScoreBoard(highscore);


}

function moveEverything(){
x+=4; // movement speed of the word
}


function drawString(x,string) {
canvasContext.font="30px Verdana";
canvasContext.fillStyle='gray';
canvasContext.fillText(string,x,280);  // place of text appearing.
}

function Background(){ // sets background
this.x = 0, this.y = 0, this.w = bg.width, this.h = bg.height;
this.render = function(){
    canvasContext.drawImage(bg, this.x--, 0);
    if(this.x <= -499){
        this.x = 0;
    }
}
}
var background = new Background(); // actual background animation
function animate(){
  background.render();

}
 var animateInterval = setInterval(animate, 40);


 function getWord(WordID) {
WordID = Math.floor(Math.random()*30) +1
difficultyWordID = WordID

if (WordID === null) {

} else
    fetch("/words/single/" + WordID, {method: 'get'}
     ).then(response => response.json()
    ).then(responseData => {
        if (responseData.hasOwnProperty('error')) {
        alert(responseData.error);
    } else {
       string = responseData.Definition;


    }
});
}


function check(WordID){
var userVal = document.getElementById("val").value;
if(userVal==string){
    return true;
}
return false;
}

function scoreVal(){
if(check()){
    fetch("/words/single/"  + difficultyWordID, {method: 'get'}
    ).then(response => response.json()
    ).then(responseData => {
        if (responseData.hasOwnProperty('error')) {
            alert(responseData.error);
        } else {
            let difficultyScore  = 1;
            difficultyScore = responseData.Difficulty;
            score=score + difficultyScore;

        }
    });
}
}

function highScoreVal(){
if(score>highscore){
    highscore=score;
}
}

  function scoreBoard(score){
scoreVal(WordID);
canvasContext.fillStyle = "White";
canvasContext.font = "40px hot_sauceitalic";
canvasContext.fillText("Your Score: ",50,60);
canvasContext.fillStyle = "White";
canvasContext.fillText(score, 250, 60);
}
function highScoreBoard(highscore){
highScoreVal();
canvasContext.fillStyle = "White";
canvasContext.fillText("Lives:",750,60);
canvasContext.fillStyle = "White";
canvasContext.font = "40px hot_sauceitalic";
canvasContext.fillText(lives, 850, 60);
}

我不确定该如何解决此问题,因为我自己无法真正解决问题,我认为这与它如何在移动单词旁边绘制背景有关。

编辑-尝试使用背景图片并获取了它。

https://i.stack.imgur.com/UhNai.png

0 个答案:

没有答案