Skip to main content
gamedev.net
🔒 Locked

boundaries

Started by lemonstix 0 replies 600+ views
Original Post
lemonstix
lemonstix
I''m using someone else''s engine to make a very simple 2D game. I''m starting out by making the user move around using the keyboard. Unfortunately, the character has no boundaries --- it can exit the screen. I tried setting limits using the ''if'' statement, but the boundaries only work on the top and left side of the screen --- the user can still move past the right and bottom screen ends. Here''s what I have (remember, i''m calling functions from someone else''s engine) //user movement down.draw(x,y); if (m_key.keyDown(DIK_UP)) { y-=2; up.draw(x,y); } else if (m_key.keyDown(DIK_DOWN)) { y+=2; down.draw(x,y); } else if (m_key.keyDown(DIK_RIGHT)) { x+=2; right.draw(x,y); } else if (m_key.keyDown(DIK_LEFT)) { x-=2; left.draw(x,y); } //... //wall boundaries if (x > HORIZ_RES || x < 0) { x = -x; } else if (y > VERT_RES || y < 0) { y = -y; } //HORIZ_RES and VERT_RES are the #defines for screen size //x and y are the user''s current position. I know you can''t see the whole code, but do anyone know of possible reasons that this doesn''t work completely? lemonstix
Dreamforger
Dreamforger
You never thought this trough, did you?

1. moving accros 0: -x -> x, perfect
2. moving accros _RES: x (which is large) becomes -x which is smaller than 0, not good
3. why the elseif? simply wrong


if (x > HORIZ_RES) x = 2*HORIZ_RES - x;
else if( x < 0) x = -x;

if (y > VERT_RES) y = 2*VERT_RES - y;
else if (y < 0) y = -y;

also you should make sure that the boundaries don''t get exeeded too far


---------------------------
I may be getting older, but I refuse to grow up
I may be getting older, but I refuse to grow up

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.