S
sylver
Hi to all,
There's a website that remake a Super Mario Bros Game here:
http://www.janis.or.jp/users/segabito/JavaScriptMaryo.html
The movements of the character (left, right, jump) are very smooth. I
tried to remake the code, but bumped into some lag issues.
-----------------Code-----------------------
<html>
<head>
<script>
function init(){
var x = document.getElementById('divvy');
x.addEventListener("onkeydown", test, false);
}
function checkOnKeyDown(event)
{
var x = document.getElementById('divvy');
if(event.keyCode == event.DOM_VK_LEFT){
//move left when press left arrow
x.style.left = (parseInt(x.style.left)-10)+"px";
}
if(event.keyCode == event.DOM_VK_RIGHT){
//move right when press right arrow
x.style.left = (parseInt(x.style.left)+10)+"px";
}
if (event.shiftKey){
//jump when press Shift key
x.style.top = (parseInt(x.style.top)-10)+"px";
}
}
function test(){alert('test');}
window.onkeydown = checkOnKeyDown;
</script>
</head>
<body onload="init();">
<div id="divvy" style="position:absolute; top:500px; left:
100px;">this is divvy</div>
</body>
</html>
-----------code ends--------------------------
Issues:
========
1) There's a pause (about 0.5 sec) when u press the keys to move.
2) the test() function is never called, even after I comment
"window.onkeydown = checkOnKeyDown;"
3)
When press SHIFT, div goes up.
When press SHIFT, then arrow keys, div move diagonally.
When press arrow keys then SHIFT, move sideways first, then move up
(guess this relates to 'bubbling' events?)
Please enlighten me on this matter.
Thanks.
There's a website that remake a Super Mario Bros Game here:
http://www.janis.or.jp/users/segabito/JavaScriptMaryo.html
The movements of the character (left, right, jump) are very smooth. I
tried to remake the code, but bumped into some lag issues.
-----------------Code-----------------------
<html>
<head>
<script>
function init(){
var x = document.getElementById('divvy');
x.addEventListener("onkeydown", test, false);
}
function checkOnKeyDown(event)
{
var x = document.getElementById('divvy');
if(event.keyCode == event.DOM_VK_LEFT){
//move left when press left arrow
x.style.left = (parseInt(x.style.left)-10)+"px";
}
if(event.keyCode == event.DOM_VK_RIGHT){
//move right when press right arrow
x.style.left = (parseInt(x.style.left)+10)+"px";
}
if (event.shiftKey){
//jump when press Shift key
x.style.top = (parseInt(x.style.top)-10)+"px";
}
}
function test(){alert('test');}
window.onkeydown = checkOnKeyDown;
</script>
</head>
<body onload="init();">
<div id="divvy" style="position:absolute; top:500px; left:
100px;">this is divvy</div>
</body>
</html>
-----------code ends--------------------------
Issues:
========
1) There's a pause (about 0.5 sec) when u press the keys to move.
2) the test() function is never called, even after I comment
"window.onkeydown = checkOnKeyDown;"
3)
When press SHIFT, div goes up.
When press SHIFT, then arrow keys, div move diagonally.
When press arrow keys then SHIFT, move sideways first, then move up
(guess this relates to 'bubbling' events?)
Please enlighten me on this matter.
Thanks.