Page 1 of 1
JavaScript Canvas Error (Code not working?)
Posted: Thu Nov 21, 2013 6:50 pm
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.

Re: JavaScript Canvas Error (Code not working?)
Posted: Thu Nov 21, 2013 7:37 pm
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;
}
Re: JavaScript Canvas Error (Code not working?)
Posted: Thu Nov 21, 2013 7:40 pm
by Script47
Thanks for the reply, I already tried this, however I tried it again and still no luck. Any other ideas?

Re: JavaScript Canvas Error (Code not working?)
Posted: Thu Nov 21, 2013 8:13 pm
by hallsofvallhalla
I am not sure what you are trying to do. When the player reaches the end of the canvas they disappear?
Re: JavaScript Canvas Error (Code not working?)
Posted: Thu Nov 21, 2013 8:15 pm
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;
}
Re: JavaScript Canvas Error (Code not working?)
Posted: Thu Nov 21, 2013 9:07 pm
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?
Re: JavaScript Canvas Error (Code not working?)
Posted: Thu Nov 21, 2013 10:25 pm
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.
Re: JavaScript Canvas Error (Code not working?)
Posted: Thu Nov 21, 2013 10:44 pm
by Script47
Ah cool, did not know that, thanks mate!
