Moving character and map with canvas+js

Asked

Viewed 841 times

1

Well I have a question, I would like to know how I could use the image in normal size, but that I can navigate it with the left and right directional, example, I have a canvas of size 968 wide and 643 height, and I have an image of 643 height and 2000 width(http://imgur.com/QnBufq4), how would I move a character(Sprite) within it and move it over the map? without it exceeding the mother image limit

2 answers

2


You need a camera or viewport system. Use the translate canvas, which already moves all objects drawn accordingly and we don’t need to keep calculating relative positions, and then draws everything with coordinates relative to the world (the image):

var iw = 2000; //Image width
var ih = 643; //Image height
var sw = 968; //Canvas width
var sh = 643; //Canvas height
var cw = 50; //Character width
var ch = 50; //Character height
var speed = 10; //Character speed

var img = new Image(iw, ih);
img.src = "http://i.imgur.com/QnBufq4.jpg";
var ctx = canvas.getContext("2d");
var pos = {x: 0, y: ih - 140};
var camera = {x: 0, y: 0}

function draw() {
 //Faz a esquerda da câmera começar meia tela antes do personagem, mas só se tiver imagem suficiente pra isso
 camera.x = Math.min(iw - sw, Math.max(0,pos.x - sw/2));
 //Reseta as transformações do canvas
 ctx.setTransform(1,0,0,1,0,0);
 //Limpa o canvas
 ctx.clearRect(0, 0, canvas.width, canvas.height);
 //Desloca o mundo inteiro, simulando uma câmera
 ctx.translate(-camera.x, -camera.y);
 //Desenha o fundo
 ctx.drawImage(img, 0, 0, sw, sh, 0, 0, iw, ih);
 //Desenha o personagem
 ctx.fillRect(pos.x, pos.y, cw, ch);

 window.requestAnimationFrame(draw);
}

document.addEventListener('keydown', function(e) {
 var key = e.which || e.key || e.keyCode;
 switch(key) {
  case 37: pos.x-=speed; break;
  case 39: pos.x+=speed; break;
 }
 pos.x = Math.max(0, Math.min(iw - cw,pos.x));
});

window.requestAnimationFrame(draw);
<canvas style="border: 1px solid black;" id="canvas" width="968" height="643">
</canvas>

And here, if you like, a more in-depth tutorial: Panning and scrolling background images using the canvas element

  • Exactly what I needed, thank you very much my friend, save my life, did not give a simple answer, you solved everything at once ^^

  • could you send me a zip? I’m not managing to apply the same in my code :P

-1

Good night, my dear. I am not giving you a solution in code, but I can tell you that the solution is not to move the character but the image behind using paralax.

an example of the functioning of paralax http://tableless.com.br/parallax-simples-com-jquery-e-css/

If you see a solution to this, I will assist you in the origin of the situation.

  • Not quite this, well I’ll give an example, say super mario, you will walk and will appear new obstacles etc but width and height and equal on the screen understand?

  • Are you using autotuning? I use auto tuning with javascript to fit the screen independent of resolution. I’m not sure I’m getting your thinking straight. But I have a game that I made of curiosity in case you want to consult http://atualgeek.com/game/

  • The game does not need since it is inside a div canvas, what I need and place the image inside the div( https://scontent-gru2-1.xx.fbcdn.net/v/t1.0-9/13442219_1794432477445121_7190000166730817106_n.jpg?oh=32f1f2dd77b6e6f085053bac902d20&oe=57C4CAB5 )

  • and put to move the map as the canvas is 940 wide and the image has to stay inside it would have 2000 understood, there with the directional pressing moving, giving the effect of movement to the character

  • I think I got it, would it show only 940px of the set initially and according to the character’s movement he explore until the px 2000? Would that be something? If it really is paralax that will help you, because the canvas and the character remain intact and only the image that moves according to the pressing of the key to the corresponding side.

  • exact, I didn’t understand how well I would use the paralax, but there is the however in the character, exactly explore the 2000 pixels but at the end the character tbm could go to the other end of the map, during the middle course etc.

  • https://www.youtube.com/watch?v=ivfnUiCVnBU

  • Yes, but I’m kind of a layman with jquery and javascript, I could do it without creating too many Ivis and dragging with the mouse?

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.