JSFiddle - React, Tailwind, and code Playground
HTML
<div class="world" id="theWorld">
<div id="player"> </div>
</div>
<br />
<br />
<br />
<br />
<div id="logKeys">result </div>
<br />
<div id="logPos">result </div>
CSS
body
{
width:500px;
height:auto;
position:absolute;
left:30%;
}
.world {
width:400px;
height:300px;
background-color:red;
z-index:-1;
}
#player
{
position:absolute;
width:80px;
height:80px;
top:70%;
background-color:blue;
}
JavaScript
(function ($) {
$.Player = function (element, world_elem) {
this.playerElement = (element instanceof $) ? element : $(element);
this.movmentSpeed = 10;
this.width = $(element).width();
this.height = $(element).height();
this.worldWidth = $(world_elem).width();
this.worldHeight = $(world_elem).height();
this.xPos = 0;
this.yPos = 0;
this.allowedToJump = true;
this.movingLeft = false;
this.movingRight = false;
this.jumping = false;
this.golbalMsg = "";
};
$.Player.prototype = {
InitEvents3: function () {
var that = this;
$(document).keydown(function (e) {
var key = e.which;
console.log(key);
switch (key) {
case 39:
that.movingRight = true;
that.movingLeft = false;
that.jumping = false;
break;
case 37:
that.movingLeft = true;
that.movingRight = false;
that.jumping = false;
break;
case 38:
that.jumping = true;
break;
}
if (that.movingRight) {
if (that.jumping) {
that.jumpUp(10);
}
that.moveRight(300, 10);
}
if (that.movingLeft) {
if (that.jumping) {
that.jumpUp(10);
}
that.moveLeft(10);
}
logKeys(that.movingLeft, that.movingRight, that.jumping);
logPosistions(that.xPos, that.yPos);
});
this.playerElement.css({
left: this.xPos,
top:...