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

Author Topic: Unique names for rectangles in class  (Read 5483 times)

0 Members and 1 Guest are viewing this topic.

Evlampiy

  • Newbie
  • *
  • Posts: 10
    • View Profile
Unique names for rectangles in class
« on: August 08, 2021, 01:20:31 pm »
Hello! I'm new to SFML and I try to create a game 2048. So, I create a class, that have a function to create and draw rectangle with:
sf::RectangleShape rect(sf::Vector2f(130.f, 130.f));
                rect.move(20.f, 20.f);
                rect.setFillColor(sf::Color(255, 160, 122));
                window.draw(rect);
But if I need to create two or more objects of this class, it will not work correctly because it create and draw one rectangle with one name. So, can I create a class, that will give an unique name for all rectangles and how?
And sorry for my english.
« Last Edit: August 08, 2021, 02:52:21 pm by Evlampiy »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Unique names for rectangles in class
« Reply #1 on: August 08, 2021, 07:10:18 pm »
If you want an undefined number of rectangle, you'll need a container (and learn how to use it), like an std::vector from the C++ standard library

Evlampiy

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Unique names for rectangles in class
« Reply #2 on: August 08, 2021, 07:28:43 pm »
Thanks you! But if I need only 16, can I use another method?

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Unique names for rectangles in class
« Reply #3 on: August 08, 2021, 07:58:11 pm »
If you need a fixed number of rectangles you can use arrays, old school arrays or std::array from the standard library.

Evlampiy

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Unique names for rectangles in class
« Reply #4 on: August 08, 2021, 08:03:03 pm »
But what I will set like a type of array?

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Unique names for rectangles in class
« Reply #5 on: August 09, 2021, 07:42:17 am »
sf::RectangleShape rect[16];
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Evlampiy

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Unique names for rectangles in class
« Reply #6 on: August 13, 2021, 12:33:06 am »
Thanks you very much and sory for my stupidity.  ;D