Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / MOving tile by tile smoothly

Author
Message
Fency Federejshn
16
Years of Service
User Offline
Joined: 8th Dec 2007
Location:
Posted: 11th Dec 2007 08:57
Hello, I've started making my tile game and the problem I am currently Facing is tile movement, i want my player to move on a 40x40 tile grid
in other words tile by tile but not just jumping tile by tile but smooth moving.

that means that if i press the say, left key my sprite moves left but when i realise my left key and my sprite isn't centerd on the tile it moves a bit forvard.

this is the code for the right arrow key:

+ Code Snippet

if ( dbKeyState ( 205 ) )
{
x=x+5;
dbSprite ( 2, x, 50, 1);
}
else
if (x%40!=0)
x=x+5;



this code works perfectly and does just what i want but the problem is when i combine all of the arrow key comands it messes up the movement... cause when right is not pressed left MIGHT be pressed but right still STOPS MY SPRITE!!!!!!!!

can anyone help!?

fency federejshn
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 12th Dec 2007 16:40 Edited at: 12th Dec 2007 16:41
I don't exactly what you're shooting for - but the first question I would have would be: Do you want multiple keys to work at same time or not. e.g. Can guy move up and left at same time or no?

This would dictate how I set up the logic to move the guy.

If you need one direction at a time - you should


(I haven't see elseif syntax - so I nest my stuff - no costs involved in doing it I'm aware of)

If you need simultaneous (Up + left) - then this would be individual "if's" generally speaking but depending on how your animation works - rules to your dude movement etc - this might need to be a little more complex... but genrally speaking - for simulateous "controls" try something along the lines of


But you might want to add logic (a mix of the two) to prevent say guy going forward and backward at same time.

I don't know if this helps you - but you basically need to play with how your logic is falling through. Hope this helps a little - maybe sparks a trial and error session where you can iron it out.

[edit] Spelling [/edit]

jasuk70
21
Years of Service
User Offline
Joined: 3rd Dec 2002
Location: Hemel Hempstead
Posted: 12th Dec 2007 17:53
If I get what you are trying to do.

Maybe have a variable that say direction e.g.:



The key check routine will make sure the direction variable is 0 before setting it to a direction

In your main loop, have a switch style statement that will look at the direction variable, if it is set to a direction it will move the sprite one step in that direction. The routine will then check if it is in the correct spot in that grid position, If it is then it will set the direction variable to 0 causing the sprite to stop and the key routine will be able to set the direction again.

If you want free movement (I.e. not needing to move to a the centre of each square) then just take the direction check out of the key routine and you will be able to move freely around.

Make sure you check diagonals with 2 keys if you intend to use it.

E.g. Upkey is pressed and LeftKey is pressed then direction = 1
DownKey is pressed and RightKey is pressed then direction = 8

Hope it helps.

Jas

----
"What is this talk of 'release'? Klingons do not'release' software. It escapes leaving a bloody trail of developers and quality assurance people in its wake!"
Sephnroth
22
Years of Service
User Offline
Joined: 10th Oct 2002
Location: United Kingdom
Posted: 12th Dec 2007 20:05
Tile engines are a lost art, but one of my favourite subjects! However, I havnt written one in nearly 2 years so excuse any errors

Render your tile grid as normal. Usually this would be running through your array/grid with two "for" loops for the x and y and pasting your tile at x * tile_width, y * tile_width;

to implement a smooth scroll you want to offset it by the player position so make sure your tile renderer has access one way or another to the players world x and y position and change your tile paste to:

PasteTile(tileid, (x * tile_width) - (playerx % tilewidth), (y * tile_height) - (playery % tile_height));

or something simular maybe the - should be a +, i cant remember, but this should give you a smooth scroll by offseting the whole map by a maxium of one tile depending on the players position! However, to avoid black space appearing you must draw an extra row and collum all around the screen, else the scroll will show gaps on the edges!

Hope that helps.

Login to post a reply

Server time is: 2024-11-19 20:34:51
Your offset time is: 2024-11-19 20:34:51