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

Author Topic: Need help with RectangleShape and array (Solved)  (Read 1939 times)

0 Members and 1 Guest are viewing this topic.

Tall

  • Newbie
  • *
  • Posts: 8
    • View Profile
Need help with RectangleShape and array (Solved)
« on: January 03, 2018, 11:32:10 am »
So, im creating a little game as a school project, a very simple one, im trying to create projectiles but when i press 'F' to fire the game crash.

here the part on the code
    int fire_c = 0;
    std::vector <sf::RectangleShape> fire (fire_c);
    std::vector <sf::Vector2f> fire_p (fire_c);
 

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::F)) {
            fire_c = fire_c + 1;
            fire.resize(fire_c);
            fire_p.resize(fire_c);
            fire[fire_c].setFillColor(sf::Color::Red);
            fire[fire_c].setSize(sf::Vector2f(2,5));
            fire_p[fire_c].x = astronave_p.x + 4;
            fire_p[fire_c].y = astronave_p.y - 6;
            pioggia[fire_c].setPosition(pioggia_p[fire_c]);
        }
        for (int i = 1; i < fire_c; i++) {
            fire_p[i].y = fire_p[i].y - 5;
            fire[i].setPosition (fire_p[i]);
        }

for (int i = 1; i < fire_c; i++) {window.draw(fire[i]);}
 
« Last Edit: January 03, 2018, 09:08:34 pm by Tall »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Need help with RectangleShape and array
« Reply #1 on: January 03, 2018, 11:55:08 am »
In C++, array indices start from 0, not 1.

I strongly suggest you to invest more time in learning the basics of C++ ; your code could be much simpler and more readable with appropriate data structures and use of std::vector.

I also suggest you to learn the basics of debugging; this crash is a simple one and any debugger on the market will tell you that you are making out of bounds access into a std::vector, with the exact line of code where it happens.
« Last Edit: January 03, 2018, 11:59:26 am by Laurent »
Laurent Gomila - SFML developer

Tall

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Need help with RectangleShape and array
« Reply #2 on: January 03, 2018, 01:52:41 pm »
thanks for your support, but code::blocks didint gave me any error.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Need help with RectangleShape and array
« Reply #3 on: January 03, 2018, 02:21:03 pm »
Quote
code::blocks didint gave me any error
If you compile with debugging flags (ie. in Debug configuration), and run your app through the debugger (F5?) then you must get something.
Laurent Gomila - SFML developer

Tall

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Need help with RectangleShape and array
« Reply #4 on: January 03, 2018, 09:07:56 pm »
thank for you help, now the program works perfectly. Tomorrow im gonna revise vertex and array.

 

anything