Banging my head against a wall with TLists :-<

BlitzMax Forums/BlitzMax Beginners Area/Banging my head against a wall with TLists :-<

RmB303(Posted 2009) [#1]
Right - I may be being thick here, but I'm 'really' stuck, and cannot figure out what's going wrong, so somebody please help!

I'm trying to just get a spaceship to fire, and am learning TLists in the process.

The code below works fine with debug turned off until I try to fire a missile.
It then crashes back to the desk top.

With debug on, I get the error: "Unhandled Exception:Attempt to access field or method of Null object", but it doesn't seem to highlight any particular line.

I've used some print statements to find that the error is in the missile.Draw() call near the end.

I know the "plasma.png" file is ok, because if I use that for the spaceship it draws fine.
The missile instance also appears to have x and y variables assigned (again - discovered by inserting Print commands to the console)

Please help as I've been tinkering with a simple shooting routine for two days now!




dan_upright(Posted 2009) [#2]
the error you're getting is actually on the line
Global spaceship:TSpaceShip=New TSpaceShip

and is caused by these two lines in the TGameObject type definition
	Field width:Int=ImageWidth(Self.image)
	Field height:Int=ImageHeight(Self.image)
you can't use imagewidth/height until there's actually an image loaded to use it on =]

speaking of which, you're not loading an image for the missile anywhere, so that'll be the next error you get


Jesse(Posted 2009) [#3]
there are several thing you are doing wrong. First what dan_upright pointed out. Another one is that for your bullets you are creating an image in the bullet type and in the tgameObject type then you are assigning value to the image in the bullet type and are drawing the image from the base type of which you never assign a value to. just get rid of the "field image:timage" in the bullet type.
When you create a missile, if you are going to save the instance in the list don't create it as global.
another thing is that you are using loadimage everytime you create abullet and that will make your code really slow. I bet you are thinking that sence you incbin the png you are not really downloading it everytime. To a certain degree that is correct but the problem is that "LoadImage" command is grabbing a copy of the PNG and converting it in to a timage everytime you create a bullet. the best thing to do is to load it as a global and then give the address to the current instance that way you are not creating multiple instances of the image file.

I got your code working but I had to modify about six different things. I'm not going to post the fixed code unless you can't figure it out on your own. :)

last thing, Don't forget to give your bullet speed. :)

by the way, you have tlist pretty much figured out.


RmB303(Posted 2009) [#4]
Thanks a lot both of you. I think I was confusing myself with the differences between the Type and each individual instance. It now works fine - just need something to shoot at!

I never considered the fact that I was trying to find the dimensions of an image I hadn't assigned yet, and certainly hadn't noticed I hadn't given the missiles any speed ;-)

Jesse - Thanks for not posting a simple solution for me, it makes more sense having figured it out myself.


Jesse(Posted 2009) [#5]
you know you don't have to use the ImageWidth function. you can access the width and height directly from the image:
myImage:Timage = loadimage("imagefile.png")
print myImage.width
print MyImage.height