tilemap offsets and scrolling help?

BlitzMax Forums/BlitzMax Beginners Area/tilemap offsets and scrolling help?

Amon_old(Posted 2005) [#1]
Hi! I want to be able to stop my scrolling tilemap when it reaches the outer edges of the map. ie for instance if we get to the end right of the map i dont want it to scroll anymore.

I have tried to calculate how big the actual map is and tried to use the width and height of the map to check for boundaries but this doesnt appear to work.

heres all the code: the tiles are 32*32 pixels.



And here is the the function that checks the borders.

Function check_borders()

	If scrollx > 1920 * 32 Then scrollx = 1920
	If scrollx < 0 Then scrollx = 0
	If scrolly > 1920* 32 Then scrolly = 1920
	If scrolly < 0 Then scrolly = 0

End Function



ANy help would be appreciated. I got some help on COders workshop but litterally without someone giving me the codes I doubt I'll get it right.

Thanks :)


Ryan Moody(Posted 2005) [#2]
Ok, here you go.

Const TotalNoOfTilesInARow     = 100
Const TotalNoOfTilesInAColumn  = 100
Const NoOfTilesToShowInARow    = 20
Const NoOfTilesToShowInAColumn = 15

Global VisibleAreaX = 0 ; Refers to the co-ordinates of the
Global VisibleAreaY = 0 ; tile that is visible at the top left

Function isPossibleToScroll( direction$ )

   ; Returns true if it is possible to scroll in the given direction.
   ; False otherwise.

   Select direction

      Case "LEFT" Return VisibleAreaX > 0
      Case "RIGHT" Return VisibleAreaX < TotalNoOfTilesInARow - NoOfTilesToShowInARow

      .
      . Similar for the other cases
      .

   End Select

End Function

Repeat

   If KeyDown(LEFT) And isPossibleToScroll("LEFT")

      VisibleAreaX = VisibleAreaX - 1

   Endif

   .
   . Similar for the other directions
   .

Forever



Amon_old(Posted 2005) [#3]
Hi Ryan :)

I dont have the equivalant of this in my code.

Const NoOfTilesToShowInARow    = 20
Const NoOfTilesToShowInAColumn = 15


I get what your code does but I dont get how to implement it in my code.

For instance if you look at my code scrollx and scrolly is used to scroll the map. Then mapx and mapy hold how many tiles in a row and colum. The tilemap is fullscreen.

Can you just put the code in the check_borders function in my existing code so I can see how it works?

Please dont get bored with me or annoyed. My brain just doesnt compute like other peoples. :/


Ryan Moody(Posted 2005) [#4]
Please dont get bored with me or annoyed


How about frustrated?

Hold on, I'll come up with a cunning plan...


Ryan Moody(Posted 2005) [#5]
What does the number 1920 refer to?


Amon_old(Posted 2005) [#6]
What does the number 1920 refer to?


I tried to calculate the width in pixels of the map array. O_o


Ryan Moody(Posted 2005) [#7]
That's not neccessary. It's easier to work with the number of tiles you have rather than their dimensions. Actually, at first, I worked with the dimensions, but I came up with this better idea.

And, dare I say it, you may want to do the same. You don't have tons of code there, so it may be worthwhile redesigning a few parts to incorporate my solution. Don't be afraid of deleting code, it's all a part of the learning process.

I advise you to declare as many constants as possible - if you use a number like 1920 in your code, a week later, you won't remember what it means (no offence!).

I suggest you save a back up of your code, then try to edit it so that you get the kind of solution I've given you. If it all goes wrong, at least you still have the old version intact.

Yet, if anyone can see a quick fix, tell Amon!

Ryan


Scott Shaver(Posted 2005) [#8]
I didn't test this but try something like this

Function scroll_map()

	If KeyDown(KEY_LEFT) then scrollx:-3
	If KeyDown(KEY_RIGHT) then scrollx:+3
	If KeyDown(KEY_UP) then scrolly:-3
	If KeyDown(KEY_DOWN) then scrolly:+3 

   if scrollx+(mapx*32)<GraphicsWidth() then scrollx= -((mapx*32)-GraphicsWidth())
   if scrolly+(mapy*32)<GraphicsHeight() then scrolly= -((mapy*32)-GraphicsHeight())
   if scrollx>0 then scrollx=0
   if scrolly>0 then scrolly=0
End Function



BTW you should really use constants or vars for the tile width and height instead of hard coding 32.