Witam, Podczas tworzenia mojej gry natrafiłem na dziwny problem.
#include <SFML/Graphics.hpp>
#include <iostream>
#include <windows.h>
using namespace sf;
bool MoveUp = true;
bool MoveDown = true;
bool MoveRight = true;
bool MoveLeft = true;
bool MoveTopRight = true;
bool MoveTopLeft = true;
bool MoveDownRight = true;
bool MoveDownLeft = true;
float MoveSpeed = 0.5f;
bool FaceUp = false;
bool FaceDown = false;
bool FaceRight = false;
bool FaceLeft = false;
bool FaceTopRight = false;
bool FaceTopLeft = false;
bool FaceDownRight = false;
bool FaceDownLeft = false;
Vector2f velocity(sf::Vector2f(0, 0));
RectangleShape B1;
RectangleShape B2;
RectangleShape B3;
RectangleShape B4;
RectangleShape B5;
RectangleShape B6;
RectangleShape B7;
RectangleShape B8;
RectangleShape B9;
RectangleShape B10;
struct nazwy {
std::vector<sf::RectangleShape*> b;
};
nazwy sciany{
{ &B1, &B2, &B3, &B4, &B5, &B6, &B7, &B8, &B9, &B10 }
};
Sprite Postac;
int main()
{
srand(time(NULL));
Texture PostacStoi;
PostacStoi.loadFromFile("_PNG/1_KNIGHT/Knight_01__IDLE_001_POP.png");
float PosXB1 = rand() % 333 + 333;
float PosYB1 = rand() % 650;
float WidB1 = rand() % 100 + 200;
float HeiB1 = rand() % 40 + 30;
float PosXB2 = rand() % 333 + 333;
float PosYB2 = rand() % 650;
float WidB2 = rand() % 100 + 200;
float HeiB2 = rand() % 40 + 30;
float PosXB3 = rand() % 333 + 666;
float PosYB3 = rand() % 650;
float WidB3 = rand() % 100 + 200;
float HeiB3 = rand() % 40 + 30;
float PosXB4 = rand() % 1000;
float PosYB4 = rand() % 650;
float WidB4 = rand() % 100 + 200;
float HeiB4 = rand() % 70 + 30;
float PosXB5 = rand() % 1000;
float PosYB5 = rand() % 650;
float WidB5 = rand() % 100 + 200;
float HeiB5 = rand() % 40 + 30;
float PosXB6 = rand() % 850;
float PosYB6 = rand() % 700;
float WidB6 = rand() % 40 + 30;
float HeiB6 = rand() % 100 + 200;
float PosXB7 = rand() % 850;
float PosYB7 = rand() % 700;
float WidB7 = rand() % 40 + 30;
float HeiB7 = rand() % 100 + 200;
float PosXB8 = rand() % 850;
float PosYB8 = rand() % 700;
float WidB8 = rand() % 40 + 30;
float HeiB8 = rand() % 100 + 200;
float PosXB9 = rand() % 850;
float PosYB9 = rand() % 700;
float WidB9 = rand() % 70 + 30;
float HeiB9 = rand() % 100 + 200;
float PosXB10 = rand() % 233 + 466;
float PosYB10 = rand() % 700;
float WidB10 = rand() % 40 + 30;
float HeiB10 = rand() % 100 + 200;
B1.setSize(Vector2f(WidB1, HeiB1));
B2.setSize(Vector2f(WidB2, HeiB2));
B3.setSize(Vector2f(WidB3, HeiB3));
B4.setSize(Vector2f(WidB4, HeiB4));
B5.setSize(Vector2f(WidB5, HeiB5));
B6.setSize(Vector2f(WidB6, HeiB6));
B7.setSize(Vector2f(WidB7, HeiB7));
B8.setSize(Vector2f(WidB8, HeiB8));
B9.setSize(Vector2f(WidB9, HeiB9));
B10.setSize(Vector2f(WidB10, HeiB10));
Postac.setTexture(PostacStoi);
Postac.setScale(0.2f, 0.2f);
Postac.setPosition(10, 470);
B10.setPosition(PosXB10, PosYB10);
B9.setPosition(PosXB9, PosYB9);
B8.setPosition(PosXB8, PosYB8);
B7.setPosition(PosXB7, PosYB7);
B6.setPosition(PosXB6, PosYB6);
B5.setPosition(PosXB5, PosYB5);
B4.setPosition(PosXB4, PosYB4);
B3.setPosition(PosXB3, PosYB3);
B2.setPosition(PosXB2, PosYB2);
B1.setPosition(PosXB1, PosYB1);
RenderWindow window(sf::VideoMode(1000, 700), "SFML works!");
window.setFramerateLimit(60);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
// Close window: exit
if (event.type == sf::Event::Closed)
window.close();
}
for (int x = 0; x <=0; x++) {
//PORUSZANIE SIĘ BEZ STYKANIA Z ELEMENTEM
if (Keyboard::isKeyPressed(Keyboard::W) && !Postac.getGlobalBounds().intersects(sciany.b[x]->getGlobalBounds())) {
FaceUp = true;
FaceDown = false;
FaceRight = false;
FaceLeft = false;
velocity.x = 0;
velocity.y = -MoveSpeed;
Postac.move(velocity.x, velocity.y);
}
if (Keyboard::isKeyPressed(Keyboard::S) && !Postac.getGlobalBounds().intersects(sciany.b[x]->getGlobalBounds())) {
FaceUp = false;
FaceDown = true;
FaceRight = false;
FaceLeft = false;
velocity.x = 0;
velocity.y = MoveSpeed;
Postac.move(velocity.x, velocity.y);
}
if (Keyboard::isKeyPressed(Keyboard::A) && !Postac.getGlobalBounds().intersects(sciany.b[x]->getGlobalBounds())) {
FaceUp = false;
FaceDown = false;
FaceRight = false;
FaceLeft = true;
velocity.x = -MoveSpeed;
velocity.y = 0;
Postac.move(velocity.x, velocity.y);
}
if (Keyboard::isKeyPressed(Keyboard::D) && !Postac.getGlobalBounds().intersects(sciany.b[x]->getGlobalBounds())) {
FaceUp = false;
FaceDown = false;
FaceRight = true;
FaceLeft = false;
velocity.x = MoveSpeed;
velocity.y = 0;
Postac.move(velocity.x, velocity.y);
}
//=======================================================================================================================================================================================
//ZATRZYMANIE PRZY DOTKNIĘCIU ELEMENTU
if (Postac.getGlobalBounds().intersects(sciany.b[x]->getGlobalBounds()) && FaceDown && Keyboard::isKeyPressed(Keyboard::S)) {
velocity.x = 0;
velocity.y = 0;
}
if (Postac.getGlobalBounds().intersects(sciany.b[x]->getGlobalBounds()) && FaceLeft && Keyboard::isKeyPressed(Keyboard::A)) {
velocity.x = 0;
velocity.y = 0;
}
if (Postac.getGlobalBounds().intersects(sciany.b[x]->getGlobalBounds()) && FaceRight && Keyboard::isKeyPressed(Keyboard::D)) {
velocity.x = 0;
velocity.y = 0;
}
if (Postac.getGlobalBounds().intersects(sciany.b[x]->getGlobalBounds()) && FaceUp && Keyboard::isKeyPressed(Keyboard::W)) {
velocity.x = 0;
velocity.y = 0;
}
if (Keyboard::isKeyPressed(Keyboard::W) && Postac.getGlobalBounds().intersects(sciany.b[x]->getGlobalBounds()) && !FaceUp) {
velocity.x = 0;
velocity.y = -MoveSpeed;
Postac.move(velocity.x, velocity.y);
}
if (Keyboard::isKeyPressed(Keyboard::S) && Postac.getGlobalBounds().intersects(sciany.b[x]->getGlobalBounds()) && !FaceDown) {
velocity.x = 0;
velocity.y = MoveSpeed;
Postac.move(velocity.x, velocity.y);
}
if (Keyboard::isKeyPressed(Keyboard::A) && Postac.getGlobalBounds().intersects(sciany.b[x]->getGlobalBounds()) && !FaceLeft) {
velocity.x = -MoveSpeed;
velocity.y = 0;
Postac.move(velocity.x, velocity.y);
}
if (Keyboard::isKeyPressed(Keyboard::D) && Postac.getGlobalBounds().intersects(sciany.b[x]->getGlobalBounds()) && !FaceRight) {
velocity.x = MoveSpeed;
velocity.y = 0;
Postac.move(velocity.x, velocity.y);
}
}
window.clear(Color(0, 0, 0));
window.draw(Postac);
window.draw(B1);
window.draw(B2);
window.draw(B3);
window.draw(B4);
window.draw(B5);
window.draw(B6);
window.draw(B7);
window.draw(B8);
window.draw(B9);
window.draw(B10);
window.display();
}
return 0;
}
Gdy dam x = 0 (pętla wykonuje się 1 raz) to gdy postać dotknie bloku B1 to normalnie się zatrzyma i dalej nie przejdzie. Natomiast gdy dam więcej to przy dotknięciu jakiegokolwiek bloku postać jedynie spowolni. Co dałoby się z tym zrobić? Spędziłem nad tym z 3 godziny i nic nie wymyśliłem. Bardzo proszę o pomoc.