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 7 of 7

Thread: Game Making

  1. #1
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Game Making

    I'm trying to make a simple game in java, but I'm having an issue with the movement. This is some sample code. When you press the arrow keys the "Hello" div is supposed to move accordingly. My problem is that the div only moves 1 pixel in a direction no matter how many times I press an arrow key.
    Code:
    <html>
    <head>
    <script>
    window.onkeyup = function(e) {
       var key = e.keyCode ? e.keyCode : e.which;
       var y = 0;
       var x = 0;
       
       function keypress(){
       if (key == 38) //upKey
       {
    		y -= 1;
           document.getElementById('demoTest').style.marginTop=y+'px';//moves element up
       }
       else if (key == 40) //downKey
       {
    		y += 1;
           document.getElementById('demoTest').style.marginTop=y+'px';//moves element down
       }
       
       else if (key == 37) //rightKey
       {
    	   x -= 1;
           document.getElementById('demoTest').style.marginLeft=x+'px';//moves element to the right
       }
       if (key == 39) //leftKey
       {
    		x += 1;
            document.getElementById('demoTest').style.marginLeft=x+'px';//moves element to the left
       }
       }
       
    </script>
    </head>
    <body>
    <div id="demoTest" marginLeft="0px" marginTop="0px">
    <h1>Hello</h1>
    </div>
    </body>
    </html>

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    725
    Thanks
    35
    Thanked 132 Times in 123 Posts
    you are resetting x and y to 0 on every keyup.

  • Users who have thanked mrhoo for this post:

    damenhannah (06-18-2013)

  • #3
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Oh, that makes sense, but how would I fix that?

  • #4
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Figured it out, make x and y a global variable.

  • #5
    New Coder
    Join Date
    May 2013
    Posts
    45
    Thanks
    10
    Thanked 0 Times in 0 Posts
    If you want to make a game with moving elements, I'd recommend doing it with canvas.

  • #6
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,274
    Thanks
    28
    Thanked 271 Times in 265 Posts
    Quote Originally Posted by IdkWhatsRc View Post
    If you want to make a game with moving elements, I'd recommend doing it with canvas.
    I've played around with canvas recently and would have to recommend SVG instead; canvas has the serious drawback of being a raster format whereas SVG scales nicely to the viewport whatever its size.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #7
    New Coder
    Join Date
    May 2013
    Posts
    45
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Arbitrator View Post
    I've played around with canvas recently and would have to recommend SVG instead; canvas has the serious drawback of being a raster format whereas SVG scales nicely to the viewport whatever its size.
    Well, it depends on what you want to achieve. Usually, with a game, you want something that draws many elements very fast and canvas are the best for that. But sure, if your main concern is visual quality, then SVG are best.


  •  

    Tags for this Thread

    Posting Permissions

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