Array problem

BlitzPlus Forums/BlitzPlus Beginners Area/Array problem

Jester(Posted 2006) [#1]
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


Floyd(Posted 2006) [#2]
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.


Jester(Posted 2006) [#3]
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


Adam Novagen(Posted 2006) [#4]
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!


b32(Posted 2006) [#5]
I think the first line should be changed to:
If gfxAlienOnScreen(x) Then Drawimage etc.
and then be placed in the loop


Jester(Posted 2006) [#6]
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.:(


b32(Posted 2006) [#7]
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 

?


Jester(Posted 2006) [#8]
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!


Adam Novagen(Posted 2006) [#9]
Here's an interesting bit of info about using If statements with True and False operators. Coding something like:
If variable = False
Is the same as saying:
If variable = 0
Or:
If Not variable
Basically, False is equal to zero. True, on the other hand, can represent two different things. If you use it in this fashion:
variable = True
Then True will equal 1, and that value will be stored in variable. If, however, you use it like this:
If variable = True
Then 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 = False
Consider:
If Not variable
And instead of:
If variable = True
Consider:
If variable
That's all. Happy coding!


b32(Posted 2006) [#10]
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.


Adam Novagen(Posted 2006) [#11]
[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. ^_^


Jester(Posted 2006) [#12]
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!


Alaric(Posted 2006) [#13]
Here's an example for you... If you like kitties! lol




b32(Posted 2006) [#14]
ow i'm sorry adam i must have missed it./


Adam Novagen(Posted 2006) [#15]
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 ;)


b32(Posted 2006) [#16]
18-wheels ? :) ok, ok, that is convincing enough. i give up, Gigamaster, you are right.