Sunday, 2 June 2013

Game not checking for keyboard input?

Game not checking for keyboard input?

I have tried to explain as much as I can with commented code also. Working example: http://www.taffatech.com/Source.js -Code is organised by commenting html game: http://www.taffatech.com/DarkOrbit.html I have an Object called Player1 that has the following information:
function Player()  //Object
{

//////Your ships values
this.PlayerHullMax = 1000;
this.PlayerHull = 1000;
this.PlayerShieldMax = 1000;
this.PlayerShield = 347;
this.Experience = 2684;
this.Speed = 2; //should be around 2 pixels every-time draw is called by interval,     directly linked to the fps global variable
////////////

///////////flags
 this.isUpKey = false;
 this.isDownKey = false;
  this.isLeftKey = false;
this.isRightKey = false;
/////////////


////Pick Ship
this.type = "Cruiser";
this.srcX = ShipSrcXPicker(this.type);
this.srcY = ShipSrcYPicker(this.type);
this.drawX = ShipdrawXPicker(this.type);
this.drawY = ShipdrawYPicker(this.type);
 this.playerWidth = ShipWidthPicker(this.type);
this.playerHeight = ShipHeightPicker(this.type);
////


}

Player.prototype.draw = function()
{
ClearShipsCanvas();
ctxShips.globalAlpha=1;
this.checkKeys(); //must before draw pic to canvas because you have new coords now from the click
ctxShips.drawImage(spriteImage,this.srcX,this.srcY,this.playerWidth,this.playerHeight,thi         s.drawX,this.drawY,this.playerWidth,this.playerHeight);

 };

Player.prototype.checkKeys = function() //these functions are in the PLayer class
 {


    if(this.isUpKey == true)//if true //WARNING this method is not being called when up is clicked
{

 this.drawY -= this.Speed;

}

if(this.isRightKey == true)
{
  this.drawX += this.Speed;
}

if(this.isDownKey == true)
{
  this.drawY += this.Speed;
}

if(this.isLeftKey == true)
{
  this.drawX -= this.Speed;
}
};
I am trying to change the x y variables of the Player1 object using the keyboard buttons so when I redraw every interval it will move the Object on screen
    $("#canvasXY").keydown(function (e) {

            checkKeyDown(e);


    });

        $("#canvasXY").keydown(function (e) {

           checkKeyUp(e);


    });

}

function checkKeyDown(e)
{
alert("UP key is released");  //this never happens so it don't get this far!
var KeyID = e.KeyCode || e.which;
if (KeyID === 38 || KeyID === 87)  //up and w keyboard buttons
 {

 Player1.isUpkey = false;
 e.preventDefault(); //webpage dont scroll when playing
}

if (KeyID === 39 || KeyID === 68)  //right and d keyboard buttons
{
Player1.isRightkey = false;
e.preventDefault(); //webpage dont scroll when playing
}

if (KeyID === 40 || KeyID === 83)  //down and s keyboard buttons
{
Player1.isDownkey = false;
e.preventDefault(); //webpage dont scroll when playing
}

if (KeyID === 37 || KeyID === 65)  //left and a keyboard buttons
 {
Player1.isLeftkey = false;
e.preventDefault(); //webpage dont scroll when playing
}



}

function checkKeyUp(e)
{



var KeyID = e.KeyCode || e.which;
if (KeyID === 38 || KeyID === 87)  //up and w keyboard buttons
{

Player1.isUpkey = true;
e.preventDefault(); //webpage dont scroll when playing

}

 if (KeyID === 39 || KeyID === 68)  //right and d keyboard buttons
{
 Player1.isRightkey =

No comments:

Post a Comment