Enemy shooting delay

Started by
3 comments, last by Maverick Programmer 16 years, 7 months ago
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.
Advertisement
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.
It would help if you post your previous implementation so we can see what you're doing wrong.
"If I were a philosopher, I might argue that an array name's type is not equivalent to its equivalent. But I'm not a philosopher, so when I suggest something like that, I'm not a philosopher; I'm only being equivalent to a philosopher.""Saturn ascends, choose one or ten. hang on or be humbled again." - Tool
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;}
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
Holy crap, you can read!

This topic is closed to new replies.

Advertisement