forums

TNT Basic Forums > Programming Q&A
Problem with scrolling
< Last Thread     Next Thread >
Author
Thread        Post A Reply

12-09-2001 22:36

Posted by:
Jase

Click Here to Email Jase   Find more posts by Jase

I'm sorta new to pogramming. And i need some help with something.
You know how in some games you'll move the player around and then when it gets too close to the edge of the screen, the screen will scroll across in that direction. Well, i can't figure out how to do it.
Please help me.

12-10-2001 00:13

Posted by:
Socrates

Click Here to Email Socrates   Find more posts by Socrates

Here's how

The detail level of my answer kinda depends on how new you are to programming, and I'm not going to be very specific to TNT because I don't know it that well myself yet, but...

A player in a game will have a position on the map determined by an X and a Y coordinate (there is a Z coordinate for TNT maps too, but I assume you'll be wanting a flat map to begin with).

But a player will also have an X and Y coordinate relative to the screen, and this is where your problem lies.

These two sets of coordinates may not be the same. If your map is bigger than the screen, but you want the player to be able to move around the map and the screen then you need to keep track of both sets of coordinates.

Okay, now when your player moves right you'll want to make his X coordinate bigger (if this is above your head then I can try and explain further, but it might be better if you start with a simpler game).

For your game, most of the time you'll want to make both the screen X coordinate bigger and the map X coordinate bigger. When the player gets to the edge of the screen, you stop making the screen coordinate bigger, but keep making the map coordinate bigger. Then obviously when he reaches the dges of the map you stop making either bigger because he can't go any further.

To draw this, you draw the player by placing him on the screen using his screen coordinate. You scroll the map by subtracting the screen coordinate from the mapp coordinate and so the map will only scroll when the player isn't moving on the screen.

I know this is probably really confusing, but a more thorough explanation would require you to be a bit more specific about what you've done so far, and what kind of scrolling map you're using (is a it just a single big picture, or a TNT tiled map resource?).

My advice would be to start either with a map the same size as the screen or with a character who always stays in the middle of the screen. A lot of good games work like that, and it probably won't make much difference to your concept. If you get that going then I can help you with code to make it a bit more complex.

S

12-10-2001 01:26

Posted by:
Jase

Click Here to Email Jase   Find more posts by Jase

Thanks for your help so far, it's real helpful.
How do you place a sprite using it's screen co-ordinate.
Because i've gotten most of this to work, except when i get it to check where in the viewport the sprite is (to see how far from the edge it is), it actually checks how far the sprite is from the edge of the map.

So when the sprite gets a certain distance(say 240) say to the left, the viewport scrolls left and looks okay but keeps moving until i move the sprite back so that on the map it's less than 240.

12-10-2001 01:28

Posted by:
Jase

Click Here to Email Jase   Find more posts by Jase

Woops, i meant to the right in that last bit

12-10-2001 02:15

Posted by:
Socrates

Find more posts by Socrates

Ah...

Well you have two choices as far as I can see. One way would be to attach the sprite to the screen instead of to the map canvas, but that would be a bit messy since you probably want it to interact with sprites on the map.

When you start the game make sure the sprite is in the middle of the screen, then just keep track of his position whenever he walks around. Don't scroll the map with the sprite, only scroll it when he walks more than 240 pixels from the middle of the screen. When he hits 240 in screen coordinates, switch to map coordinates and scroll, then if he walks in the opposite direction, switch back to screen coordinates.

You don't actually need to move him relative to the screen, just remember where he is on the screen so you know when he reaches the edge. You know the eidth of the screen (640 I presume), so that will be the width of your viewport too. That means that the middle of the screen is always 320 pixels plus the position of the viewport (horizontally). So he's walking off the edge of the screen if his position is greater than viewportX + 320 + 240 (or whatever you wan the threshold to be. If that happens, you should move the sprite AND the viewport, but if he's between viewportX + 320 - 240 and viewport + 320 + 240, just move the sprite and don't scroll the screen. Remember that for the Y axis those values will be different.

If you do this, he will appear to move with the screen, but actually the sprite and the screen are just both moving at the same speed when he's near the edge.

Sorry that's a bit verbose but I hope you get the idea.

12-10-2001 02:20

Posted by:
Socrates

Find more posts by Socrates

Probably not the best time to say this but...

You know, it may be a bit annoying in the game if the screen only scrolls when the player walk up to the edge. How will the player be able to see the baddies right in front of him?

A number of games actually do this in reverse instead, the screen scrolls so that the player is nearer the opposite side of the screen to the direction he's moving, so he can see further ahead (That's tricky though cos it means scrolling when he turns round even if he's standing still).

Just a thought...

S

12-10-2001 02:43

Posted by:
Jase

Click Here to Email Jase   Find more posts by Jase

Thankyou!!

Thanks for your help!
I finally got the scroll thing working.

What i was trying to do was to have the guy walk around with out the screen moving and then have the screen move once he got a certain distance from the edge.

So that you could see where your walking but not have the screen continuously moving.

it works now, thanks

12-10-2001 09:51

Posted by:
Socrates

Find more posts by Socrates

Cool

Oh right, I'm with you. Glad it all works now.

S

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.