• Advertisement
Sign in to follow this  

Help with using vectors

This topic is 2090 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

HI all,

I am not really sure where I am going wrong with this one...

I am attempting to create an array of enemies which will appear on screen and move around randomly, independant of each other. I am attempting to use a vector to create multiple enemy objects and iterate through them, moving them in different directions etc... However, the screen only displays one enemy. I am unsure if this is because all 5 objects are following the same path or whether I am only actually creating one enemy object.

Enemy class is shown below (Just some basic movement so far)


class enemy
{

private:

float xPos;
float yPos;
int health;
bool right;
bool down;

public:

enemy()
{
srand(time(NULL));
xPos = rand() % 700 + 100;
yPos = rand() % 700 + 100;

int decider = rand() % 10 +1;
if(decider > 5)
{
right = true;
}

decider = rand() % 10 +1;
if(decider > 5)
{
down = true;
}

health = 1;
}

void move(int sw, int sh)
{
if(right)
{
xPos = xPos + 5.0f;
if(xPos > sw-32)
{
right = false;
}
}
else
{
xPos = xPos - 5.0f;
if(xPos < 0.0f)
{
right = true;
}
}

if(down)
{
yPos = yPos + 5.0f;
if(yPos > sh-32)
{
down = false;
}
}
else
{
yPos = yPos - 5.0f;
if(yPos < 0.0f)
{
down = true;
}
}
}

float xPosition()
{
return xPos;
}

float yPosition()
{
return yPos;
}

};



Below is the code I have in the main part of the game which is relevant to the creationn of the enemy objects. The comments in the code show how it is supposed to work in my head and may not nessecarily be correct!



D3DXVECTOR3 position, enemyPosition;
std::vector<enemy> enemies; // Creates the vector object

int numOfEnemies = 5;
for(int i=0; i<numOfEnemies; i++)
{
enemies.push_back(enemy()); // Creates 5 enemy objects
}

for (std::vector<enemy>::iterator IT = enemies.begin(); IT != enemies.end(); IT++)
{
(*IT).move(SCREEN_WIDTH, SCREEN_HEIGHT); // Moves each of the 5 enemy objects
}

d3dSprite->Begin(D3DXSPRITE_ALPHABLEND);

for(std::vector<enemy>::iterator IT = enemies.begin(); IT != enemies.end(); IT++)
{
D3DXVECTOR3 enemyPosition((*IT).xPosition(), (*IT).yPosition(), 0.0f); // Updates enemyPosition with the position of each object

d3dSprite->Draw(enemySprite, NULL, &center, &enemyPosition, D3DCOLOR_XRGB(255, 255, 255)); // Draws each object
}

d3dSprite->End();



Like I said, I do see one enemy object moving around on the screen. I just cannot seem to figure out whether I am only creating the 1 object or whether all 5 objects are following the same path. Could someone point out where I am going wrong?

Kind Regards

Share this post


Link to post
Share on other sites
Advertisement
You should only make one call to [font=courier new,courier,monospace]srand()[/font]. You can call it at the beginning of [font=courier new,courier,monospace]main()[/font] and never worry about calling it again. If you call it again, it will reset the pseudo-random library, which means that if [font=courier new,courier,monospace]time()[/font] returns the same value each time you create an [font=courier new,courier,monospace]enemy[/font] (which is probably happening, since it should take less than a second to create 5 enemies, so all 5 of them might get 19492 (or some other number) back from [font=courier new,courier,monospace]time()[/font]), they will each get the same value out of [font=courier new,courier,monospace]rand()[/font].

Call it once, at the beginning of [font=courier new,courier,monospace]main()[/font].


srand(0);
int x = rand();

srand(0);
int y = rand();

//C++ requires that x == y, since you seeded the pseudo-random library with the same seed just before each call
Edited by Cornstalks

Share this post


Link to post
Share on other sites
I cannot believe I missed that! ohmy.png

I am aware that you only need seed it once but overlooked the fact that the initialiser would obviously get called more than once...

Thank you for your fast and accurate response.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement