Advertisement
nPhoenix

[ARAMIS] - Engine de movimento u.U

Jun 30th, 2012
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. window.onload = function () {
  2.     player = {
  3.         x: function () {
  4.             return parseInt(document.getElementById("sprite").style.left);
  5.         },
  6.         y: function () {
  7.             return parseInt(document.getElementById("sprite").style.top);
  8.         },
  9.         obj: function () {
  10.             return document.getElementById("sprite");
  11.         }
  12.     };
  13.     speedWay = 2;
  14.     onkeydown = function (x) {
  15.         if (x.keyCode == 37) {
  16.             player["position"] = "left";
  17.             player.obj().style.backgroundPosition = "-0px -211px";
  18.             setTimeout(function () {
  19.                 player.obj().style.backgroundPosition = "-0px -182px";
  20.             }, 300);
  21.             player.obj().style.left = (player.x() - speedWay) + "px";
  22.             return false;
  23.         };
  24.  
  25.         if (x.keyCode == 38) {
  26.             player["position"] = "up";
  27.             player.obj().style.backgroundPosition = "-0px -151px";
  28.             setTimeout(function () {
  29.                 player.obj().style.backgroundPosition = "-0px -91px";
  30.             }, 300);
  31.             player.obj().style.top = (player.y() - speedWay) + "px";
  32.             return false;
  33.         };
  34.  
  35.         if (x.keyCode == 39) {
  36.             player["position"] = "right";
  37.             player.obj().style.backgroundPosition = "-0px -297px";
  38.             setTimeout(function () {
  39.                 player.obj().style.backgroundPosition = "-0px -269px";
  40.             }, 300);
  41.             player.obj().style.left = (player.x() + speedWay) + "px";
  42.             return false;
  43.         };
  44.  
  45.         if (x.keyCode == 40) {
  46.             player["position"] = "down";
  47.             player.obj().style.backgroundPosition = "-0px -29px";
  48.             setTimeout(function () {
  49.                 player.obj().style.backgroundPosition = "-0px -0px";
  50.             }, 300);
  51.             player.obj().style.top = (player.y() + speedWay) + "px";
  52.             return false;
  53.         };
  54.         if (x.keyCode == 107) {
  55.             speedWay = 5;
  56.             return false;
  57.         };
  58.  
  59.         if (x.keyCode == 109) {
  60.             speedWay = 2;
  61.             return false;
  62.         };
  63.     };
  64.  
  65.     setInterval(function () {
  66.         if (player.y() > 329 || player.y() == 329) {
  67.             player.obj().style.top = (player.y() - speedWay) + "px";
  68.         };
  69.         if (player.x() > 514 || player.x() == 514) {
  70.             player.obj().style.left = (player.x() - speedWay) + "px";
  71.         };
  72.         if (player.y() < 0 || player.y() == 0) {
  73.             player.obj().style.top = (player.y() + speedWay) + "px";
  74.         };
  75.         if (player.x() < 0 || player.x() == 0) {
  76.             player.obj().style.left = (player.x() + speedWay) + "px";
  77.         };
  78.         if (player.x() > 286 && player.x() < 320 && player.y() > 280 && player.y() < 325) {
  79.             if (player.position == "down") {
  80.                 player.obj().style.top = (player.y() - speedWay) + "px";
  81.             };
  82.             if (player.position == "up") {
  83.                 player.obj().style.top = (player.y() + speedWay) + "px";
  84.             };
  85.             if (player.position == "right") {
  86.                 player.obj().style.left = (player.x() - speedWay) + "px";
  87.             };
  88.             if (player.position == "left") {
  89.                 player.obj().style.left = (player.x() + speedWay) + "px";
  90.             };
  91.         };
  92.     }, 0);
  93. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement