Hello!
Sorry for my lame question, but how to initialize bool array with any of loops? I was trying to initialize with for loop:
mouse class:
//there is declaration of my array, the rest is not improtant:
#pragma once
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <iostream>
using sf::Mouse;
using sf::Vector2i;
using sf::Vector2f;
using sf::RectangleShape;
using sf::Color;
using sf::FloatRect;
using std::cout;
using std::endl;
class mysz
{
public:
mysz(void);
Vector2i pozycjaMyszy;
Vector2i pobierzPozycjeMyszy();
Vector2f pozycjaFloat;
RectangleShape kwadratMysz;
FloatRect kolizjaKwadratMysz;
bool kolizja[9]; // this is it
void ustawParametryKwadratu();
void ustawPozycje();
void sprawdzKolizje(FloatRect porownywana, int krokPetli);
~mysz(void);
};
mouse cpp:
#include "mysz.h"
mysz::mysz(void)
{
for(int i = 0; i > 9; i++)
{
kolizja[i] = false; // here i try to initialize the array
}
}
Vector2i mysz::pobierzPozycjeMyszy()
{
pozycjaMyszy = Mouse::getPosition();
pozycjaFloat = static_cast<Vector2f>(pozycjaMyszy);
return pozycjaMyszy;
}
void mysz::ustawParametryKwadratu()
{
kwadratMysz.setSize(Vector2f (32,32));
kwadratMysz.setFillColor(Color::White);
kolizjaKwadratMysz = kwadratMysz.getGlobalBounds();
}
void mysz::sprawdzKolizje(FloatRect porownywana, int krokPetli)
{
if(kolizjaKwadratMysz.intersects(porownywana))
{
// wystapilyKolizje[krokPetli] = true;
}
}
mysz::~mysz(void)
{
}
but VS 2012 debugger show that all of array fields have: "true (204)"
So, can someone help with my problem?
for(int i = 0; i > 9; i++)
> should be < ;)
Don't write loops if there are STL algorithms for this task:
std::fill(kolizia, kolizia+9, false);
Or use std::array, which is a good idea anyway:
std::array<bool, 9> kolizja;
...
kolizja.fill(false);
Or initialize the array directly:
mysz::mysz(void)
: kolizja() // sets all elements to false
{
}
By the way, you should avoid using in header files. Why don't you explicitly write sf::Vector2i? That's the purpose of namespaces ;)