Array problem
BlitzPlus Forums/BlitzPlus Beginners Area/Array problem
| ||
I'm getting an error of "array index out of bounds" and I have no idea what it means. I'm trying to draw 10 aliens on the screen with this code: If gfxAlienOnScreen(x) = True Then For x = 1 To AlienCount DrawImage gfxAlienOnScreen(x),Alien_x(x),Alien_y(x) Next EndIf Ok, the DrawImage is actually in front in Blitz. My understanding of arrays, types and their use is still very primitive if you can't tell. Any help will be appreciated. Thanks |
| ||
If you declare an array with Dim a(5) then the array has six elements: a(0) a(1) ... a(5). When you refer to a(x) the index variable x must be in the range 0 to 5. Anything else is an 'out of bounds' error. |
| ||
Thanks Floyd. The program will compile now but no aliens will draw to the screen. Any tips? Here is the new code: If gfxAlienOnScreen(AlienCount) = True Then For x = 0 To AlienCount DrawImage gfxAlienOnScreen(x),Alien_x(x),Alien_y(x) ;Draw alien on screen Next EndIf Thanks |
| ||
I have a suggestion for you: scratch the whole array usage here. In a case like this, using Types can produce much better results. The built-in Blitz tutorials have some Type coverage, and if you need any more help, I'll be happy to do what I can! |
| ||
I think the first line should be changed to: If gfxAlienOnScreen(x) Then Drawimage etc. and then be placed in the loop |
| ||
ADAM, I guess now will be as good of a time as any to try my hand with types. I'll work with those for a bit and post back if I have any trouble. Thanks b32: I'll try that and see what happens but I believe only 1 alien would be drawn on the screen since the array would not be completely filled if drawimage was used before the loop. I'll post back in a bit and let you know what happens. Thanks edited to add: No aliens were drawn with drawimage in front of the for...next loop.:( |
| ||
You're right about that drawimage should be in the loop. I thought you should move the If..Then inside the loop:For x = 0 To AlienCount If gfxAlienOnScreen(x) Then DrawImage gfxAlienOnScreen(x),Alien_x(x),Alien_y(x) ;Draw alien on screen Next But why are you checking gfxAlienOnScreen() ? Maybe you could use this: For x = 0 To AlienCount DrawImage gfxAlienOnScreen(x),Alien_x(x),Alien_y(x) ;Draw alien on screen Next ? |
| ||
I was checking gfxAlienOnScreen() so it would only draw the aliens that have not been killed. How do you post code in blue like that? Thank you b32. Moving the if..then and taking the "= true" off the end works. I removed the "= true" from "If gfxAlienOnScreen()" in two places and everything seems perfect now. I wonder why it wasn't working with "If gfxAlienOnScreen() = true"? Thanks again! |
| ||
Here's an interesting bit of info about using If statements with True and False operators. Coding something like:If variable = FalseIs the same as saying: If variable = 0Or: If Not variableBasically, False is equal to zero. True, on the other hand, can represent two different things. If you use it in this fashion: variable = TrueThen True will equal 1, and that value will be stored in variable. If, however, you use it like this: If variable = TrueThen True represents any value greater than 0. How you "phrase" your code doesn't usually make a difference to the output, it just makes it more or less compact. So, instead of: If variable = FalseConsider: If Not variableAnd instead of: If variable = TrueConsider: If variableThat's all. Happy coding! |
| ||
How do you post code in blue like that? Heh .. the code is green here .. guess we have a different 'theme' enabled. But for posting code like that, use the forum codes. You can find them here: http://www.blitzbasic.com/faq/faq_entry.php?id=2 Using =true would make no difference, as Adam explained, so it was moving the instruction inside the loop that solved it, and offcourse using (x) instead of (aliencount). Outside the loop, 'x' is undefined. Say, after a loop "FOR x=0 to 10", normally 'x' would be 11. However, I would just use 11 instead of 'x' to be sure. |
| ||
[quote]as Adam explained[/qoute] AAAAARGH!!!!!!! It's not Adam, it's ADAM!!! IT SAYS THAT IN MY SIGNATURE, FOR PETE'S SAKE!!! LOOK, RIGHT THERE!!! NOT THERE, THERE!!!!! UNDER MY EMBLEM!!!!! WHY CAN'T PEOPLE UNDERSTAND THE ALL-CAPS THEME?!?!?! AAAAAAAGH!!!!!!!!! o_0 Ahem. Sorry. If you haven't already figured it out, I'm just slightly insane. Slightly. ^_^ |
| ||
lol @ ADAM. Thanks for the help guys. I just discovered the insectoids sample game in blitz and I've been trying to study the use of types there. The code isn't commented very well but I have been able to gain a little bit of insight into them.And of course I'm already confused and having some trouble but I'm going to play around with it a bit more before bugging you all for more help. Thanks again! |
| ||
Here's an example for you... If you like kitties! lol |
| ||
ow i'm sorry adam i must have missed it./ |
| ||
S'okay, don't sweat it. By the way, have you ever been run over by a fifty-foot eighteen-wheeler? I could arrange for it, if you'd like. jk lol ;) |
| ||
18-wheels ? :) ok, ok, that is convincing enough. i give up, Gigamaster, you are right. |