none
Retrieve photos/pngs to form a sprite (is a picturebox): Decisions, decisions decisions....

    Question

  • Afternoon all, I have a dilemma on which piece of code to use. The aim of what I am trying to do here is: load the pngs from wherever it is stored into the game grid so that it can be used. 

    Should I stick to option 1 where it basically goes through a file path to retrieve the png? However the implication here is the group of picturefiles are formed as 1 and I can't spread them out. Perhaps a code would separate them out. Also to mention when I try reference them by their name, it shows index out of range of the argument. 

    Here is the code which imports the sprites: 

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            DoubleBuffered = True
            For i = 1 To 3
    
                Dim pic = New PictureBox()
                With pic
                    .Name = "Pic" & i.ToString
                    .Size = GridSize
                    .BackgroundImageLayout = ImageLayout.Stretch
                    .BackgroundImage = Image.FromFile("C:\Users\BusinessOffice1Room\Documents\Waliur File\A Level\Computer Science\Coursework\Work In progression\Elements + Stage 2 Designs\Tanks\" & i.ToString & ".png")
                    '.BackgroundImage = Image.FromFile("N:\Computer Science Coursework\Work In progression\BMP Files\Tanks\" & i.ToString & ".png")
                    Me.Controls.Add(pic)
                    AddHandler .MouseDown, AddressOf Pics_MouseDown
                    'AddHandler .MouseMove, AddressOf pics_MouseMove
                    'AddHandler .MouseUp, AddressOf pics_MouseUp
                End With
    
                Dim spt As New Sprite
                spt.Pic = pic
                spt.ColRow = New Point(i)
                Sprites.Add(spt)
    
            Next
    
            Sprites(0).Pic.BackColor = Color.Red
            Sprites(0).Name = "Heavy Panzer"
            Sprites(1).Pic.BackColor = Color.Green
            Sprites(1).Name = "Heavy Scorcher"
            Sprites(2).Pic.BackColor = Color.Blue
            Sprites(2).Name = "Heavy Artillery"
    
            Sprites(0).HP = 10
            Sprites(1).HP = 10
            Sprites(2).HP = 10
    
            Sprites(0).AP = 5
            Sprites(1).AP = 5
            Sprites(2).AP = 5
    
        End Sub

    Second option Create a file to hold the images within my visual studio program and use system.io to access these imports and place them into creating a sprite without the imposed problem with what the name of the png file is called. 

    Public Sub LoadAssets()
            For Each imageFile In IO.Directory.GetFiles(IO.Path.Combine(My.Application.Info.DirectoryPath, "Assets"), "*.png")
                SpriteSheets(IO.Path.GetFileNameWithoutExtension(imageFile)) = Bitmap.FromFile(imageFile)
            Next
        End Sub

    I just don't know which is the best to use. I could use the original one but as I said before I have to name it 0.png, 1.png and so on just to get them to actually work while the code above doesn't impose such as issue. 

    In theory I would really want to use the code above because it is relatively clear to understand but it poses the issue that it won't take the characters that it actually says on there such as "Heavy Panzer.png" and yes I tried  with "*.png" which represents series of unicode/several strings.


    WRA

    Saturday, March 25, 2017 5:35 PM

Answers

  • I'll conclude since Frank who had the right answer has gone so the answer is in the next thread but this is the key hinter that I was reccomended to use

    Public Sub LoadAssets()
            For Each imageFile In IO.Directory.GetFiles(IO.Path.Combine(My.Application.Info.DirectoryPath, "Assets"), "*.png")
                SpriteSheets(IO.Path.GetFileNameWithoutExtension(imageFile)) = Bitmap.FromFile(imageFile)
            Next
        End Sub


    WRA

    • Marked as answer by Waliur Rahman Sunday, March 26, 2017 11:33 AM
    Sunday, March 26, 2017 11:33 AM

All replies

  • The first one I mentioned I would really want to use as long as there is a patch to 1) access the file according to its name rather than by ridiculous numbers.

    2) When I create these sprites then I can set out individually which one can position where such as the first sprite go into position (5,4). second sprite (3,2) and so on. 


    WRA

    Saturday, March 25, 2017 5:37 PM
  • I'll conclude since Frank who had the right answer has gone so the answer is in the next thread but this is the key hinter that I was reccomended to use

    Public Sub LoadAssets()
            For Each imageFile In IO.Directory.GetFiles(IO.Path.Combine(My.Application.Info.DirectoryPath, "Assets"), "*.png")
                SpriteSheets(IO.Path.GetFileNameWithoutExtension(imageFile)) = Bitmap.FromFile(imageFile)
            Next
        End Sub


    WRA

    • Marked as answer by Waliur Rahman Sunday, March 26, 2017 11:33 AM
    Sunday, March 26, 2017 11:33 AM