Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.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
使用canvas javascript逐字书写文本?_Javascript_Html_Canvas_Html5 Canvas - Fatal编程技术网

使用canvas javascript逐字书写文本?

使用canvas javascript逐字书写文本?,javascript,html,canvas,html5-canvas,Javascript,Html,Canvas,Html5 Canvas,我试着让这些信一个接一个地写出来,但是当每封信都写出来的时候,会有更多的超时时间。setTimeout()方法无法正常工作,因为代码的上半部分在更新函数中运行,并且每一帧都会被调用。这是我的密码: function update() { requestAnimationFrame(update); //Updates canvas every frame /**/ //Checking if the keys are pressed down and changes veloc

我试着让这些信一个接一个地写出来,但是当每封信都写出来的时候,会有更多的超时时间。setTimeout()方法无法正常工作,因为代码的上半部分在更新函数中运行,并且每一帧都会被调用。这是我的密码:

function update() {
    requestAnimationFrame(update); //Updates canvas every frame

    /**/ //Checking if the keys are pressed down and changes velocity accordingly
    if /*W & uparrow*/ (keys[87] || keys[38]) {
        if (velY > -speed) {
            velY--;
        }
    }
    if /*S & downarrow*/ (keys[83] || keys[40]) {
        if (velY < speed) {
            velY++;
        }
    }
    if /*D & rightarrow*/ (keys[68] || keys[39]) {
        if (velX < speed) {
            velX++;
        }
    }
    if /*A & leftarrow*/ (keys[65] || keys[37]) {
        if (velX > -speed) {
            velX--;
        }
    }
    /**/

    /**/ //Bullet bounds check
    if (bulletxc < (WIDTH + 40)){
        /**/ //Bullet moving if it is in frame
        if (bulletVelocity < bulletSpeed) {
            bulletVelocity++;
        }
        /**/
    } else {        
        bulletxc = 120; //Setting start position
    }
    /**/

    /**/ //Applies friction and move the character
    bulletVelocity *= bulletFriction;
    bulletxc += bulletVelocity;
    /**/

    /**/ // Applying friction so the character stops and moves according to the physics
    velY *= friction;
    y += velY;
    velX *= friction;
    x += velX;
    /**/

    /**/ // Checking for bounds On the x axis
    if (x >= (WIDTH - 78)) {
        x = (WIDTH - 78);
    } else if (x <= 3) {
        x = 3;
    }
    /**/

    /**/ // Checking for bounds On the y axis
    if (y > (HEIGHT - 120)) {
        y = (HEIGHT - 120);
    } else if (y <= 3) {
        y = 3;
    }
    /**/

    /**/ //Drawing the placeholder character
    ctx.clearRect(0, 0, WIDTH, HEIGHT);
    characterImg = document.getElementById('character');
    character = new createImage(characterImg, x, y, characterWidth, characterHeight);
    /**/

    /**/ //Drawing and setting collision hitbox for projectile
    bulletImg = document.getElementById('bullet');
    bullet = new createImage(bulletImg, bulletxc, bulletyc, bulletWidth, bulletHeight);
    var bulletx = bulletxc + (bulletWidth - 32);
    var bullety = bulletyc + bulletHeight;
    var combineX = x + characterWidth;
    var combineY = y + characterHeight;
    if((bulletyc<=combineY && y<=bullety) && (x<=bulletx && combineX>=bulletxc)){
        //alert("Collision Detected");
        bulletxc = 120;
        updateText();
        charHealthPoints += -1;
    }
    /**/
    let healthPointText = createText("black", pfont, "20px", "Health: " + charHealthPoints, 26, 36);
    let writtenText = createText("black", pfont, "50px", letter.join(""), 510, 100);
    if (charHealthPoints <= 0){
        window.open('death.html','_self');
    }
}
/**/
function increaseI(){
    i += 1;
}

function writeText(){
    letter[i] = text[i];
}

createText = function(fillStyles, fonts, fontsize, text, x, y) {
    ctx.font = fontsize + " " + fonts;
    ctx.fillStyle = fillStyles;
    ctx.fillText(text, x, y);
};

function updateText(){
    requestAnimationFrame(updateText);


    writeText();
    increaseI();
}
函数更新(){
requestAnimationFrame(更新);//每帧更新画布
/**///检查按键是否按下并相应改变速度
if/*W&向上箭头*/(键[87]| |键[38]){
如果(速度>速度){
弗利——;
}
}
if/*S和向下箭头*/(键[83]| |键[40]){
如果(速度<速度){
vc++;
}
}
if/*D和rightarrow*/(键[68]| |键[39]){
如果(velX<速度){
velX++;
}
}
if/*A和leftarrow*/(键[65]| |键[37]){
如果(velX>-速度){
绒毛--;
}
}
/**/
/**///项目符号边界检查
如果(bulletxc<(宽度+40)){
/**///如果在帧中,则项目符号移动
if(bulletVelocity=(宽度-78)){
x=(宽度-78);
}否则,如果(x(高度-120)){
y=(高度-120);

}否则如果(你为什么在这里调用new?这只是一个函数。@DaveNewton我不知道它以前是如何工作的,所以我认为你就是这样做的,但显然不是。你可以发布更新函数的代码吗?如果你的更新函数有一个超时,也许你可以检查自上次更新周期以来经过的时间并校准l
writeText
仅当传递的时间足够大时才可用。
Date.now()
提供当前时间。好的,谢谢。我会尝试一下,稍后再回来。@Flappix还使用整个更新方法更新了文章