Enemy shooting delay

This topic is 3814 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hey everyone, I am currently coding a 2d shooter for a school project, and am wondering what kind of code I can use to delay a function that I have set up to shoot bullets by say .5 seconds between shots. My current implementation isn't working very well and I am looking for some insight. Thanks in advance. EDIT: Should probably add that I am using Visual C++ and Direct X to code this.

Share on other sites
It's hard to help without the code you're using, but I can suggest one way to achieve it.

Every time a shot is performed, store the current time, or current ticks. Then when the next shot is requested, check this "last shot" time against the current time or ticks. If the difference is less than what ever delay you're after then do nothing, otherwise the shot is performed again and the time stored once again.

Hope this isn't too vague.

Share on other sites
It would help if you post your previous implementation so we can see what you're doing wrong.

Share on other sites
ok I know this code isn't anywhere near correct but here it is, broken up from the file so you can get an idea of what i am doing. As of right now the bullet spawns at the correct location, but sort of gets stuck there moving back and forth for a few seconds before moving down the screen. Thank you very much for looking at my code.

the time declaration...
static int currTime = time(NULL);

where its used...

if (currTime >= 800){
PlaySound(sound_hit);
for (int i = 0; i <= 5; i ++){
enbullet.y = (float)enemyArray.y + 30;
enbullet.x = (float)enemyArray.x + 30;}

and in the draw function...

for(int i=0; i<=5; i++){
RECT enbulletRect;
int bcolumns = 8;
enbulletRect.left = (enbullet.curframe % bcolumns) * enbullet.width;
enbulletRect.top = (enbullet.curframe / bcolumns) * enbullet.height;
enbulletRect.right = enbulletRect.left + enbullet.width;
enbulletRect.bottom = enbulletRect.top + enbullet.height;

epos.x = enbullet.x;
epos.y = enbullet.y;
sprite_handler->Draw(
enbullet_image,
&enbulletRect,
NULL,
&epos,
D3DCOLOR_XRGB(255,255,255));

//move the bullet
enbullet.y -= enbullet.movey;}

Share on other sites
I wrote up a quick method for you.

/****************************************************//*****NOTE: THIS IS NOT THE EXACT AND PROPER WAY*****//*****TO SET THUS UP. THIS IS MEANT FOR EXAMPLE *****//*****AND BASIC STRUCTURE OUTLINE. Thank you.   *****//****************************************************///first we create the delays and delay holdersconst int TICK_DELAY = 5;int _tick = 0;/*Check to see if _tick is passed TICK_DELAY, if so, return that you can shoot*/bool ifEnemyShoot(int TickDelay, int currentTick){if(currentTick > TickDelay){return true;}else {return false;}}//....//void EnemyShoot(){//If we can shoot then we obviously have passed TICK_DELAY_tick=0;/*Shooting functions like launching the attack goes here*/}//..../*In game phases. This Handles the Enemy so we won't have a big messy block of code*/void HandleEnemy(){//blah... stuff for enemy goes here//Go through methods to see if you can shoot, if so, Shoot.if(ifEnemyShoot(TICK_DELAY, _tick){EnemyShoot();}}//....//GameEngine section, this is not the correct way to set this up, but...void Update_Game(){_tick+=1;HandleEnemy();}

There you have it, a basic layout for you to have fun with. =D
Insert me in credits if used at all. <You know, I like credits; I go by Axesor>
Also, if you need anymore help [with this pr anything] PM me