06-05-2024, 08:57 AM
(05-31-2024, 09:00 PM)Marcus Wrote: Here's step 6. ...
STEP 6. What if the enemy can shoot you ?
click image to zoom in
Enemy shoots randomly, the bullet goes at the same direction as the enemy movement.
click image to zoom in
When the enemy moves toward you, they begin to shoot you.
Question :
How to use collision detection function when the enemy's bullet hit you at certain area ?
Mine is still error ....
You may answer when you have free time, don't worry, it's not urgent
Code:
'----------------
' INITIALIZATION
'----------------
...
constant ENEMY_BULLET_ID = 8
vEnemyBulletImage = loadimage("assets6/enemy_bullet.png",4,1)
...
'-----------
' GAME LOOP
'-----------
EA_Run()
'-----------
' FUNCTIONS
'-----------
function CreatePumpkinEnemy(x, y, z)
enemy = EA_Object()
enemy.id = PUMPKIN_ID
...
enemy.Update = function(dt)
.stimer = max(.stimer - dt, 0)
if .stimer = 0
.stimer = 0.5
EA_AddObject(CreateEnemyBullet(
ENEMY_BULLET_ID,
vEnemyBulletImage,
.X() + .DX()*0.2, .Y() - 0.3 + .DY()*0.2, .Z() + .DZ()*0.2,
.DX()*4, .DY()*4, .DZ()*4))
endif
...
endfunc
...
endfunc
function CreateEnemyBullet(id, img, x, y, z, dx, dy, dz)
b = EA_Object()
b.id = id
b.SetPos(x, y, z)
b.SetSprite(img, 0, false)
b.SetHeight(height(img)/64)
b.SetRadius(0.5*width(img)/64)
b.dx = dx
b.dy = dy
b.dz = dz
b.numCels = cels(img)
b.cel = 0
b.Update = function(dt)
if .numCels > 1
.cel = (.cel + dt*30)%.numCels
.SetCel(int(.cel))
endif
res = .Move(.dx*dt, .dy*dt, .dz*dt, 0)
if res.any
EA_RemoveObject(this)
else
'Error collision detection enemy bullet and player
'if .CollidesWith(PLAYER_ID)
' EA_RemoveObject(PLAYER_ID) 'remove player
'endif
endif
endfunc
return b
endfunc