Problem with a "test" game
BlitzPlus Forums/BlitzPlus Beginners Area/Problem with a "test" game
| ||
Hi everyone!, I'm new on this forum, and, of course, in blitz programming. I've been trying to create a little game in blitzplus, but I have found a problem that I can't solve. Could you help me? The case is that when I try to kill the "enemy navs" they don't disappear, and if they do, the program crashes. I'm sure that this is a very simple problem, but how I'm new in this word, I don't see it. Anyway, thanks a lot. Does anybody know how to kill the enemies? Oh, yes, this program structure is the worst in the world, I know it. :-) If you want to see the code in an editor: http://rapidshare.com/files/77814890/movement_test2.bb ---------- ;Variables Global player.nav = New nav Global bullet.ball = New ball Global enemy.nav = New nav Global leftkeyr = 1 Global randomnumber Global randomgrade = 1000 Global maximumx,maximumy,minimx,minimy Const rightkey = 205 Const leftkey = 203 Const upkey = 200 Const downkey = 208 Const spacekey = 31 Const esckey = 1 Const ball1$ = "" Const nav1$ = "<-*->" Const startx% = 400 Const starty% = 300 Const startvel = 5 ;------------------------------------------------------------------------- ;Types Type nav Field x,y Field vel Field navimage$ End Type player\x = startx player\y = starty player\vel = startvel player\navimage = nav1$ Type ball Field x,y Field vel Field ballimage End Type ;------------------------------------------------------------------------- ;######################################################################### ;Main loop Graphics 800,600,16,2 SeedRnd MilliSecs SetBuffer BackBuffer () While Not KeyHit (esckey) Cls userinput () drawplayer () drawbullets () drawenemies () deletenemies () Flip Delay 20 Wend End ;######################################################################### ;========================================================================= ;Functions ; ;========================================================================= ;draw player Function drawplayer () Text player\x,player\y,player\navimage,True,True End Function ;------------------------------------------------------------------------- ;draw bullets Function drawbullets () For bullet.ball = Each ball If (bullet\y > -10) Then Text bullet\x,bullet\y,"*",True,True bullet\y = bullet\y - bullet\vel End If Next End Function ;draw enemies Function drawenemies () randomnumber = Rnd(0,randomgrade) If randomnumber < 5 Then enemy.nav = New nav enemy\navimage = "(OO)" enemy\x = Rnd (20,790) enemy\y = Rnd (20,590) End If For enemy.nav = Each nav Text enemy\x,enemy\y,enemy\navimage,True,True Next randomgrade = randomgrade - 1 End Function ;eliminate enemies Function deletenemies () For bullet.ball = Each ball For enemy.nav = Each nav If (enemy\x < (bullet\x + 10)) And (bullet\x > (bullet\x - 10)) Then Delete enemy End If Next Next End Function ;------------------------------------------------------------------------- ;User input Function userinput () If KeyDown (leftkey) Then player\x = player\x - player\vel End If If player\x <=23 Then player\x = 23 End If If KeyDown (rightkey) Then player\x = player\x + player\vel End If If player\x >= 777 Then player\x = 777 End If If KeyDown (upkey) Then player\y = player\y - player\vel End If If player\y <=5 Then player\y = 5 End If If KeyDown (downkey) Then player\y = player\y + player\vel End If If player\y >= 595 Then player\y = 595 End If If KeyDown (31) Then bullet.ball = New ball bullet\x = player\x bullet\y = player\y bullet\vel = 10 bullet\ballimage = "*" End If End Function ;========================================================================= Again, thanks! |
| ||
The reason is because in your delete enemies function the first 'enemy' the bullet collides with is the 'player' 'nav' type and so it deletes the 'player' 'nav' type instance which means when the code gets to the userinput function the 'player' instance is now null so it crashes as soon as it is referred to. What you need to do to fix it is to prevent the player's own bullets from colliding with it. Also, if I were to give advice I would suggest not using a global type called 'player' but simply have a field within the 'nav' type object which flags whether the nav is an enemy or a player (you could even do the ball/bullet in there as well). |
| ||
Thanks you a lot! now it works! Anyway, is sure that this "game" needs a lot of reviews, but this is a good step to make it work. Ruben |