Bullets in arrays
Blitz3D Forums/Blitz3D Programming/Bullets in arrays
| ||
Hi A while ago I posted something about bullets using types, because I was having some problems with them, and someone (I can't remember who...) suggested using an array for bullets instead of a bullet type. Could someone please explain this to me? I don't really know much about arrays other than to create one you do this: dim bullets(100) How would I then hide all the bullets, and then when I want to unhide one for use, how would I do that? Thanks in advance |
| ||
What sort of problems did you have. I would use a type over an array for missiles/bullets myself =) [Edit] A little code snippet... Global gMissNum=0 Global gTime=Millisecs() Global gMissile=CreateCube() PositionEntity gMissile,0,0,-1000; put template missile out of the way Global gShip=LoadMesh("MySpaceshipMesh.b3d") Type missiletype Field mesh End Type ;and in the main loop text 0,0,"Number of active missile types "+gMissNum ;then call this function once per frame Function Update_Missiles() If MouseDown(1) Or KeyDown(157) If MilliSecs()-gTime>100 ;minumum delay between each missile fire missile.missiletype=New missiletype gMissNum=gMissNum+1 missile\mesh=CopyEntity(gMissile) PositionEntity missile\mesh,EntityX(gShip),EntityY(gShip),EntityZ(gShip)+6;position missile at ships gun gTime=MilliSecs() EndIf EndIf For missile.missiletype=Each missiletype MoveEntity missile\mesh,0,0,2 If EntityZ(missile\mesh)>50 ;kill it when it goes out of view FreeEntity missile\mesh Delete missile gMissNum=gMissNum-1 EndIf Next End Function |
| ||
A similar thing with an array might beGlobal gTime=Millisecs() Global gMissile=CreateCube() PositionEntity gMissile,0,0,-1000; put template missile out of the way Global gShip=LoadMesh("MySpaceshipMesh.b3d") Dim missile(100) ;then call this function every frame Function Update_Missiles() If MouseDown(1) Or KeyDown(157) If MilliSecs()-gTime>100 createnewmissile=True EndIf EndIf For m=0 To 99 ;create a new one in an empty array element If createnewmissile=True If missile(m)=0 missile(m)=CopyEntity(gMissile) PositionEntity missile(m),EntityX(gShip),EntityY(gShip),EntityZ(gShip)+6 createnewmissile=False gTime=MilliSecs() EndIf EndIf ;move them If missile(m)<>0 MoveEntity missile(m),0,0,2 If EntityZ(missile(m))>50 FreeEntity missile(m) missile(m)=0 EndIf EndIf Next End Function You don't have to delete the missiles either, you could just leave them offscreen and move them back when they need firing again. |
| ||
Ok, thanks. Just one thing I wanted to check as I couldn't find it in the help files: What does <> do? Does it mean 'Not equal'? |
| ||
Yes you got it. |
| ||
Ok. I just found one problem, though. After firing for a while, I can't fire any more. Why is this happening? Does using arrays mean there is some sort of limit to the ammount of bullets allowed on screen? |
| ||
I fixed it, nevermind. |