changing code to get it to work from a class?
Hey team
Kindly gave me this code for character movement
var root = this;
var player = root.player;
var keys = {};
root.main = function()
{
root.friction = 0.95;
root.keyForce = 50;
player.vX = 0;
player.vY = 0;
player.minVX = 20;
player.minVY = 20;
window.addEventListener("keydown", root.keyDownHandler);
window.addEventListener("keyup", root.keyUpHandler);
root.on("tick", root.tickHandler);
console.log("handled");
};
root.keyDownHandler = function(e)
{
keys[e.key] = true;
};
root.keyUpHandler = function(e)
{
delete keys[e.key];
};
root.tickHandler = function(e)
{
if (keys.a || keys.ArrowLeft)
player.vX -= root.keyForce;
else if (keys.d || keys.ArrowRight)
player.vX += root.keyForce;
if (keys.w || keys.ArrowUp)
player.vY -= root.keyForce;
else if (keys.s || keys.ArrowDown)
player.vY += root.keyForce;
player.vX *= root.friction;
player.vY *= root.friction;
player.x += player.vX * e.delta * 0.001;
player.y += player.vY * e.delta * 0.001;
root.rotate(player);
};
root.rotate = function(target)
{
target.rotation = Math.atan2(target.vY, target.vX) * 180 / Math.PI;
};
root.main();
So now I am trying (hard) to figure out how to alter this code (looking ulimately to alter the code in the asteroids example JS). What do I need to look for to change this code so it works from the game.js class file?
I know how to address the player (with this.root.player) but I cant seem to be able to work out where to put the variables and other functions? What search topic and I looking for to find how to do this?
Thanks guys I really appreciate your help
bundy
