www.webdeveloper.com
Results 1 to 10 of 10

Thread: This is not working. Why?

  1. #1
    Join Date
    Mar 2012
    Posts
    3

    Angry This is not working. Why?

    Code:
    <html>
    <body onkeydown="keydown(event)">
    <script type="text/javascript">
    event=new Object();
    player=new Object();
    player.x=0;
    player.y=0;
    
    function keydown(e)
    {
    
    
    if(window.event) // IE8 and earlier
    	{
    	event.type = "keydown";
            event.key  = e.keyCode;
    	}
    else if(e.which) // IE9/Firefox/Chrome/Opera/Safari
    	{
    	event.type = "keydown";
            event.key  = e.which;
    	}
    
    ///////////////////////////////////////////////////
    
    document.getElementById("keyp").innerHTML=event.key;
    switch(event.key)
    {
    case 38:
    	player.y-=1; break;
    case 40:
    	player.y+=1; break;
    case 37:
    	player.x-=1; break;
    case 39:
    	player.x+=1; break;
      
    }
    document.getElementById("px").innerHTML=player.x;
    document.getElementById("py").innerHTML=player.y;
    }
    
    </script>
    
    <div id="keyp">
    Press a key in yours keyboard.
    </div>
    <div>
    Current X: <div id="px"></div>
    Current Y: <div id="py></div>
    </div>
    </body>
    </html>
    I am doing a game. First, I am debugging the events, so I can proceed with drawing to a canvas.

    This code is not working. I've tried changing both javascript and HTML, with no sucess. The X displays correctly, but the Y doens't display at all. Why?

  2. #2
    Join Date
    May 2005
    Location
    Dirty Jersey
    Posts
    1,400
    what errors are you getting? what browser are you testing with?

    1. If you reply to my post, and your reply would then appear directly beneath my post, DON'T QUOTE MY ENTIRE POST!!! IT'S REDUNTANT!!! IT'S ASININE!!!! IT'S REDUNDANTLY ASININE!!!!! DON'T DO IT!!!!
    2. jQuery extends the functionality of JavaScript. If you don't know JavaScript, give up on that jQuery script and learn JavaScript. You'll save yourself a lot of frustration, I promise.
    3. Use the [code][/code] tags. Otherwise, you may be left wondering why no one responded to your eyesore of a thread.


  3. #3
    Join Date
    Mar 2012
    Posts
    202
    just a guess but try replacing

    Code:
    case 38:
    	player.y-=1; break;
    case 40:
    	player.y+=1; break;
    case 37:
    	player.x-=1; break;
    case 39:
    	player.x+=1; break;
    with

    Code:
    case 38:
    	player.y--; break;
    case 40:
    	player.y++; break;
    case 37:
    	player.x--; break;
    case 39:
    	player.x++; break;

  4. #4
    Join Date
    Jan 2011
    Posts
    117
    what do you mean by
    Code:
    event.type = "keydown";
    ?!!

  5. #5
    Join Date
    Mar 2012
    Posts
    202
    Quote Originally Posted by Troy III View Post
    what do you mean by...
    That is probably the problem, those lines should be removed because the event "type" is already known.

  6. #6
    Join Date
    Jan 2011
    Posts
    117
    in fact it should rise an error causing the function to fail -it's not allowed to write to event properties.

  7. #7
    Join Date
    Mar 2012
    Posts
    202
    Quote Originally Posted by Troy III View Post
    in fact it should rise an error causing the function to fail -it's not allowed to write to event properties.
    That is also correct, lol. I'm thinking more could have been done to self-troubleshoot. If not back to coding basics. The whole defining variables thing may be misunderstood by the looks of it
    Last edited by Gray1989; 03-25-2012 at 11:28 PM.

  8. #8
    Join Date
    Mar 2012
    Posts
    3
    Oh, yeah, I am stupid. Thanks for the help. Creating the 'event.type' was unecessary.

    But the code should work, what's wrong at all? The syntax seems correct...

    Sorry, this is my first try at reaching the OO part of Javascript.

  9. #9
    Join Date
    May 2005
    Location
    Dirty Jersey
    Posts
    1,400
    what errors are you getting? what browser are you testing with?

    1. If you reply to my post, and your reply would then appear directly beneath my post, DON'T QUOTE MY ENTIRE POST!!! IT'S REDUNTANT!!! IT'S ASININE!!!! IT'S REDUNDANTLY ASININE!!!!! DON'T DO IT!!!!
    2. jQuery extends the functionality of JavaScript. If you don't know JavaScript, give up on that jQuery script and learn JavaScript. You'll save yourself a lot of frustration, I promise.
    3. Use the [code][/code] tags. Otherwise, you may be left wondering why no one responded to your eyesore of a thread.


  10. #10
    Join Date
    Mar 2012
    Posts
    202
    Quote Originally Posted by Davichococat View Post
    Oh, yeah, I am stupid. Thanks for the help. Creating the 'event.type' was unecessary.

    But the code should work, what's wrong at all? The syntax seems correct...

    Sorry, this is my first try at reaching the OO part of Javascript.
    I think you may be missing something easy. Maybe switch(event.keyCode). Can you repost what you have so far

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles