Hi.
I create a Spaceship that can shoot to enemies and wrote a code for get collide between bullet and enemies. but Problem is when enemy[x] = destroy that bullet[1] collide with enemy[1] and .... .
Another problem is, I delete bullet[x] after collide with enemies and this make decrease bullet.
Please Guide Me.
SetDisplayAspect( 4.0/3.0 )
global MaxBullets = 10
global dim Bullet[MaxBullets]
global BulletNumber = 0
global Player as Integer
global EnemyNum = 10
global dim Enemy[EnemyNum]
global EnemyEmptySprite as Integer
global dim collide[EnemyNum]
//Call Functions
Game()
Function Game()
CreatePlayer()
CreateEnemy()
do
if GetRawKeyState(37) = 1 and GetSpriteX(Player) > 0
SetSpritePosition(Player,GetSpriteX(Player)-0.5,GetSpriteY(Player))
else
if GetRawKeyState(39) = 1 and GetSpriteX(Player) < 95
SetSpritePosition(Player,GetSpriteX(Player)+0.5,GetSpriteY(Player))
endif
endif
if GetRawKeyPressed(32)
CreateBullet()
endif
//Player Fire Bullet
for i = 1 to MaxBullets
if GetSpriteExists(Bullet[i]) = 1
SetSpritePosition(Bullet[i],GetSpriteX(Bullet[i]),GetSpriteY(Bullet[i])-4)
if GetSpriteY(Bullet[i]) < -5
DeleteSprite(Bullet[i])
endif
endif
next i
//Get Collision Between Bullet and Enemies
for x = 1 to EnemyNum
if GetSpriteExists(Enemy[x]) = 1
if GetSpriteExists(Bullet[x]) = 1
collide[x] = GetSpriteCollision(Bullet[x],Enemy[x])
endif
endif
if collide[x] = 1
DeleteSprite(Enemy[x])
DeleteSprite(Bullet[x])
endif
next x
Sync()
loop
EndFunction
//========================>>>>>>>>>>>>>>>Functions
//Create Player Sprite
Function CreatePlayer()
Player = CreateSprite(0)
SetSpriteSize(Player,5,-1)
SetSpritePositionByOffset(Player,50,96.5)
EndFunction
//Create Player Bullet
Function CreateBullet()
BulletNumber = BulletNumber + 1
if BulletNumber > 10 Then BulletNumber = 1
Bullet[BulletNumber] = CreateSprite(0)
SetSpriteSize(Bullet[BulletNumber], 1, 4)
SetSpritePosition(Bullet[BulletNumber],GetSpriteXByOffset(Player)-0.5,GetSpriteYByOffset(Player)-5)
EndFunction
//Create Enemy
Function CreateEnemy()
EnemyEmptySprite = CreateSprite(0)
SetSpriteVisible(EnemyEmptySprite,0)
for a = 1 to 10
if GetSpriteExists(Enemy[a]) = 0 then Enemy[a] = CloneSprite(EnemyEmptySprite)
SetSpriteVisible(Enemy[a],1)
SetSpriteSize(Enemy[a],5,-1)
SetSpritePositionByOffset(Enemy[a],-5+(a*10),20)
SetSpriteColor(Enemy[a],0,255,0,255)
next a
EndFunction