Author Topic: need help with types and when it comes to deleting them  (Read 5394 times)

0 Members and 1 Guest are viewing this topic.

Offline quinn3191

  • ZX 81
  • *
  • Posts: 3
  • Karma: 0
    • View Profile
hi I'm dazzy, I'm creating a 2d tank game


i have a problem with the game that i am trying to create. the problem is Ive created a Type for some 'boxes' that will be displayed on the screen, i want to be able to display four instances of this type ... i can do that no problem ...the actually problem is when it comes to bullets colliding with the 'boxes' ...the bullets are suppose to destroy the box and then be deleted and then replace by an image (of a damaged box) the problem is that it appears the boxes can only be destroyed in a sequence... e.g. how they were created ...1,2, 3, 4 ...i want to know how to stop this so it doesn't matter which box is hit by the bullets first ...and also need to know how to replace the image of the box with the damaged box image....

any help that anyone can give me would be much appreciated :D

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
You could add an 'IsDead' flag to your type and use that to determine whether to display/process it or not.

Welcome to the forum dazzy!

Jim
Challenge Trophies Won:

Offline quinn3191

  • ZX 81
  • *
  • Posts: 3
  • Karma: 0
    • View Profile
hi jim,

how do you mean ? sorry ive only just started to learn... so just a little tiny explanation :)
 the problem is that im having is that when i fire the bullets at one of the boxes it deletes, but theres certain box you have to destroy first ...so say like i fire at the other boxes it says the error 'object does not exist'

belows the code that checks if the images collide :

For Player.shell = Each shell
      For b.box = Each box
          If ImagesCollide (shellimage, Player\x, Player\y,0,  Computer, b\bX, b\bY,0 )
            Delete Player
            Delete b         
         EndIf
              
      Next
   Next
 

thanks so much for your help btw :D

Offline zawran

  • Sponsor
  • Pentium
  • *******
  • Posts: 909
  • Karma: 67
    • View Profile
When your code is running, it will try and go though all of the box objects for each of the player shells, but since you might be deleting some of the box objects within the box loop you will get an error since it can no longer find that object when it runs the box loop next time. Your box and shell objects should perhaps have a delete field you could set while in the loop and then have some other code that goes though the list of objects afterwards and deals with those objects that needs to be deleted. I hope that made sense.

Offline quinn3191

  • ZX 81
  • *
  • Posts: 3
  • Karma: 0
    • View Profile
I think so :) , any idea on how I would go through the objects to to check which ones need to be deleted, just a push in the right direction is all I need :)


thanks for your help

Offline zawran

  • Sponsor
  • Pentium
  • *******
  • Posts: 909
  • Karma: 67
    • View Profile
Its been quite a while since I have used Blitz3D and BlitzPlus, so I might not remember the syntax exactly.

Code: [Select]
' here is your double loop where you find out if a shell collide with a box
For Player.shell = Each shell
      For b.box = Each box
          If ImagesCollide (shellimage, Player\x, Player\y,0,  Computer, b\bX, b\bY,0 )
            player\deleteflag = 1
            b\deleteflag = 1
         EndIf
      Next
   Next
' now you go though each shell to see if it needs to be deleted
For Player.shell = Each shell
   if player\deleteflag = 1 then delete player
next
' and then you go though each box to see if it needs to be deleted
For b.box = Each box
   if b\deleteflag = 1 then delete b
next

Off the top of my mind I don't remember any way around this unfortunately. And welcome to the forum btw :)

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
What I meant was if you have an IsDead flag then you do not need to delete anything.
Code: [Select]
For Player.shell = Each shell
   If Not Player.IsDead
      For b.box = Each box
          If ImagesCollide (shellimage, Player\x, Player\y,0,  Computer, b\bX, b\bY,0 )
            Player.IsDead = True
            b.IsDead = True       
         EndIf             
      Next
    EndIf
 Next

Then in your display/update code you can go

Code: [Select]
For Player.shell = Each shell
   If Not Player.IsDead
      UpdateAndDisplayPlayer(Player)
  End If
Next
For b.box = Each box
   If Not b.IsDead
      UpdateAndDisplayBox(b)
   EndIf
Next

That way you are not doing any deletion, you're just switching the objects on and off.

Jim
Challenge Trophies Won: