HI,
I am am having trouble with a small game that i am working on. The problem lies with the collision of the bullet and the enemy. For some reason i can only get the enemy to delete when the collision is detected, if i place in the command to delete the curBullet when the collision is detected i recieve a memory access error and the program ends. I don't understand why the error occurs, i am assuming that i need to place the delete command in a different place but so far i have not been successful,
Thanks to all
-Webfaction
ps: Im am using blitz3d by the way
;Creates variables to represent screenwidth and screenheight
Global sh=480
Global sw=640
;Sets Graphics Mode
Graphics sw,sh,0,0
;Include Files
Include "Spark.bb"
;Sets the buffer that we are currently writing on to be the "BackBuffer"
SetBuffer BackBuffer()
Global star=LoadImage("star.bmp")
Global shipl=LoadImage("ship4.bmp")
Global shipr=LoadImage("ship3.bmp")
Global r1=LoadImage("r1.bmp")
Global r2=LoadImage("r2.bmp")
Global r3=LoadImage("r3.bmp")
Global name$
Global enum=1
Global h=0
;Create Types
Type obj
Field x,y,i,h,f,w,hi
End Type
Type bullet
Field x,y,i$,h,f
End Type
Type enemy
Field x,y,i$,h,w,hi
End Type
Global x=0
Global y=0
;Create Player
Global player.obj = New obj
player\x=100
player\y=100
player\i=LoadImage("ship3.bmp")
player\hi=15
;Spark inverse position(s)
Global sparkx= player\x+8
Global sparky=player\y+11
; Function to draw everything
Function draw()
TileBlock star,x,y
; Draw player
DrawImage player\i,player\x,player\y
; Draw bullets
For curBullet.bullet = Each bullet
DrawImage curBullet\i, curBullet\x, curBullet\y
Next
; Draw enemies
For curenemy.enemy = Each enemy
MaskImage curenemy\i,0,0,0
DrawImage curenemy\i,curenemy\x,curenemy\y
Next
x=x-2
End Function
;------------------------------------------------------------
; Borders for plane
Function border()
If player\x>= sw-player\w Then player\x=sw-player\w
If player\x=<0 Then player\x=0
If player\y>=sh-player\hi Then player\y=sh-player\hi
If player\y=<0 Then player\y=0
End Function
;------------------------------------------------------------
;Creates HUD
Function HUD()
Text 0,0,"X Position:"+player\x
Text 0,10,"Y Position:"+player\y
Text 125,0,"Player Name:"+ name$
End Function
;------------------------------------------------------------
Function CreateBullet()
newBullet.bullet = New bullet
newBullet\x = player\x+15
newBullet\y = player\y +10
newBullet\i = LoadImage("bullet.bmp")
newBullet\h = 0
newBullet\f = 1
End Function
;------------------------------------------------------------
Function CreateEnemy()
If enum < 5 Then
enemy.enemy= New enemy
enemy\x=Rand(1,4)
enemy\y=Rand(1,5)
enemy\i=Rand(1,3)
If enemy\i=1 Then enemy\i= r1
If enemy\i=2 Then enemy\i=r2
If enemy\i=3 Then enemy\i=r3
;Sets A Rand Y
If enemy\y= 1 Then enemy\y=10
If enemy\y =2 Then enemy\y=134
If enemy\y =3 Then enemy\y=271
If enemy\y= 4 Then enemy\y=304
If enemy\y =5 Then enemy\y=567
;Sets A Rand X
If enemy\x=1 Then enemy\x=745
If enemy\x=2 Then enemy\x=700
If enemy\x=3 Then enemy\x=680
If enemy\x=4 Then enemy\x=800
enum=enum+1
End If
End Function
;------------------------------------------------------------
Function MoveBullets()
For curBullet.bullet = Each bullet
curBullet\x = curBullet\x + 5
Next
End Function
Function MoveEnemy()
For curenemy.enemy = Each enemy
curenemy\x=curenemy\x-3
Next
End Function
;------------------------------------------------------------
Function CheckCollisions()
For curBullet.bullet = Each bullet
; Delete bullet If it has passed screen
If curBullet\x > sw Then
Delete curBullet
Else
For curEnemy.enemy = Each enemy
If ImagesCollide(curenemy\i,curenemy\x,curenemy\y,0,curBullet\i,curBullet\x,curBullet\y,0) Then
Delete curEnemy
End If
enum=-enum - Rand(1,2)
Next
End If
Next
End Function
;------------------------------------------------------------
;name$=Input("Enter Your Name ")
;Main Game Loop
While KeyDown(1)=0
Cls
;Scan for input
If KeyDown(200) Then player\y = player\y - 2
If KeyDown(208) Then player\y = player\y + 2
If KeyDown(203) Then player\x = player\x - 2 ;sparkx=(playerx+18)
If KeyDown(205) Then player\x = player\x + 2 player\i=shipr
;If Spacebar is pressed then fire a bullet
If KeyHit(57) Then CreateBullet()
CreateEnemy
MoveBullets
MoveEnemy
CheckCollisions
border
draw
HUD
createspark
renderspark
;Flips everything from the backbuffer to the front buffer
Flip
Wend