Allegro.cc Forums » Programming Questions » Creating An Enemy Array

 Dizzy Egg Member #10,824 March 2009 ```Sprite sprite[10]; //0 indexed, so 0-9, not 1-10!! Sprite[0].x = 10 //Sprite 1 Sprite[0].y = 20 Sprite[1].x = 10 //Sprite 2 Sprite[1].y = 10 ``` etc etc ----------------------------------------------------Please check out my songs:https://soundcloud.com/dont-rob-the-machina
 allegro programmer Member #15,653 June 2014 Wow that's easy enough.Where would I initiate that in the prototype as InitSprite(Sprite &sprite[10])?or in the code as ```void InitSprites(Sprite &sprite) Sprite sprite[10]; //0 indexed, so 0-9, not 1-10!! Sprite[0].x = 10 //Sprite 1 Sprite[0].y = 20 ect ect sprite.live=true; ``` i was also recommended a double linked list, still trying to figure that out atm.
 Edgar Reynaldo Member #8,592 May 2007 You don't want an array of vectors, just a vector of Sprites. If you don't know the STL (Standard Template Library) very well yet, take a look at this great reference :https://www.sgi.com/tech/stl/and specifically for std::vector :https://www.sgi.com/tech/stl/Vector.html```std::vector my_vector; Sprite s; s.bmp = al_load_bitmap("my_sprite.png"); s.x = 30; // ... my_vector.push_back(s); ``` To alter the contents of your vector you need to use a reference : ```Sprite& s1 = my_vector[0]; s1.bmp = al_load_bitmap("my_sprite.png"); // alternatively use the reference the vector accessor gives you : my_vector[0].bmp = al_load_bitmap("my_sprite.png"); ``` You can access a vector just like an array, using indices ie... my_vector[zero_based_index].Quote: How do I draw these on the screen using allegro and then be able to manipulate each one interdependently. RTFM? al_draw_bitmap(bmp , x , y , flags);See http://alleg.sourceforge.net/a5docs/5.0.10/andhttp://alleg.sourceforge.net/a5docs/refman/index.htmlfor the latest allegro 5 docs. Elizabeth Warren for President 2020! | Modern Allegro 4 and 5 binaries | Allegro 5 compile guideKing Piccolo will make it so
 Edgar Reynaldo Member #8,592 May 2007 vectors are easy. You only need to know a few things about them to use them.1. vector is a template class, which means you need to instantiate the type when you instantiate the vector object. ```vector sprite_vector; ``` 2. Access to elements is done using array style indices. ```sprite_vector[0] = s; sprite_vector[0].x = 10; //... ``` 3. Adding an element to the vector is done using push_back. ```sprite_vector.push_back(s); ``` 4. Removing an element can be done using erase and iterators : ```vector::iterator it = sprite_vector.begin(); if (it != sprite_vector.end()) { sprite_vector.erase(it); } ``` 5. Iterators can be incremented and the underlying element can be accessed through them. ```++it; Sprite s = *it; ``` That should be most of what you need.The other thing to keep in mind is that vectors perform copy operations during resize and anything else that changes the structure of the vector so you need to keep in mind the difference between shallow and deep copies of the objects that you add to your vector. Generally vectors store pointers, which can be copied without altering the underlying object they point to. Elizabeth Warren for President 2020! | Modern Allegro 4 and 5 binaries | Allegro 5 compile guideKing Piccolo will make it so