Synister Posted January 20, 2010 Report Posted January 20, 2010 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. 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. Plus I've been busy so reading books n whatnot is not an option as I don't have much time anymore. Quote
L.C. Posted January 20, 2010 Report Posted January 20, 2010 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? *realizes this post is irrelevant to requested help* Quote
Samapico Posted January 20, 2010 Report Posted January 20, 2010 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 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. Quote
Synister Posted January 21, 2010 Author Report Posted January 21, 2010 @ 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 Quote
»doc flabby Posted January 21, 2010 Report Posted January 21, 2010 (edited) I seem to remember VB6 arrays starting at 1 and not 0. So might have to be: For x = 1 To 60 Unless you use Option Explict. I don't know if they changed this in vb.net though. Edited January 21, 2010 by doc flabby Quote
Synister Posted January 21, 2010 Author Report Posted January 21, 2010 (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 January 21, 2010 by Synister Quote
Samapico Posted January 21, 2010 Report Posted January 21, 2010 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 '... NextNote that achieving your objective of having spaceships is only secondary to actually learning how to code Edit: Structures in VB.net: Read this Quote
Synister Posted January 22, 2010 Author Report Posted January 22, 2010 Cool thanks for the help Sama =D Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.