...

View Full Version : Game Script- up and down mark



tomusxs
05-06-2008, 05: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.

:)

shyam
05-06-2008, 07:19 PM
because style does not have an attribute called offsetTop document.getElementById("divek").offsetTop will give you the top of the element (its readonly) but, to actually move it you gotta use the style's top attribute like this document.getElementById("divek").style.top = ...

tomusxs
05-06-2008, 07:21 PM
because style does not have an attribute called offsetTop document.getElementById("divek").offsetTop will give you the top of the element (its readonly) but, to actually move it you gotta use the style's top attribute like this document.getElementById("divek").style.top = ...

I've got hard days actually-_- Stupid mistake:)) Thank you so much for help!

tomusxs
05-07-2008, 03:58 PM
http://www.bth.cnet.pl/jsgame2.html


So okey- everything is working fine... BUT


I've got another problem: something sie working on 10&#37; in IE , and in Opera 90% works properly. I do not have idea what I have to do.


I want an alert when Offset of first object is the same as Offset in second object. So I wrote:


if(divek.offsetLeft == kwadracik1.offsetLeft){
alert("ble")


<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);



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.top=divek.offsetTop +10}

if ((character=="W")) {


document.getElementById("divek").style.top=divek.offsetTop -10}
if(divek.offsetLeft == kwadracik1.offsetLeft){
alert("ble")


}

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.top=divek.offsetTop
}
</script><body onkeyup="doSomething()" />

<html>
<head>
<script language="javascript">

</script>
</head>


So... If I turn this on IE alert is working, but animation is not working actually. If I put it into Opera I can see animation, but when I put first object on second object alert is not working.

What I have to do with it now? I don't know actually what is the reason. Is always problem with Internet Browsers with JavaScript?

Please, help me!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum