Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    May 2008
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Game Script- up and down mark

    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:
    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.

    Last edited by tomusxs; 05-06-2008 at 05:48 PM.

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    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 = ...
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    New Coder
    Join Date
    May 2008
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by shyam View Post
    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!

  • #4
    New Coder
    Join Date
    May 2008
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    Code:
    if(divek.offsetLeft == kwadracik1.offsetLeft){
    alert("ble")
    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);
    	
    
    
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •