Sign in to follow this  

Enemy shooting delay

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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[i].y = (float)enemyArray[i].y + 30;
enbullet[i].x = (float)enemyArray[i].x + 30;}

and in the draw function...

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

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

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

Share this post


Link to post
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 holders
const 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

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this