Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » making scrolling and character movement work..

This thread is locked; no one can reply to it. rss feed Print
making scrolling and character movement work..
red-dragon
Member #7,722
August 2006

Ok,
I can see much flack coming for this. But yeah, I still need help. I thought making a new thread was in order, since this code might get confused with the other I posted.

I have some code here. I've never done this before. I'll start slow. Variables/predefined processors etc. you should know:
#define WIDTH 640
#define HEIGHT 480
#define TILEW 32
#define TILEH 32
#define MAP_ACROSS 31
#define MAP_DOWN 33
#define MAPW MAP_ACROSS * TILEW
#define MAPH MAP_DOWN * TILEH
#define BOTTOM MAPH-HEIGHT
#define LEFT MAPW-WIDTH
int scrollx=LEFT, scrolly=BOTTOM;

1if(scrolly < 0) scrolly = 0;
2if(scrolly > BOTTOM) scrolly = BOTTOM;
3if(scrollx < 0) scrollx = 0;
4if(scrollx > LEFT) scrollx = LEFT;
5
6
7 //border/edge detection
8 if (link->x > SCREEN_W-22)
9 {
10 link->x = 0;
11 scrollx += SCREEN_W;
12 }
13 else if (link->x < 0)
14 {
15 link->x = SCREEN_W-22;
16 scrollx -= SCREEN_W;
17 }
18 else if (link->y > SCREEN_H-24)
19 {
20 link->y = 0;
21 scrolly += SCREEN_H;
22 }
23 
24 else if (link->y < 0)
25 {
26
27 link->y = SCREEN_H-24;
28 scrolly -=SCREEN_H;
29 }

Problems with this: An error occurs when you try to go all the way up. You start at the bottom, you go up and the map changes fine, then you try to go up once more, and instead of map change, the error occurs and app. has to be closed. I think I know why this is. I'm not sure I understand all of the math, I tried to follow the book:
MAPH = MAP_DOWN * TILEH = 33*32 = 1056. Since I use SCREEN_H (=480) to scroll vertically, we can see that 1056-480=576. That's fine. Now 576-480=96. Not fine. It might be trying to increment by SCREEN_H for the last time, but goes out of bounds. Might this be the case? If so, how come something similar doesn't happen to scrollx (it works)... AND what can I do about this?

I also realize that if say, if(scrollx < 0) scrollx = 0 holds TRUE, then link still changes position. I'd like to change this after the above problem is solved.

Basically, I'm really stuck here. I've tried a couple of things (which I'd rather not mention here, since those details might be confusing). I"m just looking to get rid of that error, and fix the 2nd problem I've mentioned. I REALLY need the help, I'd really appreciate it. TIA.

P.S. I realize the code isn't necessarily clean, I appologize.
P.S.S. Were you guys this stuck with little things when you started game programming?

piccolo
Member #3,163
January 2003
avatar

it crashing because you are not checking to see if the view screen is at the top of your world.

    else if (link->y < 0)
    {
      if(scrolly - SCREEN_H > Value_that_mean_its_at_the_top_maybe_0)
       {
          link->y = SCREEN_H-24;       
          scrolly -=SCREEN_H;
       }

    }

i dont get why you set y to 0. i use scrolly ++; and y++;

wow
-------------------------------
i am who you are not am i

red-dragon
Member #7,722
August 2006

hey piccolo,
I added the if(scrolly - SCREEN_H > Value_that_mean_its_at_the_top_maybe_0) statement. But something you said leaves me a bit confused. Perhaps you can clarify it for me... First, I set the "Value_that_mean_its_at_the_top_maybe_0" to be 0... what else would it be? Seriously, explain.

I'll explain how I'm doing this: I simply say, if (link->y < 0), so if the user has pressed up to the point where link is on very top edge of screen (0,0 is on top left in allegro, right?), then two things should happen:
1. When the shift happens, you want link to be bottom of the screen, hence the "link->y = SCREEN_H-24;" statement.
2. you want to scrolly the world by scrolly -= SCREEN_H UNLESS (maybe!) scrolly<96.

Either way, I'm curious why the scrollx works by scrolly is giving me the problem.
If someone wants, I can post the project on here. Lemme know.

p.s. I set y to zero so he appears at top of screen..? I might be missing your point here.

piccolo
Member #3,163
January 2003
avatar

i see now i would have did the link game a little different .
i would use 2d array of maps the size of screen called areas[][];
if links in area [4][2] and goes to the top goto area[4][1];

but try this i think this will fix your bug.

    else if (link->y < scrolly )
    {
      if(scrolly - SCREEN_H > Value_that_mean_its_at_the_top_maybe_0)
       {
          link->y = SCREEN_H-24;       
          scrolly -=SCREEN_H;
       }

    }

edit: sorry for untimely reply allegro did not email me???

wow
-------------------------------
i am who you are not am i

red-dragon
Member #7,722
August 2006

hey piccolo,
yeah, this is my first time game programming. i'm following the famous allegro book, and modifying the code into what I want to do. People might say starting from scratch is better, but I learn best this way. I just can't believe how much time I'm wasting with this... and not solving the bug :(

I have tried your second suggestion, but to no avail. I'm gonna upload my dev project here so that you might feel free to mess around (or anyone really). Yeah, code isn't clean, comments are all over.. go to movelink() and at the bottom is the piece of code that I've posted here. Here's the zip file for the project.

Keep the inputs coming, I'm a bit desperate to solve this 'bug'.

piccolo
Member #3,163
January 2003
avatar

hey im still looking at your code it seems to be doing the area[][] thingy i was talking about. but i don't see the where the tile are put together to make the maps areas. if this is your first game i don,t think it a nice way to lern allegro because you don't see graphics i leaned allegro with this site use the rpg Examples they also have a Zelda clone.http://agdn.netfirms.com/main/ take a look at it while i check out you bug if you want to.

Edit:

ok heres the changed code

1 
2else if (link->y < 0)
3 {
4 if(scrolly-SCREEN_H < 0)
5 {
6 scrolly +=SCREEN_H;
7 link->y = SCREEN_H-24;
8 }
9 else
10 {
11 scrolly -=SCREEN_H;
12 link->y = SCREEN_H-24;
13 }
14 
15 }

you sound like you learn just like me so check out that site i gave you it really got me started. you can check out my game as well it in c++ mvsc6 http://www.allegro.cc/depot/Thegame/
if you have yahoo messager you can message me at jeffnicq good luck

wow
-------------------------------
i am who you are not am i

red-dragon
Member #7,722
August 2006

piccolo, I'm using a map made in Mappy. I'm using mappyal.c/.h...so I load map1 like this: MapLoad("map1.fmp"), and then to draw the map, I call MapDrawBG.

I found the tutorial you're talking about and will look into that tomorrow. I just refuse to believe that noone else has done it the way I'm talking about or doesn't know what I'mtalking about.

It's late at night and I can only hope that when I check this thread in the morning, a couple more people will weigh in on this matter. Even if they are just ideas!

Piccolo, your idea is not bad; but that'd mean leaving this matter unresolved..... and i'd HATE to do that, heh. It's kind of eating away at me.

C'mon peeps, pop on in here ;)

piccolo
Member #3,163
January 2003
avatar

did you try the code i posted it fixes your bug.???

wow
-------------------------------
i am who you are not am i

red-dragon
Member #7,722
August 2006

Aw, hey piccolo,
thanks for all your help. I did try your solution, but it partially works. Well, no. What happens is that when you hit up, he comes all the way back to the bottom... right? What is suppose to happen is that there is a portion of the map still up there that doesn't get displayed. How do you display a partial map when it is smaller than SCREEN_H?
Piccolo, the map's 4 sides are all surrounded by blue. Try running your code again and keep pushing up; you'll see he never reaches the top blue.
I do appreciate you sticking by, it's very helpful. Keep it comin' ;)

piccolo
Member #3,163
January 2003
avatar

ok now i see what you mean could you give me the link to the program you use to look inside the map1.fmp file. i need to know how big the map real is and how much of it your grabbing for each map area. what could be happening is your grab to much each time . so when you want to grab again there not enough to make a full screen. that will look like this.

9999999999
9112341429
9234545629
9231232329
9234244319
9342432489<-
9343243249<- your here first
9999999999<-

say your on the bottom right corner witch is we need 3 rows of data to fit on screen hight screen width;
32489
43249
99999
when you go up to the next screen you grab the next 3 rows

45629
32329
44319

and when you try to go up again you try to grab the next 3 rows but there are only 2 row left

99999
41429
not enough to make the whole screen thats why you get error
i see maybe is you change the amount you grab or make the map bigger so that that all grabs are even add one more row of data to the top. this is just a guess i still want to look in the map1.fmp file

[edit] i just found the program and it seems that my guess is right
ill see if i can find out how to make the hight bigger.

wow
-------------------------------
i am who you are not am i

red-dragon
Member #7,722
August 2006

piccolo,
it is called Mappy. You can open the map1.fmp in there.
I am sooo glad you understand now what I was trying to say. I am only curious why this doesn't happen with scrollx.

Anyway, it wasn't my map, it came with the author's tutorial... so other than making sure that the map is the perfect size.. there's not much I can do, eh..?

I'll be waiting for your reply, Piccolo.

EDIT: So, I changed height of map to 30 tiles. 30*32 (32 = tilewidth) = 960. 960-480 = 480; 480-480 = 0. SO, that starts working, but when I hit the top bottom edge of map, an error pops up with error message. I even get a weird horizontal scroll error.
So, is the other way, with the array much better than this? Should I just call quits on this and go the other way??

piccolo
Member #3,163
January 2003
avatar

ok heres the error change #define MAP_DOWN 33 to
#define MAP_DOWN 46
edit:
this means the map is 46 tiles high

its in the link.h around the top

wow
-------------------------------
i am who you are not am i

red-dragon
Member #7,722
August 2006

piccolo,
heh, i tried what you said again. when i make it 46, the map displays out of bounds with a lot of pink. this time, little Link almost reaches the very top and then program crashes.

I'm thinking of doing it with the arrays. I'm looking at the rpg tutorial that you gave me the link to piccolo.... have you followed it? Link Anyway, here the tutorial mentions DATAFILE *data... wth is that??

Lastly, we have load_map() code:

1void load_map()
2{
3 PACKFILE *pfile;
4 
5 pfile=pack_fopen("map1.dat", "rp");
6 
7 int i,j;
8 for (i=0; i<GRID_WIDTH; i++)
9 {
10 for (j=0; j<GRID_HEIGHT; j++)
11 {
12 map.cell[j]<i> = pack_igetl(pfile);
13 }
14 }
15 pack_fclose(pfile);
16}

How do you make a map and save it as map1.dat? Can you do that in Mappy (this is the only map editor I know...)... plus, what does "rp" mean??

piccolo
Member #3,163
January 2003
avatar

yeah take a look at my game

wow
-------------------------------
i am who you are not am i

Go to: