1

I'm trying to create a map class for a simple 2D game. The map has an array(100,100,5) of string, returning the kind of tiles to draw ( with 5 possible layers from 0 to 4) players will move on the 2nd layer, the background of the game is on layer 0)

I'm want a drawMap sub that I would be able to call as needed. ( not the onPaint method, because I'm loading the game data map from a binary file, and this game contains the needed coordinates, number of tiles, etc and the array of tiles, so I need to load the map info 1st, then use the draw sub)

I'm struggling right now because only the Tiles(0 to 3, 0 to 1,0) are showing ... while my array is full of tiles !

enter image description here

Heres the code :

Public class Map
   inherits Panel

'... properties and stuff ....'

 Public Sub New(Optional ByVal mapName As String = "")

    G = Me.CreateGraphics()

    Me.BorderStyle = Windows.Forms.BorderStyle.Fixed3D
    Me.Location = New Point(250, 50)

    Load(mapName)

    Me.Width = mapSizeX * tileSize
    Me.Height = mapSizeY * tileSize

End Sub

Public Sub DrawGraphics(ByVal layer As Integer, ByVal g As System.Drawing.Graphics)
    For y As Integer = 0 To Map.mapSizeY - 1
        For x As Integer = 0 To Map.mapSizeX - 1
            If Not Map.Tile(x, y, layer) = "" Then
                GetSourceRect( x, y, layer, Map) 'return rectangle from my spritesheet '
                dRect = New Rectangle((x * tileSize), (y * Map.tileSize), tileSize, tileSize)
                g.DrawImage(SpriteSheet, dRect, sRect, GraphicsUnit.Pixel)
            End If

        Next
    Next
End Sub
end class

Load(mapname) deserializes a binary file containing the value for mapSizeX and mapSizeY, as well as tileSize and the Tiles(100,100,5) array all filled up.

What am I doing wrong at this point ?

House
  • 73,224
  • 17
  • 184
  • 273
  • This is pretty close to a "debug my code for me" question. What debugging steps have you taken already? – House Jul 11 '13 at 03:31
  • re "(not the OnPaint method ...)": If you are going to insist on not doing it right, why should we bother trying to help. Using the OnPaint method is the correct mechanism, for far more reasons than can be enumerated in a comment; just do it. – Pieter Geerkens Jul 11 '13 at 03:48

1 Answers1

0

The answer was pretty simple, but took me a whole 2 days of reading forums ... the Paint event overrides the drawn image, cancelling/stopping it.

  • firewall at work stop the login at the top of the screen, but I could "log in" from a "guest" account. – Thierry Savard Jul 12 '13 at 14:55
  • as for comments from Byte56 :If I take the time to post on the forums, its because Im at a loss about what to do. This is not an error... the stuff do render. I change part of my code each time I want to try something, and I do it blindly not knowing if I'm in the right direction, hence the question.

    Pieter : I coulnt use the paint event because I load all the parameters used to draw from the constructor, and I needed the item to exist before I draw the graphics. for now I manage to make it work by creating a mapInfo class, and after that creating this map class. but it seems a little weird.

    – Thierry Savard Jul 12 '13 at 14:59
  • and i'm sure I'll have tons of problems as soon as I get monsters and movements in if I keep it this way, but thats for another day... – Thierry Savard Jul 12 '13 at 15:03