Note that there are some explanatory texts on larger screens.

plurals
  1. PO2d html5 canvas collision, howto
    primarykey
    data
    text
    <p>As the title suggests, I am having trouble with object collision... I am currently working on a 2d Html5 canvas game using JavaScript. I know how to keep the "player" object from going outside the width/height of the game canvas, and i know how to do something when the player collides with an object (such as a power up or enemy or whatever) but i just don't know how to make a "solid" object meaning when the player hits the solid object, the player just stops, and cannot go through the solid object.</p> <p>This is what I have now (not all the code just what I feel is relevant, sorry if it's too much/too little.:</p> <pre><code> var canvasPlayer = document.getElementById('canvasPlayer'); var ctxPlayer = canvasPlayer.getContext('2d'); var canvasWalls = document.getElementById('canvasWalls'); var ctxWalls = canvasWalls.getContext('2d'); function checkKeyDown(e) { var keyID = (e.keyCode) || e.which; if (keyID === 38 || keyID === 87) { // up arrow OR W key if (!player1.isDownKey &amp;&amp; !player1.isLeftKey &amp;&amp; !player1.isRightKey) { player1.isUpKey = true; e.preventDefault(); } } if (keyID === 39 || keyID === 68) { //right arrow OR D key if (!player1.isDownKey &amp;&amp; !player1.isLeftKey &amp;&amp; !player1.isUpKey) { player1.isRightKey = true; e.preventDefault(); } } if (keyID === 40 || keyID === 83) {//down arrow OR S key if (!player1.isUpKey &amp;&amp; !player1.isLeftKey &amp;&amp; !player1.isRightKey) { player1.isDownKey = true; e.preventDefault(); } } if (keyID === 37 || keyID === 65) {//left arrow OR A key if (!player1.isDownKey &amp;&amp; !player1.isUpKey &amp;&amp; !player1.isRightKey) { player1.isLeftKey = true; e.preventDefault(); } } } Walls.prototype.draw = function (){ ctxWalls.drawImage(imgSprite,this.srcX,this.srcY,this.width,this.height,this.drawX,this.drawY,this.width,this.height); this.checkHitPlayer(); }; Walls.prototype.checkHitPlayer = function() { if (this.drawX &gt; player1.drawX &amp;&amp; this.drawX &lt;= player1.drawX + player1.width &amp;&amp; this.drawY &gt;= player1.drawY &amp;&amp; this.drawY &lt; player1.drawY + player1.height) { player1.isUpKey = false; player1.isDownKey = false; player1.isRightKey = false; player1.isLeftKey = false; } }; </code></pre> <p>This works... except when trying to go up or left, the player only moves maybe 2-3 pixels, so it takes 3 left or up arrows to go left or up. As well the player can move straight through the wall which is not what i want. Any help is much appreciated sorry if i included too much or not enough code. Oh, i also forgot to mention the game is a puzzle game, and I have it set-up so a player can only move one direction at a time until hitting a wall.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload