for...next for bullets
Blitz3D Forums/Blitz3D Programming/for...next for bullets
| ||
| I can't figure out how to make bullets with different weapons see, usually i do but then when I choose another weapon after I fire 16 bullets it says "Array index out of bounds" what do i do? |
| ||
| If you want 16 bullets and you're using a Dim variable... Dim Bullet(15) ;or Global Bullet[15] the number starts at 0 and goes to 15 which means there's 16 all together. Does that help at all? Edit: Btw you forgot an End Select |
| ||
| no GIA_Green_Fire_, what i mean is, i have lets say, 3 weapons (a pistol, with 16 bullets; a shotgun, with 8 bullets; and a sub machine gun, with 30 bullets) with the pistol it works fine, but when i choose another weapon i can only fire 16 bullets and then it says: "Array index out of bounds" That's what i mean |
| ||
| Array index out of bounds... Are you sure you are accessing them correctly. When you create an array for instance "Dim Bullet(16)", you will actually have 16 (0 - 15) elements in the array. The bounds of the array will be as follows... Bullet(0) Bullet(1) Bullet(2) Bullet(3) Bullet(4) Bullet(5) Bullet(6) Bullet(7) Bullet(8) Bullet(9) Bullet(10) Bullet(11) Bullet(12) Bullet(13) Bullet(14) Bullet(15) If you tried to access "Bullet(16)", you wil get the message "Array index out of bounds" I hope that helps. |
| ||
| Wrong - in blitz dim myarray(10) gives you access to 11 elements 0-10. Types are much better suited to bullet/projectile than arrays (my own preference anyway). |
| ||
| Matty... I stand corrected. ;) Dim myarray(10) For i = 0 To 10 myarray(i) = i Next For i = 0 To 10 Print myarray(i) Next WaitKey() End |
| ||
I'm trying to find a projectile example using Blitz3d. I found this one, but it seems too mush function involved. If anyone have a simple way to do it, please can anyone post it.
; Add projectile Type
Type Projectile
Field sprite, time_out
End Type
; Add Explosion Type
Type Explosion
Field sprite, scale#
End Type
; Load Projectile Sprites
explosion_sprite = LoadSprite( "explosion.bmp" )
HideEntity explosion_sprite
projectile_sprite = LoadSprite( "heart.bmp" )
EntityRadius projectile_sprite, 2
EntityType projectile_sprite, coll_projectile
HideEntity projectile_sprite
Global player, projectile_sprite, explosion_sprite, hit, shoot, boom
Function object_key_control( obj )
; Add spacebar = fire
If KeyHit(57) Then CreateProjectile( player )
End Function
Function UpdateProjectiles()
For P.Projectile = Each Projectile
UpdateProjectile( P )
Next
For E.Explosion = Each Explosion
UpdateExplosion( E )
Next
End Function
Function CreateProjectile.Projectile( source )
P.Projectile = New Projectile
P\time_out = 150
P\sprite = CopyEntity( projectile_sprite, source )
; Don't want projectiles coming out of player's feet :)
MoveEntity P\sprite, 0, 2, 0
EntityParent P\sprite, 0
shootChannel = PlaySound ( shoot )
Return P
End Function
Function UpdateProjectile( P.Projectile )
If CountCollisions( P\sprite )
If EntityCollided( P\sprite, coll_characters )
For k = 1 To CountCollisions( P\sprite )
hit = CollisionEntity( P\sprite, k )
If GetEntityType( hit ) = coll_characters
Exit
EndIf
Next
boomChannel = PlaySound (boom)
CreateExplosion( P )
FreeEntity P\sprite
Delete P
Return
EndIf
EndIf
P\time_out = P\time_out - 1
If P\time_out = 0
FreeEntity P\sprite
Delete P
Return
EndIf
MoveEntity P\sprite, 0, 0, 1
End Function
Function CreateExplosion.Explosion( P.Projectile )
E.Explosion = New Explosion
E\scale = 1
E\sprite = CopyEntity( explosion_sprite, P\sprite )
EntityParent E\sprite, 0
Return E
End Function
Function UpdateExplosion( E.Explosion )
If E\scale < 5
E\scale = E\scale + .2
ScaleSprite E\sprite, E\scale, E\scale
Else
FreeEntity E\sprite
Delete E
For Character.Sprite = Each Sprite
If Character\ID = hit Then Delete Character : FreeEntity hit
Next
EndIf
End Function
I'm trying to understand this one myself. I will get it eventually. Source code from: http://jnoodle.com/Blitz3D/lesson8.html |
| ||
| The code doesn't have to be difficult for projectiles: Example code: Const Width = 800, Height = 600
Graphics3D Width, Height, 32, 1
SetBuffer BackBuffer()
Type Bullet
Field Entity
End Type
;Load stuff...
BulletTemplate = LoadMesh("Bullet.3ds")
;Set up stuff...
Camera = CreateCamera()
CameraViewport Camera, 0, 0, Width, Height
CameraRange Camera, 1, 45
;Main code and loop here...
;E.G:
While Not Keyhit(1)
If KeyHit(57) Then
Bullets.Bullet = New Bullet
Bullets\Entity = CopyEntity(BulletTemplate)
ShowEntity Bullets\Entity
EndIf
UpdateBullets()
UpdateWorld
RenderWorld
Flip
Wend
;Function called once per loop to update bullets
Function UpdateBullets()
For Bullets.Bullet = Each Bullet
MoveEntity Bullets\Entity, 0, 0, 1
If EntityZ(Bullets\Entity) > 50 Then
FreeEntity Bullets\Entity
Delete Bullets
EndIf
Next
End FunctionThe beauty of using types rather than an array is that you can have as many or as little bullets as you want. Each loop the function UpdateBullets is called which goes through each bullet and moves it, deleting it if it goes out of view of the camera. |
| ||
| indentation makes code unreadable. |