Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- window.onload = function () {
- player = {
- x: function () {
- return parseInt(document.getElementById("sprite").style.left);
- },
- y: function () {
- return parseInt(document.getElementById("sprite").style.top);
- },
- obj: function () {
- return document.getElementById("sprite");
- }
- };
- speedWay = 2;
- onkeydown = function (x) {
- if (x.keyCode == 37) {
- player["position"] = "left";
- player.obj().style.backgroundPosition = "-0px -211px";
- setTimeout(function () {
- player.obj().style.backgroundPosition = "-0px -182px";
- }, 300);
- player.obj().style.left = (player.x() - speedWay) + "px";
- return false;
- };
- if (x.keyCode == 38) {
- player["position"] = "up";
- player.obj().style.backgroundPosition = "-0px -151px";
- setTimeout(function () {
- player.obj().style.backgroundPosition = "-0px -91px";
- }, 300);
- player.obj().style.top = (player.y() - speedWay) + "px";
- return false;
- };
- if (x.keyCode == 39) {
- player["position"] = "right";
- player.obj().style.backgroundPosition = "-0px -297px";
- setTimeout(function () {
- player.obj().style.backgroundPosition = "-0px -269px";
- }, 300);
- player.obj().style.left = (player.x() + speedWay) + "px";
- return false;
- };
- if (x.keyCode == 40) {
- player["position"] = "down";
- player.obj().style.backgroundPosition = "-0px -29px";
- setTimeout(function () {
- player.obj().style.backgroundPosition = "-0px -0px";
- }, 300);
- player.obj().style.top = (player.y() + speedWay) + "px";
- return false;
- };
- if (x.keyCode == 107) {
- speedWay = 5;
- return false;
- };
- if (x.keyCode == 109) {
- speedWay = 2;
- return false;
- };
- };
- setInterval(function () {
- if (player.y() > 329 || player.y() == 329) {
- player.obj().style.top = (player.y() - speedWay) + "px";
- };
- if (player.x() > 514 || player.x() == 514) {
- player.obj().style.left = (player.x() - speedWay) + "px";
- };
- if (player.y() < 0 || player.y() == 0) {
- player.obj().style.top = (player.y() + speedWay) + "px";
- };
- if (player.x() < 0 || player.x() == 0) {
- player.obj().style.left = (player.x() + speedWay) + "px";
- };
- if (player.x() > 286 && player.x() < 320 && player.y() > 280 && player.y() < 325) {
- if (player.position == "down") {
- player.obj().style.top = (player.y() - speedWay) + "px";
- };
- if (player.position == "up") {
- player.obj().style.top = (player.y() + speedWay) + "px";
- };
- if (player.position == "right") {
- player.obj().style.left = (player.x() - speedWay) + "px";
- };
- if (player.position == "left") {
- player.obj().style.left = (player.x() + speedWay) + "px";
- };
- };
- }, 0);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement