在我的Html帆布游戏中滞后

时间:2017-03-08 20:22:07

标签: javascript html5 canvas lag

我开始使用Html画布和JavaScript制作视频游戏:你是外星人必须射杀其他红色外星人。如果你靠近外星人(敌人),敌人开始向你的方向移动。现在,如果玩家靠近敌人,那就在另一个敌人附近(但没有那么多看到玩家)游戏开始滞后。

这是距离的函数并移动到函数:

this.moveto = function(vec,vel){

    distX   = vec.x - vec.offsetx - this.x
    distY   = vec.y - vec.offsety - this.y

    dist    = Math.sqrt(distX*distX + distY*distY)
    distX /= dist
    distY /= dist
    i+= 1
    if(dist >= 2){
    this.x += (distX/100)*vel
    this.y += (distY/100)*vel
    }

}

this.dist = function(vec){

    dist2X  = vec.x - vec.offsetx - this.x
    dist2Y  = vec.y - vec.offsety - this.y

    dist2   = Math.sqrt(dist2X*dist2X + dist2Y*dist2Y)

    return(dist2)

}

这就是它的用法:

for(i = 0; i < this.enemies.length; i++){

        this.enemies[i].update()

        if(this.enemies[i].dist(this.player) > 100){
            this.enemies[i].move()
        }else{

            this.enemies[i].moveto(this.player,30)

        }

        if(this.enemies[i].lives <= 0){

            this.enemies.splice(i,1)

        }

    }

(“敌人”是一个有5个敌人的阵列) 这是敌人.move():

this.vector = new Vector(x,y,534/8,1517/8)

this.vector.move = function(){

    if(this.passi <= 2){ //1

        game.rspe.play()

        if(game.level.tick){

            this.IndexX++;

        }

        this.currentImage = game.enemyl
        this.x-=3;

        if (this.IndexX > 10){

            this.passi++
            this.IndexX = 0;

        }

    }else if(this.passi > 2 && this.passi <= 5){  //2

        game.rspe.play()

        if(game.level.tick){
            this.IndexX++;  
        }

        this.currentImage = game.enemyr
        this.x+=3;

        if (this.IndexX > 10){

            this.IndexX = 0;
            this.passi++

        }

    }else{

        this.passi = 0

    }

}

return(this.vector)

这是git存储库:https://github.com/Lollipopper/A1bKJBABJQ-W6-hi-jH-

0 个答案:

没有答案