Note that there are some explanatory texts on larger screens.

plurals
  1. POHTML5 Canvas drawing image at random
    primarykey
    data
    text
    <p>I'm quite a noob when it comes to programming, so please bear with me and point out any additional info that I need to provide. I'm making a game with Javascript and HTML5 canvas. At the current stage, I call two images and assign them to objects. It uses a basic loop that runs through update and render functions, and their subfunctions.</p> <pre><code>var player = {} player.truck = new Image(); player.truck.src = "truck.png"; var missile = {} missile.rocket = new Image(); missile.rocket.src = "missile2.png" </code></pre> <p>Both are manipulated through separate render functions who's variables are manipulated by update functions and who's variables are in turn manipulated by keyPress events. At refresh, the player image displays and behaves just fine - but the missile one displays completely at random. </p> <p>Check keys: </p> <pre><code>function checkKeys() { if (keyPressList[37]==true) { ConsoleLog.log("pressed left ") player.moveX = player.x -= 3; } if (keyPressList[39]==true) {ConsoleLog.log("pressed right") player.moveX = player.x += 3; } if (keyPressList[32]==true) {ConsoleLog.log("space pressed") shotFired=true; } } </code></pre> <p>Update Missile (subfunction of update()):</p> <pre><code>function updateMissiles(){ if (shotFired==false){ missile.x = player.x + 68; missile.y = 521; }else if(shotFired==true &amp;&amp; currentGameStateFunction==gameStatePlayLevel){ missile.y -= 5; } } </code></pre> <p>Render Missile (subfunction of render()):</p> <pre><code>function renderMissiles(x,y){ context.drawImage(missile.rocket,missile.x,missile.y); } </code></pre> <p>EDIT: I'm currently multitasking, so I uploaded the whole thing onto www.techgoldmine.com</p>
    singulars
    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