Array index out of range + identifier not found.

Monkey Forums/Monkey Beginners/Array index out of range + identifier not found.

Crispy(Posted April) [#1]
Need some help, I'm fairly new to monkey x and I cant understand why this code wont run. It started by giving me the array index out of range for my projectile array when a new projectile was fired (line 211). Then while attempting to fix it, it has begun saying that the Player identifier cannot be found on line 253 and has stopped compiling at all. If I've missed any sections out or if you have any tips, please enlighten me. I apologise for the general poor presentation and such :/




Gerry Quinn(Posted April) [#2]
Player is a field of the single StarshipBastion instance. Projectile needs to be able to find it. I made a global theSB:StarshipBastion which allows it to do so. Projectile can now access theSB.Player.

I also renamed projectileArray to projectiles ('cos it's not an array...). It has to be initialised (done in OnCreate() ). Also, Lists don't have a Length() property. You can call Count() to count the elements, but anyway what you wanted was AddLast() instead.

(Array index out of range happened because arrays have a fixed size. Trying to access the element at length+1 causes this error. Lists can grow freely, and a Stack would also work.)

It runs now but the screen is blank as you aren't creating any projectiles.





Gerry Quinn(Posted April) [#3]
Another problem I just noticed glancing at the code: you are loading a copy of the same image into each projectile. You only want one image for all projectiles. Again, can be a global.


Crispy(Posted April) [#4]
Thank you so much! I would've never been able to see what I did wrong on my own.


Crispy(Posted April) [#5]
Just another quick question, when you say it doesn't run as it isn't creating projectiles, is that because simply adding a new projectile to the list does not create an object of the class? In my mind it should but I cant understand why it can't now create projectiles.


Gerry Quinn(Posted April) [#6]
Sorry, my bad - I don't have your images and when I replaced them with basic graphics I missed some, so it was bugging out. I think the version below is running more or less as intended now - at least the spaceship moves and shoots...




Crispy(Posted April) [#7]
Seems to only shoot in a single direction, will look into it. It also crashes calling line 263/99. Possibly because it should be theSB.projectile.draw() ?

*Edit* It crashes when using the actual graphics. Using circles works though only with the single direction shooting.


Gerry Quinn(Posted April) [#8]
Well, there's something wrong with your images, I would imagine - check out the bananas for examples of image use. It's pretty straightforward. For now, load them all in OnCreate(). And print out the width of each after loading, to make sure they are properly loaded. I'd be surprised if that isn't where the problem lies.

EDIT: you can make them globals, or fields of the app - either way they can be accessed easily and you won't have unwanted extra copies. There are reasons why you might choose to have images belong to other classes, but keep things simple to start with.


Crispy(Posted April) [#9]
Sorry for the long delay, I've been away and my laptop broke on the second day of my trip. I managed to fix the game crashing, and the projectile images are being loaded! The only issue I'm having now is that when the projectiles are fired, both the spaceship and the projectiles colours become inverted. If you'd still be willing to help then I could send you the files? Thanks!!

Edit: Fixed! I've been keeping the circles loading just in case, but after removing them and setcolour everything is working. :)