Hey so I was working on my pong game and found it quite tedious to design a new sprite ball everytime i wanted to spawn one ase i ended up being very repetative:
sf::Image img;
img.Create(30,50,sf::Color(255,0,0));
mySprite box(img, sf::Vector2f(0,300), sf::Vector2f(1,1), 0, 36, 300); //TODO: make collision work at extremely high speeds
box.SetCenter(15,25);
sf::Image img2;
img2.Create(56.5,80,sf::Color(255,0,0));
mySprite box2(img2, sf::Vector2f(800,300), sf::Vector2f(1,1), 0, 239, 300);
box2.SetCenter(25,40);
sf::Image img3;
img3.Create(56.5,80,sf::Color(255,0,10));
mySprite box3(img3, sf::Vector2f(500,300), sf::Vector2f(1,1), 0, 135, 400);
box3.SetCenter(25,40);
sf::Image img4;
img4.Create(56.5,80,sf::Color(255,166,90));
mySprite box4(img4, sf::Vector2f(300,500), sf::Vector2f(1,1), 0, 10, 100);
box4.SetCenter(25,40);
Creating a Spawn() function doesn’t work because i have to create a new name for the sprite instantiation every new sprite(thus the box,box1,box2, etc.). How can i make a function that somehow randomly formulates a name for another ball i want to spawn?
You can’t randomly generate variable names. How would you reference them in your code?
Why don’t you just use a
vectorto store your images and boxes, and then use a loop to spawn sprites:This assumes that you have a data-structure
Parametersthat can hold all the parameters you require (with better names than the ones in my example). You then loop over all the parameters-sets you have, and create sprite for each one.