Jump to content
SubSpace Forum Network

Recommended Posts

Posted

So a few weeks ago I had some time off and decided to try coding. I started with Microsoft VB.net, I wanted to make a space invaders game. (Hey I was bored) and I heard vb.net is great for beginners.

 

So the problem is, I have an array of 60 pictureboxes for my enemies. I added the code for everything and it all works, except the lasershooting part. I get an error reading "Object reference is not set to the instance of an object".

 

The code works fine if I have just 1 picturebox but I have an array of 60. 8)

 

This is the code I have:

 

Sub enemyhit()

 

For x = 0 To 59

If un_laser.Bounds.IntersectsWith(enemymovement(x).Bounds) And enemymovement(x).Tag = "alive" Then

tmrshoot.Enabled = False

un_laser.Visible = False

enemymovement(x).Tag = "dead"

enemymovement(x).Visible = False

Call scorecontrol()

End If

Next

End Sub

 

Any help would be great cuz I gave up and its been bugging me as I hate leaving things unfinished. blum.gif Plus I've been busy so reading books n whatnot is not an option as I don't have much time anymore.

Posted

Is it going to have Subspace graphics? Same starfield? Warbird as player ship, Javelin through Shark as enemies, Nightwasp/turret ship as boss? Same icon's on left and right side of screen for inventory? blum.gif

 

*realizes this post is irrelevant to requested help*

Posted

Use the line by line debugging to tell where exactly it crashes... Basically one of your 'enemymovement' object isn't defined...

 

Also... I suggest you look into the System.Drawing and System.Image I think... Using pictureboxes is a very bad way to do what you're trying to do blum.gif

 

Basically, load the images in some Image objects, then just paint them wherever you need on your window, which could be a picturebox, or you can draw directly on the form I think.

You should also use structures for your enemies, instead of the tag thing. The structure could hold various information, such as the current location of the enemy, its remaining energy, if it's dead or not, etc.

Posted

@ L.C It actually is with subspace gfx lol..

 

@Sama - It crashes on the If Then statement, I know the problem is with enemymovement array that much is sure. As far as structures go I have no clue XD

Posted (edited)

Yea I tried 1 to 60 as well, something is wrong with my enemymovement array. For example;

This is the only way I can declare it as an array:

Public enemymovement(59) As PictureBox '= {picenemy1, picenemy2, picenemy3, picenemy4, picenemy5, picenemy6, picenemy7, picenemy8, picenemy9, picenemy10, picenemy11, picenemy12, picenemy13, picenemy14, picenemy15, picenemy16, picenemy17, picenemy18, picenemy19, picenemy20, picenemy21, picenemy22, picenemy23, picenemy24, picenemy25, picenemy26, picenemy27, picenemy28, picenemy29, picenemy30, picenemy31, picenemy32, picenemy33, picenemy34, picenemy35, picenemy36, picenemy37, picenemy38, picenemy39, picenemy40, picenemy41, picenemy42, picenemy43, picenemy44, picenemy45, picenemy46, picenemy47, picenemy48, picenemy49, picenemy50, picenemy51, picenemy52, picenemy53, picenemy54, picenemy55, picenemy56, picenemy57, picenemy58, picenemy59, picenemy60}

 

If I try to do it any other way it won't work. I've been thinking of reducing the number of enemies but so far the number of enemies isn't the problem, more or less the array itself isn't co-operating with me. >.<

 

I've tried declaring the array in the Sub enemyhit() procedure but that didn't work either. :(

 

EDIT: Problem fixed, I declared the array in the procedure with no explicit bounds and it works. =D If anyone knows any tips for structures that would be great.

Edited by Synister
Posted

oh i see what is wrong

 

 

Public enemymovement(59) As PictureBox

 

These declare an array of Objects (which are basically some 4-bytes numeric value that points to an address in the memory, where the actual object data is stored), but it does not initialize the objects themselves.

You have to do this after:

 

for i = 0 to 59
set enemymovement(i) = new PictureBox

'Initialize any other data you wish to here
'i.e.:  enemymovement(i).tag = "dead"
next

 

 

I seem to remember VB6 arrays starting at 1 and not 0.
That is true for some arrays... mostly the items in a listbox controls, or things similar. The weird thing VB does with arrays is:

Public enemymovement(59) As PictureBox
'This declares an array going from enemymovement(0) to enemymovement(59), which is 60 elements
'You can also declare an array like this:
Public enemymovement(1 to 60) as PictureBox
'This allows you to set the bounds
'You can use LBound(enemymovement) and UBound(enemymovement) to get the bounds of the array, which would be 1 and 60 in this last case.

 

Note that this is all true for VB6... I suspect it will be similar in vb.net, but you might want to experiment...

I also suggest you look at how to define constants... If you ever want to have more than 60 enemies, you'll have to redo all your code; not if you use constants. It would look like:

Public enemymovement(MAX_ENEMIES - 1) as PictureBox

'...

For i = 0 to MAX_ENEMIES - 1
'...
Next

Note that achieving your objective of having spaceships is only secondary to actually learning how to code blum.gif

 

 

 

Edit: Structures in VB.net: Read this

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...