首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HTML5画布字符跳转

HTML5画布字符跳转
EN

Stack Overflow用户
提问于 2013-04-13 05:22:42
回答 2查看 6.3K关注 0票数 2

我试着通过阅读这个教程来制作角色的动画:http://mrbool.com/html5-canvas-moving-a-character-with-sprites/26239。让角色向左转很容易(‘向右转’已经完成了)。但是如何让角色跳跃(带有动画)呢?我在想这样的事情:

代码语言:javascript
复制
    case 38:
        if (y + dy > HEIGHT){
            y += dy
        } 

    break;

...but它只是向上移动角色(没有动画)。有人能帮我吗?一些代码示例会很有用。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-13 05:57:11

您将获得如下跳转行为(使用本教程中的相同代码)

JSFiddle

代码语言:javascript
复制
var canvas;// the canvas element which will draw on
var ctx;// the "context" of the canvas that will be used (2D or 3D)
var dx = 50;// the rate of change (speed) horizontal object
var x = 30;// horizontal position of the object (with initial value)
var y = 150;// vertical position of the object (with initial value)
var limit = 10; //jump limit
var jump_y = y;
var WIDTH = 1000;// width of the rectangular area
var HEIGHT = 340;// height of the rectangular area
var tile1 = new Image ();// Image to be loaded and drawn on canvas
var posicao = 0;// display the current position of the character
var NUM_POSICOES = 6;// Number of images that make up the movement
var goingDown = false;
var jumping;

function KeyDown(evt){
    switch (evt.keyCode) {
        case 39:  /* Arrow to the right */
            if (x + dx < WIDTH){
                x += dx;
                posicao++;
                if(posicao == NUM_POSICOES)
                    posicao = 1;

                Update();
            }
            break;    
        case 38:
            jumping = setInterval(Jump, 100);
    }
}

function Draw() {      
    ctx.font="20px Georgia";
    ctx.beginPath();
    ctx.fillStyle = "red";   
    ctx.beginPath();
    ctx.rect(x, y, 10, 10);
    ctx.closePath();
    ctx.fill();   
    console.log(posicao);
}
function LimparTela() {
    ctx.fillStyle = "rgb(233,233,233)";   
    ctx.beginPath();
    ctx.rect(0, 0, WIDTH, HEIGHT);
    ctx.closePath();
    ctx.fill();   
}
function Update() {
    LimparTela();    
    Draw();
}

var Jump = function(){
    if(y > limit && !goingDown){
        y-=10;
        console.log('jumping: ' + y);
    } else{
    goingDown = true;
        y +=10;
        if(y > jump_y){
            clearInterval(jumping);
            goingDown = false;
        }

    }
}

function Start() {
    canvas = document.getElementById("canvas");
    ctx = canvas.getContext("2d");
    return setInterval(Update, 100);
}

window.addEventListener('keydown', KeyDown);
Start();
票数 2
EN

Stack Overflow用户

发布于 2013-04-13 05:30:09

这个问题没有一个正确的答案,除非你找到一个游戏设计库,否则也没有简单的答案。你的问题是,你是在瞬间移动角色以响应输入,但跳跃需要随着时间的推移而移动。你要么找到一个移动的精灵库--我没有特别推荐的库,但我肯定谷歌有几个--要么自己设置一些东西,每隔这么多毫秒运行一次,并更新角色的位置和某种速度变量。

编辑:看看这篇教程,我想到的最简单的解决方案就是把你的动画代码放在Update()里面,就像这样:

代码语言:javascript
复制
function Update() {
    LimparTela();
    Animate();
    Draw();
}

Animate()中,您应该跟踪角色的高度和垂直动量。如果动量为正,则将y位置略微增加,否则稍微减小y位置。无论哪种方式,都可以稍微减少一些动量。添加一些东西以防止角色穿过地板,如果角色在地板上,请使用up键将角色的动量设置为正。

请注意,这是一个令人难以置信的基本解决方案,但对于基本教程来说,它就可以完成这项工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15981062

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档