advice on TLink please
BlitzMax Forums/BlitzMax Beginners Area/advice on TLink please
| ||
For a space invaders clone, you'll be aware only one invader moves per frame. I'm having a problem whereby in my mainloop I check all invaders and remove those in collision from the alienObjectList In addition, in order to move one invader per frame, I set a variable before my main loop: Link:TLink = AlienObjectList.FirstLink() Then inside the main loop I do this: for each alien.draw() ' draw all aliens alien.collisionCheck() ' remove from AlienObjectList if collision next finally I use the Link variable to update the position of one invader per frame... If Link <> Null Local MyObject:TAlien1 = TAlien1(Link.Value()) 'cast object MyObject.updatestate() ' update the onscreen position Link = Link.NextLink() Else Link = AlienObjectList.FirstLink() EndIf I'm getting crashes during play because I think when doing checkCollision() and removing an alien from the list, its messing up the Link variable. Is there a better way ? Error: Attempt to access field or method of Null Object |
| ||
Don't use a link to keep track of which alien you are at, but use an index variable instead. Something likeLocal i:Int = 0 ... 'Mainloop: i :+ 1 If i>list.Count() Then i = 0 Local j:Int For alien = EachIn list alien.draw() ' draw all aliens alien.collisionCheck() ' remove from AlienObjectList if collision If i=j Then alien.UpdateState() j :+ 1 Next |
| ||
As a personal opinion I wouldnt bother storing the invaders in a list. I went down that same route when I wrote mine and eventually I found it far easier to treat the invaders as a single InvaderWave object. It would store the individual invaders in a 2d array and keep track of what direction they should be moving, what individual should be moving next, and which invader should be dropping a bomb. I used TLists for storing the falling bombs and the explosions which I upgraded to a simple particle effect explosion. |
| ||
OTUS thanks - will give it a try EDITED: Otus, I implemented your change and it's working perfectly. Thanks QuietBloke Yes, I thought about an array which would certainly be much easier as there's a known quantity of invaders and each could be given a display flag based on whether they've been hit or not. But this was meant to be an exercise for me to get my head around types and lists and OO, so I ignored that route. Might get a separate version running like this. |
| ||
thats fine.. no better way to learn than to get stuck in. Good luck. :) |