styke
styke

Reputation: 2174

HTML5 Canvas drawing image at random

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.

var player = {}
player.truck = new Image();
player.truck.src = "truck.png";
var missile = {}
missile.rocket = new Image();
missile.rocket.src = "missile2.png"

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.

Check keys:

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;
    }
}

Update Missile (subfunction of update()):

function updateMissiles(){
    if (shotFired==false){
        missile.x = player.x + 68;
        missile.y = 521;
    }else if(shotFired==true && currentGameStateFunction==gameStatePlayLevel){
        missile.y -= 5;
    }
}

Render Missile (subfunction of render()):

function renderMissiles(x,y){
    context.drawImage(missile.rocket,missile.x,missile.y);
}

EDIT: I'm currently multitasking, so I uploaded the whole thing onto www.techgoldmine.com

Upvotes: 0

Views: 1000

Answers (1)

PAULDAWG
PAULDAWG

Reputation: 790

When does shotfired ever get reset to false? Could be that once you fire, it just keeps firing at the updateMissiles().

Upvotes: 1

Related Questions