2017-05-27 7 views
0

Ich habe mich gefragt, warum meine Kollision nur funktioniert, wenn ich mein Sprite zum Ursprung des Bildschirms verschiebe. Ist es ein Problem mit der Art, wie ich meine Karte geladen habe, oder gibt es etwas darüber, wie Sprites funktionieren, die ich nicht vollständig verstehe? Hier ist mein Code für die tilemapSFML-Kollisionserkennung funktioniert nur am Ursprung?

Tilemap.cpp

#include "Tilemap.h" 
#include "Player.h" 

#include <SFML/Graphics.hpp> 
#include <iostream> 
#include <fstream> 
#include <string> 

using namespace std; 
using namespace sf; 

//default constructor 
Tilemap::Tilemap() 
{ 
    if (!texture.loadFromFile("map/purpleBlock.png")) 
    { 
     cout << "Error opening file" << endl; 
    } 

    sprite.setTexture(texture); 
} 

//reads tilemap from a text file into an array 
void Tilemap::loadTile(RenderWindow &window, string filename) 
{ 
    string temp; 
    ifstream mapFile; 
    mapFile.open("map/test.map"); 

    if (!mapFile.is_open()) 
    { 
     cout << "Error opening " << filename << endl; 
    } 

    //getline(mapFile, tileSet); 

    //reading height, width, tile height, tile width and storing them into variables 
    getline(mapFile, temp); 
    tileWidth = stoi(temp, nullptr); 
    getline(mapFile, temp); 
    tileHeight = stoi(temp, nullptr); 

    getline(mapFile, temp); 
    width = stoi(temp, nullptr); 
    getline(mapFile, temp); 
    height = stoi(temp, nullptr); 

    data = new int[width*height]; 

    //reading values into array 
    for (int y = 0; y < height; ++y) 
    { 
     for (int x = 0; x < width; ++x) 
     { 
      char temp; 
      mapFile >> data[x + y * width] >> temp; 
     } 
    } 

    mapFile.close(); 
} 

//drawing the map onto the screen 
void Tilemap::drawMap(RenderWindow &window) 
{ 
    for (int y = 0; y < height; ++y) 
    { 
     for (int x = 0; x < width; ++x) 
     { 
      if (!data[x + y * width] == 0) 
      { 
       sprite.setPosition(x * 32, y * 32); 
       sprite.setTextureRect(IntRect(0, 0, tileWidth, tileHeight)); 
       window.draw(sprite); 
      } 
     } 
    } 

} 

//testing collision 
bool Tilemap::tileCollision(RenderWindow &window, RectangleShape &rect) 
{ 
    if (sprite.getGlobalBounds().intersects(rect.getGlobalBounds())) 
    { 
     cout << "Collision" << endl; 
    } 

    return true; 
} 

enter image description here

Antwort

0

Sie nur ein Sprite und Sie seine Position zu ziehen ändern. Okay, aber wenn Sie testen, ob es eine Kollision gibt, testen Sie die Kollision mit der letzten Position, die Sie auf Ihr Sprite gesetzt haben. Also sollten Sie die Kollision mit Ihrem Sprite nicht testen. Wenn Sie weiterhin auf Ihre Karte zugreifen können, verwenden Sie sie stattdessen.

EDIT:

ich sah Ihr Beitrag Using tiles in SFML and collision detection

Die sf :: Die Textur ist die Ressource, sf :: Sprite ist nur eine Klasse zu ziehen, so dass Sie viele Sprites mit wenigen Texturen erstellen können. (std::vector<std::vector<sf::Sprite>> map)

Verwandte Themen