Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: is there a way for automatically initializing infinite sprites or shapes?  (Read 3339 times)

0 Members and 1 Guest are viewing this topic.

K.F

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
    • Email
Hi.

 Is there a way to let the user make any number of shapes or sprites without me initializing them first?

 For example, I am making 2D billiard game as exercise using SFML 2.0 and want the player to create the balls by clicking on the place where he wants the ball to be, I want a way for a ball sprite to be created and named "ball_01,ball_02" every time he clicks without me making them one by one.

 Other related question is how do I enter these sprites in a loop or if statement?

 For example, I want a loop or function for these sprites to check for collision with every other sprite without writing different if statements for each sprite?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
This is all possible with C++, but C++ isn't a language for which you can write your wishes in English and the compiler will turn it into your game. ;)
You need to learn how to use C++ first. This also involves thinking diffrently form a point of view of a programmer perspective.

And since may insiste on a solution, I give a bit text, if you don't understand it, you just realized you need to read more on C++.
You can use a STL vector to hold your sprites. Then whenever you click somewhere with mouse you can retrieve the coordinates and use them to place a new sprite which you then push onto the vector. For every frame you just iterate through the vector and draw each sprite. But make sure that the sprites and textures don't go out of scope, otherwise you would run into some problems.

Have fun learning more about C++! :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

K.F

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
    • Email
I knew vectors could be used in this way for variables but the fact they could hold sprites as well is new to me. :P

Will try this soon, thank you very much.