Jump to content
  • Advertisement
Sign in to follow this  

Trouble with vectors

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

I'm having a few problems using vectors. Background:- I've created a wee tiling routine. It draws a surface filled with tiles according to the integers in an ordinary array. So I have background array and level array. (The background array just contains a portion of the level array which is updated as the game scrolls). Now I need to figure out how to include objects in the level (tree's etc) The easiest way I can think of is too create new objects for everything at the start of each level. Now heres my problem. The number of objects will vary, so I need a dynamic array. I have a sprite class so I tred to do vector<sprite> *theTrees. But I can't seem to figure out how to add my sprites to it. I achieved what I wanted with static arrays by doing the following:-
	sprite *theTrees[10];
	for(int i=0; i<9; i++){
             theTrees= new sprite;
             theTrees->initSprite("data/images/items/tree.bmp", screen, 400+(i*64), 200, 64, 80);
    }


Can anyone help me figure out how to do this with a dynamic array.

Share this post


Link to post
Share on other sites
Advertisement
You want


vector<sprite *> the_trees;


You're declaring a pointer to a vector of sprites, not a vector of sprite pointers.

You may want to just use a vector of sprites, since that's where they'll be living, and they have a default constructor:


//Define number of sprites. Doesn't have to be a const, but may as well be.
const size_t num_sprites = 10;

//Declare a vector of sprites.
vector<sprite> the_trees(num_sprites);

//Initialize the sprites.
int i = 0;
for(vector<sprite>::iterator current_sprite = the_trees.begin();
current_sprite != the_trees.end();
current_sprite++, i++)
{
current_sprite->initSprite("data/images/items/tree.bmp", screen, 400+(i*64), 200, 64, 80);
}


You will have to change sprites[n]-> to sprites[n]. everywhere, but at least you don't have to worry about deleting the sprites. That'll happen when the_trees goes out of scope.

[Edited by - Couvillion on March 16, 2005 1:36:35 PM]

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!