JavaScript Canvas Error (Code not working?)

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
Script47
Posts: 147
Joined: Thu Nov 21, 2013 6:11 pm

JavaScript Canvas Error (Code not working?)

Post by Script47 »

I have this error which I can't see, to fix, the picture should explain it more clearly.

This is the code I am using to check, tell me if you would need more. I will add upon request.

Code: Select all

if(player.x  > canvas.height - 9) {
        player.x = player.x - 15;
} else if(player.y > canvas.width - 9) {
        player.y = player.y - 15;
}
Thanks in advance for any help. :)
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: JavaScript Canvas Error (Code not working?)

Post by hallsofvallhalla »

if I understand what you are trying to do then you need to add on the right and bottom.

Code: Select all

if(player.x  > canvas.height - 9) 
{
        player.x = player.x - 15;
} 
if(player.y > canvas.width - 9) 
{
        player.y = player.y - 15;
}
if(player.x  > canvas.height + 9) 
{
        player.x = player.x + 15;
} 
 if(player.y > canvas.width + 9) 
{
        player.y = player.y + 15;
}
User avatar
Script47
Posts: 147
Joined: Thu Nov 21, 2013 6:11 pm

Re: JavaScript Canvas Error (Code not working?)

Post by Script47 »

Thanks for the reply, I already tried this, however I tried it again and still no luck. Any other ideas? :)
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: JavaScript Canvas Error (Code not working?)

Post by hallsofvallhalla »

I am not sure what you are trying to do. When the player reaches the end of the canvas they disappear?
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: JavaScript Canvas Error (Code not working?)

Post by hallsofvallhalla »

ah wait I think i see what you are trying to do

Code: Select all

if(player.x  > canvas.height - 9) 
{
        player.x = player.x - 15;
} 
if(player.y > canvas.width - 9) 
{
        player.y = player.y - 15;
}
if(player.x  < 10) 
{
        player.x = player.x + 15;
} 
 if(player.y <  9) 
{
        player.y = player.y + 15;
}
User avatar
Script47
Posts: 147
Joined: Thu Nov 21, 2013 6:11 pm

Re: JavaScript Canvas Error (Code not working?)

Post by Script47 »

Worked like a charm, thanks mate. What I don't understand is why doesn't it need the canvas.width/height variable? Is it because the X & Y are only checked on one side?
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: JavaScript Canvas Error (Code not working?)

Post by hallsofvallhalla »

on the top, left side of your canvas the x and y values are 0. So the width and height doesn't matter. You need to detect if the player is less than 10 as once you go left or above the canvas you get into the negatives.

If you want to make that look fancier I would base it off your key press instead.

Code: Select all

if (player.isDownKey && player.y < canvas.width - 5)
{
move player down
}
This way if the player is on the edge of the canvas and tries to push down it wont move but they can push up. It keeps the jumping out of the animation when they hit a wall.
User avatar
Script47
Posts: 147
Joined: Thu Nov 21, 2013 6:11 pm

Re: JavaScript Canvas Error (Code not working?)

Post by Script47 »

Ah cool, did not know that, thanks mate! :)
Post Reply

Return to “Advanced Help and Support”