Sign in to follow this  
Silarr

Array Issue

Recommended Posts

I'm having trouble figuring out how to manage my array of objects to "fire" independantly of one-another without all moving at the same time ontop of eachother :/.

The issue isn't setting up the array, nor getting (all) objects to move on keypress, but I'm trying to figure out how to manage the array so that when I press the key only 1 at a time moves (like bullets).


struct Laser
{

float xLaserPos;
float yLaserPos;
//bool Collide;
//bool visible;


};Laser *LaserArray=NULL;

bool FireLaser=false;
bool CannotFire=false;
NumLasers=50;


LaserArray = new Laser[NumLasers];

for (int i=0; i<NumLasers;i ++){

LaserArray[i].xLaserPos = 0;
LaserArray[i].yLaserPos = 0;
//LaserArray[i].visible = false;
//LaserArray[i].Collide = false;

}




// Render Logic //

if (FireLaser == true){

// -1 Everytime we fire is true from laser array value
int i;
i = NumLasers;
i-=1;

// Render Laser Sprite
Laser->Sprite(LaserArray[i].xPos,LaserArray[i].xLaserPos,LaserArray[i].yLaserPos);

// Move
LaserArray[i].xLaserPos +=10;

// OutOfLaser
if(i<=0){
CannotFire=true;
}

}



if((SPACE_PRESSED)&&(CannotFire=false)){

FireLaser=true;


}






i++ in the code wont display for some reason.



I know I'm rendering all the lasers at once, but I require some guidance on how to render 1 at a time each time the fire laser is true.

Share this post


Link to post
Share on other sites
Ok some pseudo code :





function fireLaser()
{
if(fireDelay<=0)
{
laser= new Laser(x,y)
laserArray.push(laser);
fireDelay = 1000;
}

}

function updateLasers(elapsedTime)
{
if(fireDelay>0)
{
fireDelay -= elapsedTime;
if(fireDelay<0)
fireDelay =0;
}

for(laser in laserArray)
laser.update(elapsedTime)

}

function checkInput()
{

if (keys['space'])
fireLaser();
}

function update()
{
elapsedTime = getElapsedTimer();
updateLasers(elapsedTime);
}





What's going on is when you fire a laser you set a timer and if that timer hasn't run out yet you can't fire new lasers. The above will cause a 1000 ms delay between lasers if elapsedTime is in milliseconds. Since lasers will be created with 1000ms intervals and they will be updated they will not be rendered at the same position.

Hope that helps.

Share this post


Link to post
Share on other sites

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