tomusxs
05-06-2008, 04:31 PM
Hello!
It is my first post on CodingForums- I have been watching this forum since I started learning how to programm . Now I'm writing a game in Java Script- I started learning this language few days ago. I have a small problem with offsets:
My game code:
<script type="text/javascript">
function doSomething(e) {
var code;
if (!e) var e = window.event;
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
var character = String.fromCharCode(code);
var offsetY=window.event.offsetY
if ((character=="D")) {
document.getElementById("divek").style.left=divek.offsetLeft +10}
if ((character=="A")) {
document.getElementById("divek").style.left=divek.offsetLeft -10}
if ((character=="S")) {
document.getElementById("divek").style.offsetTop=divek.offsetTop +5
}
function pokaz(event){
document.f.x.value=divek.offsetLeft +10
document.f.y.value=divek.offsetTop +10
}
document.getElementById("divek").style.left=divek.offsetLeft
document.getElementById("divek").style.offsetTop=divek.offsetTop +5
}
</script><body onkeyup="doSomething()" />
<html>
<head>
<script language="javascript">
</script>
</head>
<div id="divek" style="position:absolute;"><img src=obrazek.png></div>
<form name="f">
<form>
</body>
</html>
As you can see here: www.bth.cnet.pl/jsgame.html when you push button :"D" or "S" on you keyboard the mark will go to the right side or to the left side- I wrote it in offsetLeft. But the problem is with up and down key- when I put offsetTop to the "W" button nothing is happening. I don't know what is wrong with this OffsetTop(I tried Bottm also). Msybe you will know what I have to change in my code? OffsetTop is not working- maybe something else will work in this game?
Please, I need your help;)
P.S I'm using IE & Opera.
:)
It is my first post on CodingForums- I have been watching this forum since I started learning how to programm . Now I'm writing a game in Java Script- I started learning this language few days ago. I have a small problem with offsets:
My game code:
<script type="text/javascript">
function doSomething(e) {
var code;
if (!e) var e = window.event;
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
var character = String.fromCharCode(code);
var offsetY=window.event.offsetY
if ((character=="D")) {
document.getElementById("divek").style.left=divek.offsetLeft +10}
if ((character=="A")) {
document.getElementById("divek").style.left=divek.offsetLeft -10}
if ((character=="S")) {
document.getElementById("divek").style.offsetTop=divek.offsetTop +5
}
function pokaz(event){
document.f.x.value=divek.offsetLeft +10
document.f.y.value=divek.offsetTop +10
}
document.getElementById("divek").style.left=divek.offsetLeft
document.getElementById("divek").style.offsetTop=divek.offsetTop +5
}
</script><body onkeyup="doSomething()" />
<html>
<head>
<script language="javascript">
</script>
</head>
<div id="divek" style="position:absolute;"><img src=obrazek.png></div>
<form name="f">
<form>
</body>
</html>
As you can see here: www.bth.cnet.pl/jsgame.html when you push button :"D" or "S" on you keyboard the mark will go to the right side or to the left side- I wrote it in offsetLeft. But the problem is with up and down key- when I put offsetTop to the "W" button nothing is happening. I don't know what is wrong with this OffsetTop(I tried Bottm also). Msybe you will know what I have to change in my code? OffsetTop is not working- maybe something else will work in this game?
Please, I need your help;)
P.S I'm using IE & Opera.
:)