Scrolling map works up and down but not left and right! ???
Blitz3D Forums/Blitz3D Beginners Area/Scrolling map works up and down but not left and right! ???
| ||
Hi folks, I am working on some code that loads in a level of map tiles and scrolls one block at a time using the arrow keys. Thing is... i cant get left and right to scroll correctly, all the tiles are jumbled up - rather than scrolling they appear random. Any help would be appreciated. heres the code: global tileset global locationx global locationy locationx=100 : locationy=100 dim tile_position_array(500,500) ;### Load in Level myfile = ReadFile("D:\Projects\Learning\Level1.dat") For i = 0 to 500 For j = 0 to 500 Tile_Position_Array(i,j) = ReadByte (myfile) Next Next Closefile myfile graphics 1024,768,32,1 setbuffer backbuffer() tileset=LoadanimImage("d:\projects\game graphics\tileset2.bmp",64,64,0,50) while not keyhit(1) a=0 b=0 for x=0 to 1024 step 64 for y=0 to 768 step 64 bob = Tile_Position_Array(locationx +a,locationy +b) drawimage tileset,x,y,bob a=a+1 next b=b+1 next flip if locationx<500 and keyhit(208) then locationx=locationx+1 if locationx>0 and keyhit(200) then locationx=locationx-1 if locationy<500 and keyhit(205) then locationy=locationy+1 if locationy>0 and keyhit(203) then locationy=locationy-1 wend |
| ||
Having a quick look at your code I see something that doesnt quite look right. In the loops to draw the tiles you are incrementing a in the innner y loop and b in the outer x loop but you are using them the other way around then using the Thle_Position_array. So if you swap the lines a=a+1 and b=b+1 over. You should also be reseting the b variable at the start of the y loop. I think your code should be something like : a=0 for x=0 to 1024 step 64 b=0 for y=0 to 768 step 64 bob = Tile_Position_Array(locationx +a,locationy +b) drawimage tileset,x,y,bob b=b+1 next a=a+1 next |
| ||
Well That now works! Thanks John. I dont quite understand what i was doing wrong and my head is really fried trying to grasp this problem for a few hours! Wait - i think i got it now. Many thanks! |
| ||
Im glad to hear that fixed it for you. Feel free to ask if you are still not sure about why the new code now works. |
| ||
Thanks John my next task (now that I kind of understand whats going on) is to work out how to make it scroll smoothly as opposed to block scrolling as it does now. My initial idea on this one is to have some kind of offset value and use origin. Any thoughts? |
| ||
yes.. that is just how I would do it. store a locationXOffset, locationYOffset. Then when checking for the keypresses add/subtract values from the offset values. Then add some code to check the offset values to decide if the locationX, locationY values need to be changed. eg. if locationXOffset < 0 then locationXOffset = locationXOffset + 64 locationX = locationX - 1 end if if locationXOffset > 63 then locationXOffset = locationXOffset - 64 locationX = locationX + 1 end if and the same from the y values. If you are wondering why I am checking for < 0 and adding 64 or checking for > 63 and subtracting 64 I have found its a nice way to cater for various speeds of scrolling. That code will work even if you move 2,3,4 + pixels at a time up to a maximum of a full tile size. |
| ||
Thanks John Will check this out. Im starting to learn this stuff at last! |
| ||
Thanks to John (QuietBloke) I have created my first scrolling tilemap program!!! Woohoo! The code is a bit rough and ready at the moment and it needs some checks putting in so that it cannot scroll out of the map area but appart from that I am well chuffed. I am looking at using it for a Rebelstar Raiders game on the old speccy! Anyway, heres the code: ______________________________________________________ ;### Initialisation of Variables global tileset global locationx global locationy global locationxoffset Global locationyoffset dim tile_position_array(500,500) locationxoffset=0 locationyoffset=0 locationx=100 locationy=100 ;### Load in level data from a dat file created earlier myfile = ReadFile("D:\Projects\Learning\Level1.dat") For i = 0 to 500 For j = 0 to 500 Tile_Position_Array(i,j) = ReadByte (myfile) Next Next Closefile myfile ;### Initialise Screen and load in tileset created earlier graphics 1024,768,32,1 setbuffer backbuffer() tileset=LoadanimImage("d:\projects\game graphics\tileset2.bmp",64,64,0,50) ;### Main program loop while not keyhit(1) ;### calculate screen offset for initial placement of tiles if locationXOffset < 0 then locationXOffset = locationXOffset + 64 locationX = locationX + 1 end if if locationXOffset > 63 then locationXOffset = locationXOffset - 64 locationX = locationX - 1 end if if locationyOffset < 0 then locationyOffset = locationyOffset + 64 locationy = locationy + 1 end if if locationyOffset > 63 then locationyOffset = locationyOffset - 64 locationy = locationy - 1 end if ;### Display tiles from calculated offset using origin command origin locationxoffset,locationyoffset a=0 for x=-64 to 1024 step 64 b=0 for y=-64 to 768 step 64 bob = Tile_Position_Array(locationx +a,locationy +b) drawimage tileset,x,y,bob b=b+1 next a=a+1 next ;### Just in case... reset the origin to 0,0 origin 0,0 ;### Bring created screen to the front flip ;### Process Inputs and continue looping if locationy<500 and keydown(208) then locationyoffset=locationyoffset+8 if locationy>0 and keydown(200) then locationyoffset=locationyoffset-8 if locationx<500 and keydown(205) then locationxoffset=locationxoffset+8 if locationx>0 and keydown(203) then locationxoffset=locationxoffset-8 wend |
| ||
I forgot to mention that you will obviously need the tileset2.bmp and the level1.dat files to get this working - email me on uraliss@... if you need these. |