简体   繁体   English

HTML5游戏jump()函数无法正常运行

[英]HTML5 Game jump() function not working properly

I'm new with canvas. 我是帆布新手。 I'm a student currently developing a Runner Game in canvas. 我是一名学生,目前正在开发画布上的亚军游戏。 I have the function jump() , that is called when the mousedown() event occurs; 我有功能jump() ,当mousedown()事件发生时会调用该函数;

function jump(){
    img= new Image();
    //is_jump=1;
    h.clearRect(20,430,170,250);
    img.onload= function(){
        //h.drawImage(img,mx,my,190,280,120,410,190,350);
        h.drawImage(img,mx,my,190,350);
        //mX--;
        //mY--;
    }
    img.src= "mario-Copy.png";
    h.clearRect(mx,my,190,350);
    //for(i=mx;i<130;i+=7)
    //{
    if(mx<110&&my<480){
        mx=(mx+5);
        my=(my-15);
        //setTimeout(h.drawImage(img,mx,my,190,350),100000/5);
        h.drawImage(img,mx,my,190,350);
    }else{
        mx=60;
        my=410;
    }
}

The problem is that the jumping character is not working properly, it jumps slowly and not clear the previous jumping state image while moving on. 问题在于,跳跃角色无法正常工作,它会缓慢跳跃并且在继续前进时无法清除先前的跳跃状态图像。

So help me out so my animation works properly and smoothly. 因此,请帮助我,使我的动画正常且流畅地工作。

The goal is to make the character jump by click. 目的是使角色通过单击跳转。

I think the problem is you're clearing and redrawing in the same function of jump. 我认为问题在于您正在使用同一跳转功能清除和重绘。 what I would do is creat a seperate draw() and clear() and put those inside an setinterval and have that run. 我要做的是创建一个单独的draw()和clear()并将它们放入setinterval中并运行。 Then, all your jump function needs to do is update the position of your object, which you can either do directly by having the position being drawn changed by your jump(), or have a seperate update() function before draw to edit those values. 然后,您所有的跳转功能需要做的就是更新对象的位置,您可以直接通过jump()更改要绘制的位置来直接执行此操作,也可以在绘制之前使用单独的update()函数来编辑这些值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM