Problems with Arrays...

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 3205 Views - Last Post: 12 December 2012 - 02:59 PM Rate Topic: -----

#1 AshinoxVGs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-December 12

Problems with Arrays...

Posted 11 December 2012 - 06:50 PM

Before we continue, I'd like to say I'm notorious for biting off more than I can chew.

I've been working on a 2-D Platformer shoot-em-up and I've encountered an error.

When I try to run my program, I'm struck by a "Object reference not set to an instance of an object." I'm not entirely sure WHY this is happening but it has to do with my monster's spawning.

The code I have for spawning is
 Dim i As Integer = 19
    Dim Bats(i) As PictureBox
    Dim Slime(i) As PictureBox 

and
         For Me.i = 1 To 1
            Me.Controls.Add(Bats(i))
            Me.Controls.Add(Slime(i))
            Bats(i) = New PictureBox
            Slime(i) = New PictureBox
            Bats(i).Location = New Point(12, 415)
            Bats(i).Size = New Size(50, 50)
            Bats(i).BackgroundImage = My.Resources.Bat
            Slime(i).Location = New Point(666, 415)
            Slime(i).Size = New Size(50, 25)
            Slime(i).BackgroundImage = My.Resources.slime
        Next i 


and the code that I'm getting the error from is...
  If picAttack.Bounds.IntersectsWith(Bats(i).Bounds) Then
            If prgEXP.Value >= prgEXP.Maximum Then
                lblLevelCounter.Text = lblLevelCounter.Text + 1
                prgEXP.Value = 0
                prgEXP.Maximum = prgEXP.Maximum * 2
                prgLife.Maximum = prgLife.Maximum + 50
                prgLife.Value = prgLife.Maximum
                lblLifeValue.Text = prgLife.Value & "/" & prgLife.Maximum
            End If 


I'm kind of a beginner so I wouldn't be surprised if it was just something I'm over-looking, but it would be helpful if someone could point out where I'm going wrong...

Thanks for the assistance and I'm looking forward to an answer so I can finish this project.

-Ash

View PostAshinoxVGs, on 11 December 2012 - 06:47 PM, said:

Main problem.


and unfortunately, I cannot move this project over to C++, C#, Java, python, etc...
because this is my computer science final project and we have to use VB.Net :/

Is This A Good Question/Topic? 0
  • +

Replies To: Problems with Arrays...

#2 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,511
  • Joined: 16-March 11

Re: Problems with Arrays...

Posted 11 December 2012 - 11:47 PM

I can't see any real issues with your code except maybe the part where you add the control to the container before actually instancing it. While that isn't throwing an error there it may be causing a problem. Otherwise I say place a breakpoint, click the line next to the line of code and place a red blip, and when it hits the red blip mouse over all variables and double check that the right values are there.
Was This Post Helpful? 1
  • +
  • -

#3 AshinoxVGs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-December 12

Re: Problems with Arrays...

Posted 12 December 2012 - 10:59 AM

View Posttrevster344, on 11 December 2012 - 11:47 PM, said:

I can't see any real issues with your code except maybe the part where you add the control to the container before actually instancing it. While that isn't throwing an error there it may be causing a problem. Otherwise I say place a breakpoint, click the line next to the line of code and place a red blip, and when it hits the red blip mouse over all variables and double check that the right values are there.


Yeah, I didn't see anything wrong with my code either :(/>
When I remove the form controls, it spawns the box, but not the image. So that's another nugget of information. Is there a way to give a name to the newly spawned picture box? If so, I could potentially do that and turn my spawning timer to a public sub (because that's makes the code inside available to all of my private subs... right?) and that might fix it.

I'm not sure if this will help at all, but here's all of my code for the game
Public Class Form2
    Dim Jump As Boolean
    Dim MRight As Boolean
    Dim MLeft As Boolean
    Dim Jumpcount As Byte
    Dim Standing As Boolean
    Dim Attack As Boolean
    Dim Attack2 As Boolean
    Dim Floor As PictureBox
    Dim k As Integer = 4
    Dim Platform(k) As PictureBox
    Dim i As Integer = 19
    Dim Bats(i) As PictureBox
    Dim Slime(i) As PictureBox
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        prgLife.Value = 100
        lblLifeValue.Text = prgLife.Value & "/" & prgLife.Maximum
        lblEXPValue.Text = prgEXP.Value & "/" & prgEXP.Maximum
        Floor = picFloor
        Platform(k) = picPlat1
        picAttack.Visible = False
        picAttack2.Visible = False
        Attacks.Enabled = False
    End Sub
    Private Sub Form2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

        If e.KeyCode = Keys.Escape Then
            Me.Close()
            Form1.Show()
        End If

        If Standing = True Then
            If e.KeyCode = Keys.Up Then
                Jump = True
                Standing = False
            End If
        End If

        If e.KeyCode = Keys.Left Then
            MLeft = True
        End If

        If e.KeyCode = Keys.Right Then
            MRight = True
        End If

        If e.KeyCode = Keys.Z Then
            If picAttack.Visible = False Then
                picChar.BackgroundImage = My.Resources.gun1
                Attacks.Enabled = True
                picAttack.Left = picChar.Left + 125
                picAttack.Top = picChar.Top + 50
                Attack = True
            End If
        End If

        If e.KeyCode = Keys.X Then
            If picAttack2.Visible = False Then
                picChar.BackgroundImage = My.Resources.gun2
                Attacks2.Enabled = True
                picAttack2.Left = picChar.Left - 125
                picAttack2.Top = picChar.Top + 50
                Attack2 = True
            End If
        End If
    End Sub
    Private Sub form2_keyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp

        If e.KeyCode = Keys.Left Then
            MLeft = False
        End If

        If e.KeyCode = Keys.Right Then
            MRight = False
        End If
    End Sub
    Private Sub Gravity_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Gravity.Tick
        If Jump = True Then
            picChar.Top -= 25
            Jumpcount += 1
            If Jumpcount >= 2 Then
                Jump = False
                Jumpcount = 0
                If picChar.Top <= 0 Then
                    picChar.Top = 0
                End If
            End If
        End If
        If MLeft = True Then
            picChar.Left -= 15
            picChar.BackgroundImage = My.Resources.attempt2
            If picChar.Left <= 0 Then
                picChar.Left = 0
            End If
        End If

        If MRight = True Then
            picChar.Left += 15
            picChar.BackgroundImage = My.Resources.attempt1
            If picChar.Left >= 800 Then
                picChar.Left = 800
            End If

            If Standing = True Then
                picChar.Top -= 3
            End If
        End If
        If Attack = True Then
            picAttack.Visible = True
        End If
        If Attack2 = True Then
            picAttack2.Visible = True
        End If
    End Sub

    Private Sub GameTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GameTimer.Tick
        picChar.Top += 3
        For x = 1 To 1
            If picChar.Bounds.IntersectsWith(Floor.Bounds) And Floor.Visible = True Then
                Standing = True
                picChar.Top -= 3
            End If
        Next x

        For x = 1 To 1
            If picChar.Bounds.IntersectsWith(Platform(k).Bounds) And Platform(k).Visible = True Then
                Standing = True
                picChar.Top -= 3
            End If
        Next
    End Sub

    Private Sub Attacks_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Attacks.Tick
        If picAttack.Visible = True Then
            picAttack.Left += 25
            If picAttack.Left >= 800 Then
                picAttack.Visible = False
                Attack = False
            End If
        End If

        If picAttack.Bounds.IntersectsWith(Bats(i).Bounds) Then
            If prgEXP.Value >= prgEXP.Maximum Then
                lblLevelCounter.Text = lblLevelCounter.Text + 1
                prgEXP.Value = 0
                prgEXP.Maximum = prgEXP.Maximum * 2
                prgLife.Maximum = prgLife.Maximum + 50
                prgLife.Value = prgLife.Maximum
                lblLifeValue.Text = prgLife.Value & "/" & prgLife.Maximum
            End If
            Bats(i).Visible = False
            picAttack.Visible = False
            Enemies.Stop()
            Spawn.Start()
            prgEXP.Value = prgEXP.Value + 5
            lblEXPValue.Text = prgEXP.Value & "/" & prgEXP.Maximum
            Bats(i).Top = 518
            Bats(i).Left = 600
            picAttack.Left = 58
            picAttack.Top = 540
            Bats(i).Location = New Point(12, 415)
            Bats(i).Size = New Size(50, 50)
            Bats(i).BackgroundImage = My.Resources.Bat
            Controls.Add(Bats(i))
            Attack = False
        End If

        If picAttack.Bounds.IntersectsWith(Bats(i).Bounds) Then
            Me.Controls.Remove(Bats(i))
            Bats(i).Left = 600
            Bats(1).Top = 518
            Bats(i).Visible = True
            picAttack.Visible = False
            prgEXP.Value = prgEXP.Value + 5
            lblEXPValue.Text = prgEXP.Value & "/" & prgEXP.Maximum
            Bats(i).Top = 415
            Bats(i).Left = 666
            picAttack.Left = 58
            picAttack.Top = 540
            Enemies.Start()
            Spawn.Stop()
            Attack = False
        End If
    End Sub
    Private Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Dim response As MsgBoxResult
        Dim response2 As MsgBoxResult
        response = MsgBox("Do you wish to return to the Main Screen?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confirm")
        If response = MsgBoxResult.Yes Then
            response2 = MsgBox("Are you sure? You'll lose all your Progress.", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confim")
            If response2 = MsgBoxResult.Yes Then
                Me.Dispose()
                Form1.Show()
            ElseIf response2 = MsgBoxResult.No Then
                e.Cancel = True
                Exit Sub
            End If
        ElseIf response = MsgBoxResult.No Then
            e.Cancel = True
            Exit Sub
        End If
    End Sub

    Private Sub Enemies_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Enemies.Tick
        For Me.i = 1 To 1
            Me.Controls.Add(Bats(i))
            Me.Controls.Add(Slime(i))
            Bats(i) = New PictureBox
            Slime(i) = New PictureBox
            Bats(i).Location = New Point(12, 415)
            Bats(i).Size = New Size(50, 50)
            Bats(i).BackgroundImage = My.Resources.Bat
            Slime(i).Location = New Point(666, 415)
            Slime(i).Size = New Size(50, 25)
            Slime(i).BackgroundImage = My.Resources.slime
        Next i
    End Sub
    Private Sub Attacks2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Attacks2.Tick
        If picAttack2.Visible = True Then
            picAttack2.Left -= 25
            If picAttack2.Left <= 0 Then
                picAttack2.Visible = False
                Attack2 = False
            End If
        End If

        If picAttack2.Bounds.IntersectsWith(Bats(i).Bounds) Then
            Bats(i).Visible = False
            picAttack2.Visible = False
            Enemies.Stop()
            Spawn.Start()
            prgEXP.Value = prgEXP.Value + 5
            lblEXPValue.Text = prgEXP.Value & "/" & prgEXP.Maximum
            Bats(i).Top = 518
            Bats(i).Left = 600
            picAttack2.Left = 58
            picAttack2.Top = 540
            Bats(i).Location = New Point(12, 415)
            Bats(i).Size = New Size(50, 50)
            Bats(i).BackgroundImage = My.Resources.Bat
            Controls.Add(Bats(i))
            Attack2 = False
            If prgEXP.Value >= prgEXP.Maximum Then
                lblLevelCounter.Text = lblLevelCounter.Text + 1
                prgEXP.Value = 0
                prgEXP.Maximum = prgEXP.Maximum * 2
                prgLife.Maximum = prgLife.Maximum + 50
                prgLife.Value = prgLife.Maximum
                lblLifeValue.Text = prgLife.Value & "/" & prgLife.Maximum
            End If
        End If

        If picAttack2.Bounds.IntersectsWith(Bats(i).Bounds) Then
            Me.Controls.Remove(Bats(i))
            Bats(i).Left = 600
            Bats(i).Top = 518
            Bats(i).Visible = True
            picAttack2.Visible = False
            prgEXP.Value = prgEXP.Value + 5
            lblEXPValue.Text = prgEXP.Value & "/" & prgEXP.Maximum
            Bats(i).Top = 415
            Bats(i).Left = 666
            picAttack2.Left = 58
            picAttack2.Top = 540
            Enemies.Start()
            Spawn.Stop()
            Attack2 = False
        End If

    End Sub

    Private Sub Spawn_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Spawn.Tick
        If Slime(i).Visible = True Then
            Slime(i).Top += 1

            For x = 1 To 1
                If Slime(i).Bounds.IntersectsWith(Floor.Bounds) And Floor.Visible = True Then
                    Standing = True
                    Slime(i).Top -= 1
                End If
            Next x

            For x = 1 To 1
                If Slime(i).Bounds.IntersectsWith(Platform(k).Bounds) And Platform(k).Visible = True Then
                    Standing = True
                    Slime(i).Top -= 1
                End If
            Next
            If Slime(i).Bounds.IntersectsWith(picChar.Bounds) Then
                If prgLife.Value <= prgEXP.Minimum Then
                    End
                End If
                prgLife.Value = prgLife.Value - 5
                lblLifeValue.Text = prgLife.Value & "/" & prgLife.Maximum
            End If
        End If

        If Bats(i).Visible = True Then
            If Bats(i).Visible = True Then
                If Bats(i).Bounds.IntersectsWith(picChar.Bounds) Then
                    If prgLife.Value <= prgEXP.Minimum Then
                        End
                    End If
                    prgLife.Value = prgLife.Value - 5
                    lblLifeValue.Text = prgLife.Value & "/" & prgLife.Maximum
                    If Bats(i).Left <= 0 Then
                        Enemies.Stop()
                        Spawn.Start()
                        Bats(i).Location = New Point(12, 415)
                        Bats(i).Size = New Size(50, 50)
                        Bats(i).BackgroundImage = My.Resources.Bat
                        Controls.Add(Bats(i))
                    End If
                    If Bats(i).Bounds.IntersectsWith(picChar.Bounds) Then
                        If prgLife.Value <= prgEXP.Minimum Then
                            End
                        End If
                        prgLife.Value = prgLife.Value - 2
                        lblLifeValue.Text = prgLife.Value & "/" & prgLife.Maximum
                    End If
                End If
            End If
        End If
    End Sub
    Private Sub Movement_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Movement.Tick

    End Sub
End Class



It's a lot of code, but I'm sure you've seen worse. Also, this way if there's something that's interfering with my spawn, it won't be as hard to pin-point... as you have everything I do.

(EDIT)

Looking over my code, and I'm seeing a lot of duplication's of code in it... mainly in my attack timers...

Silly Ash, get it together :sweatdrop:
Was This Post Helpful? 0
  • +
  • -

#4 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,511
  • Joined: 16-March 11

Re: Problems with Arrays...

Posted 12 December 2012 - 11:02 AM

I'd say just create a List(Of Picturebox) and store them in that.

Yes that would make it accessible.

On a side note I'd definitely suggest not relying on picturebox's so heavily. Should my tutorial be approved soon I'll give you a link to it. It goes over Drawing, and the Picturebox, both pros and cons, and the basics of each when it comes to game creation as well as usage.

Also did you try using a breakpoint to double check all your values are correct?
Was This Post Helpful? 1
  • +
  • -

#5 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 498
  • View blog
  • Posts: 3,317
  • Joined: 12-January 10

Re: Problems with Arrays...

Posted 12 December 2012 - 11:03 AM

did you try putting a stopper on the for loop and f10 the thing until you get the error?
Was This Post Helpful? 0
  • +
  • -

#6 AshinoxVGs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-December 12

Re: Problems with Arrays...

Posted 12 December 2012 - 11:07 AM

View Posttrevster344, on 12 December 2012 - 11:02 AM, said:

I'd say just create a List(Of Picturebox) and store them in that.

Yes that would make it accessible.

On a side note I'd definitely suggest not relying on picturebox's so heavily. Should my tutorial be approved soon I'll give you a link to it. It goes over Drawing, and the Picturebox, both pros and cons, and the basics of each when it comes to game creation as well as usage.

Also did you try using a breakpoint to double check all your values are correct?


I'm not entirely sure on Lists. Do I have to have all the picture boxes already there? and if I do, could I just have four or five picture boxes and it cycles through them?
Another question, if I can't use picture boxes, what else could I use to make my monsters?
I'm still fairly new to VB.net, only started it in september :/

and yeah, I put a break-point and everything looks fine and dandy.

View PostDarenR, on 12 December 2012 - 11:03 AM, said:

did you try putting a stopper on the for loop and f10 the thing until you get the error?


I get the error on the if... statement.
Was This Post Helpful? 0
  • +
  • -

#7 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 769
  • Joined: 19-October 11

Re: Problems with Arrays...

Posted 12 December 2012 - 11:16 AM

300 lines of code for one single class? This is so called spaghetti code. I just skimmed through your code, and have seen some confusing things: variable i, strange usage of for loops (I can't really explain to myself what should be the point of For Me.i = 1 To 1 and For x = 1 To 1...). In my opinion, you should refactor your code, separate concerns, and do some debugging.

If you get error message, telling you that you need to instantiate object first, before you use it, then do it. You can't add non existing picturebox to controls (you can't add nothing).
Was This Post Helpful? 0
  • +
  • -

#8 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,511
  • Joined: 16-March 11

Re: Problems with Arrays...

Posted 12 December 2012 - 11:23 AM

Actually lucky3 I've gotten the same error from things completely unrelated. Sometimes the compiler will throw errors that don't necessarily have any real relevance but, the programs I experienced that in were quite complex. So we have to consider that as an option, though I do highly doubt it is the case.

Quote

before you use it, then do it.


Honestly I don't agree with this, he's here for help which means he's having a difficult time trying to fix the problem. Simply saying go and do it doesn't help anything.
Was This Post Helpful? 1
  • +
  • -

#9 AshinoxVGs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-December 12

Re: Problems with Arrays...

Posted 12 December 2012 - 11:23 AM

View Postlucky3, on 12 December 2012 - 11:16 AM, said:

300 lines of code for one single class? This is so called spaghetti code. I just skimmed through your code, and have seen some confusing things: variable i, strange usage of for loops (I can't really explain to myself what should be the point of For Me.i = 1 To 1 and For x = 1 To 1...). In my opinion, you should refactor your code, separate concerns, and do some debugging.

If you get error message, telling you that you need to instantiate object first, before you use it, then do it. You can't add non existing picturebox to controls (you can't add nothing).

Like I've said, I'm fairly new to VB.net (heck, coding in general) and my teacher hasn't taught me any of the things you're talking about. And I had my arrays linked to a picture once and it went a little like this:
Bats(i) = picEnemy1
Slime(i) = picEnemy2



and all it would do is just move the picture... which isn't what I wanted :/
I just want to finish this project...
Was This Post Helpful? 0
  • +
  • -

#10 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,511
  • Joined: 16-March 11

Re: Problems with Arrays...

Posted 12 December 2012 - 11:28 AM

View PostAshinoxVGs, on 12 December 2012 - 12:07 PM, said:

View Posttrevster344, on 12 December 2012 - 11:02 AM, said:

I'd say just create a List(Of Picturebox) and store them in that.

Yes that would make it accessible.

On a side note I'd definitely suggest not relying on picturebox's so heavily. Should my tutorial be approved soon I'll give you a link to it. It goes over Drawing, and the Picturebox, both pros and cons, and the basics of each when it comes to game creation as well as usage.

Also did you try using a breakpoint to double check all your values are correct?


I'm not entirely sure on Lists. Do I have to have all the picture boxes already there? and if I do, could I just have four or five picture boxes and it cycles through them?
Another question, if I can't use picture boxes, what else could I use to make my monsters?
I'm still fairly new to VB.net, only started it in september ://>/>/>/>/>

and yeah, I put a break-point and everything looks fine and dandy.

View PostDarenR, on 12 December 2012 - 11:03 AM, said:

did you try putting a stopper on the for loop and f10 the thing until you get the error?


I get the error on the if... statement.


Dim ListBox as new List(Of Picturebox)

For i = 0 to 1
Dim Newpic as new Picturebox
Listbox.add(NewPic)
Next




You can use a for each loop to run through all the items in the list as well.

You can use drawing. Which uses the System.drawing.graphics object to grab the handle to a child window, or parent window for drawing.

Dim G as System.Drawing.Graphics = System.Drawing.Graphics.FromHwnd(Me.Handle) 'form1.handle
Dim MyImage as Image = Image.FromFile("C:\testpic.jpg")
G.DrawImage(MyImage, NewPoint(50, 50)) 'Draws the image at the specified point in the handle given to it.
G.dispose



Simple as that, or you can use the paint event of the form, or a picturebox(this can be a game screen as well).

Then from there you simply use the me.invalidate method or picturebox.invalidate method to erase the screen and call the paint method or any method you'd like to use. I always suggest the paint method because the paint method is naturally called by the invalidate function and it's simple to leave some code in there to draw all your objects. At this point though you'd need to create a class to hold some values for each of your characters. So if it's a bat create a bat class with x, and y values, a string to the path of the image. I tend to store each separate instance of a class in a list of that said class, and use a for each loop to run through them for drawing. If the game gets too costly though I'd suggest only running through lists in certain regions of the game. Takes a bit more coding but it's a far better method, eats up less memory, easier to get transparencies to work right, and teaches you about refreshing in games among other things.

If you're not completely familiar with objects, or the fundamentals of Object Oriented Programming then I'd say pause your project here, and go learn that. Practice it, gain a solid understanding of it.

Best source here: OOP Fundamentals - MSDN

This post has been edited by trevster344: 12 December 2012 - 11:39 AM

Was This Post Helpful? 1
  • +
  • -

#11 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 769
  • Joined: 19-October 11

Re: Problems with Arrays...

Posted 12 December 2012 - 11:34 AM

trevster344 did you read next statement? Error clearly says: "Object reference not set to an instance of an object." What does that mean? There is no instance (no live object to work with) of object.

If he would just put one breakpoint, and step through the code (he got the links), he would clearly see, that he is trying to add Nothing to Controls collection. All that I mentioned in my previous answer, how is that not helpful? Should I wrote the game for him? Helping someone doesn't mean doing his work for me.
Was This Post Helpful? 0
  • +
  • -

#12 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,511
  • Joined: 16-March 11

Re: Problems with Arrays...

Posted 12 December 2012 - 11:38 AM

View Postlucky3, on 12 December 2012 - 12:34 PM, said:

trevster344 did you read next statement? Error clearly says: "Object reference not set to an instance of an object." What does that mean? There is no instance (no live object to work with) of object.

If he would just put one breakpoint, and step through the code (he got the links), he would clearly see, that he is trying to add Nothing to Controls collection. All that I mentioned in my previous answer, how is that not helpful? Should I wrote the game for him? Helping someone doesn't mean doing his work for me.


Did you read my statement just after? Clearly not.

Those are some of his options, which I've already mentioned to him, or he can switch to drawing for easier management.

Nor did I say write the game for him, so please don't put words in my mouth.
Was This Post Helpful? 1
  • +
  • -

#13 AshinoxVGs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-December 12

Re: Problems with Arrays...

Posted 12 December 2012 - 11:53 AM

View Posttrevster344, on 12 December 2012 - 11:28 AM, said:

View PostAshinoxVGs, on 12 December 2012 - 12:07 PM, said:

View Posttrevster344, on 12 December 2012 - 11:02 AM, said:

I'd say just create a List(Of Picturebox) and store them in that.

Yes that would make it accessible.

On a side note I'd definitely suggest not relying on picturebox's so heavily. Should my tutorial be approved soon I'll give you a link to it. It goes over Drawing, and the Picturebox, both pros and cons, and the basics of each when it comes to game creation as well as usage.

Also did you try using a breakpoint to double check all your values are correct?


I'm not entirely sure on Lists. Do I have to have all the picture boxes already there? and if I do, could I just have four or five picture boxes and it cycles through them?
Another question, if I can't use picture boxes, what else could I use to make my monsters?
I'm still fairly new to VB.net, only started it in september ://>/>/>/>/>/>

and yeah, I put a break-point and everything looks fine and dandy.

View PostDarenR, on 12 December 2012 - 11:03 AM, said:

did you try putting a stopper on the for loop and f10 the thing until you get the error?


I get the error on the if... statement.


Dim ListBox as new List(Of Picturebox)

For i = 0 to 1
Dim Newpic as new Picturebox
Listbox.add(NewPic)
Next




You can use a for each loop to run through all the items in the list as well.

You can use drawing. Which uses the System.drawing.graphics object to grab the handle to a child window, or parent window for drawing.

Dim G as System.Drawing.Graphics = System.Drawing.Graphics.FromHwnd(Me.Handle) 'form1.handle
Dim MyImage as Image = Image.FromFile("C:\testpic.jpg")
G.DrawImage(MyImage, NewPoint(50, 50)) 'Draws the image at the specified point in the handle given to it.
G.dispose


Well, I've switched to using lists, but not I need to figure out how to set up my other attributes for the newly created pictures. For example, my attack collision. How will I check if my attack is hitting the newly formed picture box?

Quote

If you're not completely familiar with objects, or the fundamentals of Object Oriented Programming then I'd say pause your project here, and go learn that. Practice it, gain a solid understanding of it.

Best source here: OOP Fundamentals - MSDN


I really wish I could :/
Unfortunately, I have roughly Seven days to finish this, as this is my final project for my Computer Science class... as I've said before, I'm notorious for biting off more than I can chew :/
Was This Post Helpful? 0
  • +
  • -

#14 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,511
  • Joined: 16-March 11

Re: Problems with Arrays...

Posted 12 December 2012 - 12:00 PM

Okay well you can use a for each loop which will run through each element in an array, or list, or whatever allowing you to search for conditions or whatever you want.

For each Sprite as Picturebox in PictureboxList

'code here

Next



There are more advanced ways to do this, but I won't list them lol.
Was This Post Helpful? 1
  • +
  • -

#15 AshinoxVGs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-December 12

Re: Problems with Arrays...

Posted 12 December 2012 - 12:29 PM

View Posttrevster344, on 12 December 2012 - 12:00 PM, said:

Okay well you can use a for each loop which will run through each element in an array, or list, or whatever allowing you to search for conditions or whatever you want.

For each Sprite as Picturebox in PictureboxList

'code here

Next



There are more advanced ways to do this, but I won't list them lol.

I'VE DONE IT... kind of.

They're spawning and the attributes are there... except they apparently don't like to spawn with their sprites showing. In other words... THEY'RE INVISIBLE! What do I do? I mean, they SHOULD be spawning with a picture...
        For Me.i = 0 To 1
            Dim NewBat As New PictureBox
            Dim NewSlime As New PictureBox
            NewBat.Size = New Size(50, 50)
            NewSlime.Size = New Size(50, 25)
            NewBat.Location = New Point(367, 211)
            NewSlime.Location = New Point(367, 211)
            NewBat.BackgroundImage = My.Resources.Bat
            NewSlime.BackgroundImage = My.Resources.slime
            Batbox.Add(NewBat)
            Slimebox.Add(NewSlime)
        Next i


... Right?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2