Displaying multiple of the same sprite in SFML within a certain area o the

Right I have a small problem, I want to spawn 60 enemies which i have an array for, in memory and move them along the X axis like the zombies on the last stand game the way they run onto the screen, ive managed to draw one enemy sprite and he works fine coming onto the screen, however how do i spawn the arrays amount being 60 into memory and onto the screen at different times within the game time. the necessary code is below, feel free to ask if you want to know more i need the help, thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

//here is the array for the enemies

sf::sprite    arr_enemies[NO_ENEMIES]

static const unsigned int   NO_ENEMIES=60U


//declared enemy sprite loaded its texture into memory
this->_sprite_res.load(4,"media/Textures/enemy.png");

//	//load spritesheet
	for (int i = 0; i < NO_ENEMIES; i++)
	{
		this->_arr_sprites[i].setTexture(this->_sprite_res.get(4));
		this->_arr_sprites[i].setPosition(-10,640);
	}

//update loop this is how enemy sprite moves
for (int i = 0; i < NO_ENEMIES; i++)
	{
	sf::Vector2f ttrans(s_dir * 100.0f * ptpf.asSeconds(),s_jump);
		this->_arr_sprites[i].move(ttrans);
	}

//render loop contains this code
 for (int i = 0; i < LJMUGame::NO_ENEMIES; i++)
	{
		this->_wndw.draw(this->_arr_sprites[i]);
	}


id appreciate any help right now
You only need one sf::Sprite for all your enemies - you can reposition it and draw it multiple times to draw all your enemies. Remember, once something gets drawn, you can only draw over it - there is no 'move' or 'undraw'.
i do only have 1 sf::sprite for my enemy dont i? how do i go about drawing it multiple times and reposition them id like them to travel down 3 lanes? thats the issue i dont know how to do it at all is there a formula you know by any chance?
@LB, wouldn't that complicate things? He would then need to store the position of each "sprite", and iterate through all the positions setting them to the sf::Sprite to move them. And AFAIK sf::Sprite is fairly lightweight so 60 shouldn't be much of an issue.

But either way I don't think this answers DriftKing13's question which is how to spawn the enemies at different times. Unfortunately I'm not a game developer so I am unable to answer this question myself.
i just need a formula or how to alter this code in order to spawn multiple enemies off memory and move them onto the screen so i can torch there asses haha im making a game like the last stand i just want the enemies like that but with my sprite
Do your enemies have their own state classes? Stuff like health, status effects? If not, you should make them now. You could easily put a boolean flag in one of those that says whether your enemy is alive or not (which determines if the enemy gets rendered/updated or not), and if you don't care much about client-server separation, you could even put your sf::Sprites in them too.

-Albatross
no its just a sprite that (in the future of my project) gets cleared upon collision with my bullets that work currently
Last edited on
You can still have an array of bools or state objects for your enemies to signify whether they're alive or not.

-Albatross
i know i can i just dont know how to, my teacher isnt the best at teaching us so i have had to learn for myself, sorry if i sound dumb but :/ i know how to do bools i done one in my code for when a key is pressed
BUMP!
Topic archived. No new replies allowed.