bullet help

Blitz3D Forums/Blitz3D Beginners Area/bullet help

Hi, I'm new to blitz, I'm current;y working on my 3rd blitz game and I am trying to make it one of my finest. My problem is when I hit the spacebar the bullet appears for about half a second I think it has somethinf to do with this line

If KeyHit(57) And hpistol = 1 And pistol\ammo > 0
b.bullet = New bullet

b\x = pistol\x

b\y = pistol\y

pistol\ammo = pistol\ammo - 1
b\x = b\x + 1
DrawImage(ibullet,b\x,b\y)

If pistol\ammo <= 0
pistol\ammo = 0
EndIf
EndIf

Can some one help me?

Read this tutorial:

http://www.blitzbasic.co.nz/Community/posts.php?topic=35127

I commented and got a lot of help but I think it can help you.

-Rook

I know your error,'cause i made it sometimes :P
If you put the "drawimage"command in the same "if keyhit" ecc ecc you are drawing the image if the button is pressed,so when the button isn't pressed the image is not drawn!

Try modify in this manner your function:
If KeyHit(57) And hpistol = 1 And pistol\ammo > 0
b.bullet = New bullet

b\x = pistol\x

b\y = pistol\y

pistol\ammo = pistol\ammo - 1
b\x = b\x + 1
;DrawImage(ibullet,b\x,b\y)commented!!must be uotside the loop!!

If pistol\ammo <= 0
pistol\ammo = 0
EndIf
EndIf 
DrawImage(ibullet,b\x,b\y)   ;must be outside and it works!


Valgar, I think that is only slightly better, this should do what you want:
If KeyHit(57) And hpistol = 1 And pistol\ammo > 0
b.bullet = New bullet

b\x = pistol\x

b\y = pistol\y

pistol\ammo = pistol\ammo - 1

If pistol\ammo <= 0
pistol\ammo = 0
EndIf
EndIf
For b.bullet = Each bullet
  b\x = b\x + 1
  DrawImage(ibullet,b\x,b\y)
Next


Yes,i've made only this light modification for helping him only to know what caused the error.
He must add the "for--next"loop to cicle trough the bullet type(my mistake sorry)before using outside the function any of the bullet fields...

Thank you soooo much Now I am going to make some grenades for this :P

alright I need some more help, when I try to switch my guns everything works out fine but when I try to shoot them seperate, nothing comes out of the shotgun but both bullets come out of the pistol

AppTitle "Attack of the Evil Smileys"
Graphics 800,600,0,2
SetBuffer BackBuffer()

;load images

character = LoadImage("Images\killer.png")
MaskImage character,255,0,255

smiley = LoadAnimImage("Images\smiley.png",38,36,0,2)
MaskImage smiley,255,0,255

healthpack = LoadImage("Images\healthbar.png")

ishotgun = LoadImage("Images\ishotgun.png")
MaskImage ishotgun,255,255,255

ipistol = LoadImage("Images\ipistol.png")
MaskImage ipistol,255,255,255

ibullet = LoadImage("Images\ibullet.png")
MaskImage ibullet,255,255,255

isbullet = LoadImage("Images\isbullet.bmp")
MaskImage isbullet,255,255,255

Type player
	Field x,y
	Field health
	Field image
	Field lives
	Field score
End Type

Type enemy
	Field x,y
	Field health
	Field image
	Field frame
End Type

Type health_pack
	Field x,y
End Type

Type shotgun
	Field x,y
	Field ammo
End Type

Type pistol
	Field x,y
	Field ammo
End Type

Type bullet
	Field x,y
End Type

player.player = New player
enemy.enemy = New enemy
health_pack.health_pack = New health_pack
shotgun.shotgun = New shotgun
pistol.pistol = New pistol


player\x = 400
player\y = 500
player\health = 100
player\lives = 3
player\score = 0

enemy\x = 100
enemy\y = 500
enemy\health = 100

health_pack\x = 500
health_pack\y = 500

shotgun\ammo = 25
pistol\ammo = 30


; if player is facing left or right
direction = 1
;Holding Gun?
hshotgun = 0
hpistol = 0
hm4a1 = 0
eshotgun = 0
epistol = 0
em4a1 = 0



While Not KeyHit(1)


	
;move player
If KeyDown(203)
player\x = player\x - 2
direction = 0
EndIf
If KeyDown(205)
player\x = player\x + 2
direction = 1
EndIf





;update colliding thanks to shane from blitzcoder
For h.health_pack = Each health_pack
  If ImagesCollide(character,player\x,player\y,0,healthpack,h\x,h\y,0) Then
    player\health = player\health + 20
    Delete h

    If player\health >= 150
      player\health = 150
    EndIf
  EndIf
Next




;update characters

	If ImagesCollide(character,player\x,player\y,0,smiley,enemy\x,enemy\y,0) And enemy\frame <>1
	player\health = player\health - 10
	enemy\frame = enemy\frame + 1
EndIf







;update ghetto hud

Locate 0,20 Print "lives: " + player\lives
Locate 0,40 Print "Score: " + player\score
Locate 0,0 Print "health: " + player\health





;draw the characters
DrawImage(character,player\x,player\y)

For h.health_pack = Each health_pack
DrawImage(healthpack,h\x,h\y)
Next

DrawImage(smiley,enemy\x,enemy\y,enemy\frame)

If KeyHit(2)
hshotgun = 1
hpistol = 0
EndIf

If KeyHit(3)
hpistol = 1
hshotgun = 0
EndIf

If hshotgun = 1
shotgun\x = player\x + 5
shotgun\y = player\y + 25
DrawImage(ishotgun,shotgun\x,shotgun\y)
Locate 500,10 Print "Ammo: " + shotgun\ammo
EndIf

If hpistol = 1 
pistol\x = player\x + 35
pistol\y = player\y + 6
DrawImage(ipistol,pistol\x,pistol\y)
Locate 500,10 Print "Ammo: " + pistol\ammo
EndIf




If KeyHit(57) And hpistol = 1 And pistol\ammo > 0 
b.bullet = New bullet

b\x = pistol\x

b\y = pistol\y

pistol\ammo = pistol\ammo - 1

If pistol\ammo <= 0
pistol\ammo = 0
EndIf
EndIf
For b.bullet = Each bullet
  b\x = b\x + 6
  DrawImage(ibullet,b\x,b\y)
Next

If KeyHit(57) And hshotgun = 1 And shotgun\ammo > 0
s.bullet = New bullet

s\x = shotgun\x
s\y = shotgun\y

shotgun\ammo = shotgun\ammo - 1


If shotgun\ammo <= 0
shotgun\ammo = 0
EndIf
EndIf
For s.bullet = Each bullet
  s\x = s\x + 8
  DrawImage(isbullet,s\x,s\y)
Next


Flip
Cls
Wend
End


Dude, when you loop trough all bullets, you loop trough ALL bullets. If you write s.bullet = New bullet or b.bullet = New bullet doesn't matter, they are all the same. Either you can create two different types, or you can add a few extra attributes to your type, for now you will need a speed field and an image handle field, later on you will probably need a field for the damage that the bullet will do. I don't know why your shotgun doesn't fire.

I have resolved this to make a different type for every direction as eBusiness say.
So you must threat the types in different manner accordingly to the direction.