Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Can someone please help me with jump logic in this game I am making?

I created logic to allow my character to jump and double jump. Then I added logic to walk and jump which works in both directions. Then I added running logic to increase my dx and I can run and jump in the left direction only. I cannot run and jump in the right direction. There doesn't seem to be any code preventing this. I even tried swapping all the "left logic" with "right logic" to see if the problem would reverse... it doesn't. I am completely stumped.

Can someone please help me out?

Also if someone can recommend a better way to use the animation frames (sprite images) I'd appreciate it. It's pretty sloppy.
I am also wondering why the "this." keyword is being inconsistent. This isn't my first JS project but I gave in and just typed the word "corgi." wherever a "this." was needed so that I would stop getting console errors.

I plan on cleaning up all the "bad code" like the repetitive dog bed code later on. I am just working on the physics stuff right now.

Thanks everyone!
 
Last edited:
HTML:
<!DOCTYPE html>
<html>
<body>
    <canvas id="canvas" width="1200" height="700" style="border:2px solid grey"></canvas>
    <div style="display: none;">
        <img id="background" width="1200" height="700" src="grass background.jpg">
        <img id="dog_bed" width="1200" height="700" src="dog_bed.png">
        <img id="corgi_left" width="432" height="108" src="corgi_move_left.png">
        <img id="corgi_right" width="432" height="108" src="corgi_move_right.png">
    </div>
    <script>
        let test = function(){console.log('test')};
        const c = document.getElementById("canvas");
        const ctx = c.getContext("2d");
        ctx.fillStyle = "grey";
        ctx.fillRect(0, 0, canvas.width, canvas.height);
        let physics = {
            image: document.getElementById("background"),
            G: 0.25,
            drag: 0.99,
            frameLimit: 8,
            frameCount: 0,
            ground: 650
        };
        let beds = {
            image: document.getElementById("dog_bed"),
            width: 220,
            height: 147,
            qty: 3
        };
        let corgi = {
            width: 108,
            height: 108,
            BORDER_WIDTH: 1,
            SPACING_WIDTH: 1,
            row: 0,
            col: 4,
            x: (canvas.width/2) - 54,
            y: 592,
            dx: 4,
            dy: 0,
            jumpPower: -5,
            onGround: true,
            doubleJump: false,
            keyPresses: {},
            spritePosition: function(row, col) {
                return {
                    x: (corgi.BORDER_WIDTH +
                        col * (corgi.SPACING_WIDTH + corgi.width)
                        ),
                    y: (corgi.BORDER_WIDTH +
                        row * (corgi.SPACING_WIDTH + corgi.height)
                        )
                    }
            },
            image: document.getElementById("corgi_left"),
            movingLeft: true,
            applyGravity: function(){
                    this.dy += physics.G;
                    this.dy *= physics.drag;
                    this.y += this.dy;
                if(corgi.y + corgi.height >= physics.ground){
                    corgi.y = physics.ground - corgi.height;
                    this.dy = 0;
                    this.onGround = true;
                }else{this.onGround = false};
            },
            boundaries: function(){
                if(corgi.x <= 0){
                    corgi.x = 0;
                };
                if(corgi.x + corgi.width >= canvas.width){
                    corgi.x = canvas.width - corgi.width;
                };
            },
            move: function(){
                let moving = false;
                //move left
                if (corgi.keyPresses.a && !corgi.keyPresses.d){
                    corgi.x -= corgi.dx;
                    moving = true;
                    corgi.movingLeft = true;
                    corgi.image = document.getElementById("corgi_left");
                };
                //move right
                if (corgi.keyPresses.d && !corgi.keyPresses.a){
                    corgi.x += corgi.dx;
                    moving = true;
                    corgi.movingLeft = false;
                    corgi.image = document.getElementById("corgi_right");
                };
                //run
                if(corgi.keyPresses[' ']){
                    corgi.dx = 8; physics.frameLimit = 6;
                    if(!corgi.onGround){corgi.dx = 6; physics.frameLimit = 5};
                }else{corgi.dx = 4; physics.frameLimit = 8};
                //jump
                if(corgi.keyPresses.w && corgi.onGround){
                    corgi.dy = corgi.jumpPower;
                };
                //double jump
                if(!corgi.onGround && !corgi.keyPresses.w)
                    {corgi.doubleJump = true};
                if(corgi.keyPresses.w && corgi.doubleJump){
                    corgi.dy = corgi.jumpPower;
                    corgi.doubleJump = false;
                };
                corgi.boundaries();
                return moving;
            },
            animate: function(){
                physics.frames++;
                corgi.applyGravity();
                corgi.y += corgi.dy;
                //move corgi
                let moving = corgi.move();
                //update corgi sprite sheet frame
                if(moving){
                physics.frameCount++;
                    if(physics.frameCount >= physics.frameLimit && corgi.onGround){
                        corgi.col += 1;
                        physics.frameCount = 0;
                    };
                }
                else{
                    if(!corgi.movingLeft){corgi.col = 3};
                    if(corgi.movingLeft){corgi.col = 4};
                };
                if(!corgi.onGround){
                    if(!corgi.movingLeft){corgi.col = 2};
                    if(corgi.movingLeft){corgi.col = 1};
                };
                if(corgi.col === 4){corgi.col = 0};
                if(corgi.row === 0){corgi.row = 0};
                let position = corgi.spritePosition(corgi.row, corgi.col);
                ctx.clearRect(0, 0, canvas.width, canvas.height);
                ctx.drawImage(
                    physics.image,
                    0,
                    0
                );
                ctx.drawImage(
                    beds.image,
                    500,
                    200
                );
                ctx.drawImage(
                    beds.image,
                    600,
                    300
                );
                ctx.drawImage(
                    beds.image,
                    700,
                    400
                );
                ctx.drawImage(
                    corgi.image,
                    position.x,
                    position.y,
                    corgi.width,
                    corgi.height,
                    corgi.x,
                    corgi.y,
                    corgi.width,
                    corgi.height
                );
                requestAnimationFrame(corgi.animate);
            },
        };
        window.addEventListener('keydown', keyDownListener);
            function keyDownListener(event) {
            corgi.keyPresses[event.key] = true;
        };
        window.addEventListener('keyup', keyUpListener);
            function keyUpListener(event) {
            corgi.keyPresses[event.key] = false;
        };
        corgi.animate();
      
      
    </script>
</body>
</html>
 
Last edited by a moderator:
Hey there,
This may be because JavaScript will sometimes concatenate two variables to produce a string when using + or += if the data types for either of the variables is not a number. You may want to make sure these variables are numbers before adding them, by doing Number(corgi.x) += Number(corgi.dx);

Are there any errors in the console when you attempt to run and jump to the right?
 
Hey there,
This may be because JavaScript will sometimes concatenate two variables to produce a string when using + or += if the data types for either of the variables is not a number. You may want to make sure these variables are numbers before adding them, by doing Number(corgi.x) += Number(corgi.dx);

Are there any errors in the console when you attempt to run and jump to the right?
x, y, dx and dy are all given numerical values in the corgi object declaration. There are no errors in the console. The jump method will simply not execute. I have found no bugs yet other than the one explained. Thanks
 
Last edited:
I tried running the code you posted using random images in place of the referenced images, and all the moving functions seem to be working, is there something I'm missing?
 
I created logic to allow my character to jump and double jump. Then I added logic to walk and jump which works in both directions. Then I added running logic to increase my dx and I can run and jump in the left direction only. I cannot run and jump in the right direction. There doesn't seem to be any code preventing this. I even tried swapping all the "left logic" with "right logic" to see if the problem would reverse... it doesn't. I am completely stumped.

Can someone please help me out?

Also if someone can recommend a better way to use the animation frames (sprite images) I'd appreciate it. It's pretty sloppy.
I am also wondering why the "this." keyword is being inconsistent. This isn't my first JS project but I gave in and just typed the word "corgi." wherever a "this." was needed so that I would stop getting console errors.

I plan on cleaning up all the "bad code" like the repetitive dog bed code later on. I am just working on the physics stuff right now.

Thanks everyone!
Is there any reason why my scans are going off like there's no tomorrow? This is getting flagged as a trojan
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom