@baxslash
I compound my code with your code and create this code and seem enemy shoot randomly. please see and tell this way is correct or no? is there better way?
//Enemy Fire Bullet
for x = 1 to MaxBullets
if GetSpriteExists(Enemy) = 1
if GetSpriteExists(EnemyBullet[x]) = 0
if time# < Timer() then time# = time# + 0.01
if time# >= Timer()
EnemyBullet()
time# = Timer() - Random(0,1000) * 0.01
endif
endif
endif
next x
and this is my full code:
SetDisplayAspect( 4.0/3.0 )
global MaxBullets = 10
global dim Bullet[MaxBullets]
global BulletNumber = 0
global Player as Integer
global Enemy as Integer
global dim EnemyBullet[MaxBullets]
global EnemyBulletNumber = 0
global time# = 0.0
//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
Print (timer())
Print (time#)
//Enemy Fire Bullet
for x = 1 to MaxBullets
if GetSpriteExists(Enemy) = 1
if GetSpriteExists(EnemyBullet[x]) = 0
if time# < Timer() then time# = time# + 0.01
if time# >= Timer()
EnemyBullet()
time# = Timer() - Random(200,1000) * 0.01
endif
endif
endif
next x
for j = 1 to MaxBullets
if GetSpriteExists(EnemyBullet[j]) = 1
SetSpritePosition(EnemyBullet[j],GetSpriteX(EnemyBullet[j]),GetSpriteY(EnemyBullet[j])+3)
if GetSpriteY(EnemyBullet[j]) > 105
DeleteSprite(EnemyBullet[j])
endif
endif
next j
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()
Enemy = CreateSprite(0)
SetSpriteSize(Enemy,5,-1)
SetSpritePositionByOffset(Enemy,50,20)
SetSpriteColor(Enemy,0,255,0,255)
EndFunction
//Create EnemyBullet
Function EnemyBullet()
EnemyBulletNumber = EnemyBulletNumber + 1
if EnemyBulletNumber > MaxBullets
EnemyBulletNumber = 1
endif
EnemyBullet[EnemyBulletNumber] = CreateSprite(0)
SetSpriteSize(EnemyBullet[EnemyBulletNumber],1,4)
SetSpriteColor(EnemyBullet[EnemyBulletNumber],0,255,0,255)
SetSpritePosition(EnemyBullet[EnemyBulletNumber],GetSpriteXByOffset(Enemy)-0.5,GetSpriteYByOffset(Enemy)+5)
EndFunction
UPDATE: this is above code with little different that there is 10 enemies and I want randomly fire them, but after compile give me an error, please tell me where is the problem?
Problem is relate to below code and when I changed Enemy to Enemy(x).
//Enemy Fire Bullet
for x = 1 to MaxBullets
if GetSpriteExists(Enemy[x]) = 1
if GetSpriteExists(EnemyBullet[x]) = 0
if time# < Timer() then time# = time# + 0.01
if time# >= Timer()
EnemyBullet()
time# = Timer() - Random(200,1000) * 0.01
endif
endif
endif
next x
this is full code:
SetDisplayAspect( 4.0/3.0 )
global MaxBullets = 10
global dim Bullet[MaxBullets]
global BulletNumber = 0
global Player as Integer
global dim Enemy[10]
global dim EnemyBullet[MaxBullets]
global EnemyBulletNumber = 0
global time# = 0.0
//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
Print (timer())
Print (time#)
//Enemy Fire Bullet
for x = 1 to MaxBullets
if GetSpriteExists(Enemy[x]) = 1
if GetSpriteExists(EnemyBullet[x]) = 0
if time# < Timer() then time# = time# + 0.01
if time# >= Timer()
EnemyBullet()
time# = Timer() - Random(200,1000) * 0.01
endif
endif
endif
next x
for j = 1 to MaxBullets
if GetSpriteExists(EnemyBullet[j]) = 1
SetSpritePosition(EnemyBullet[j],GetSpriteX(EnemyBullet[j]),GetSpriteY(EnemyBullet[j])+3)
if GetSpriteY(EnemyBullet[j]) > 105
DeleteSprite(EnemyBullet[j])
endif
endif
next j
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()
for a = 1 to 10
Enemy[a] = CreateSprite(0)
SetSpriteSize(Enemy[a],5,-1)
SetSpritePositionByOffset(Enemy[a],-5+(a*10),20)
SetSpriteColor(Enemy[a],0,255,0,255)
next a
EndFunction
//Create EnemyBullet
Function EnemyBullet()
EnemyBulletNumber = EnemyBulletNumber + 1
if EnemyBulletNumber > MaxBullets
EnemyBulletNumber = 1
endif
EnemyBullet[EnemyBulletNumber] = CreateSprite(0)
SetSpriteSize(EnemyBullet[EnemyBulletNumber],1,4)
SetSpriteColor(EnemyBullet[EnemyBulletNumber],0,255,0,255)
SetSpritePosition(EnemyBullet[EnemyBulletNumber],GetSpriteXByOffset(Enemy)-0.5,GetSpriteYByOffset(Enemy)+5)
EndFunction
Thanks