0
我想知道爲什麼我的碰撞只在將我的精靈移動到屏幕原點時才起作用。這是我加載我的地圖的方式有問題,還是有一些關於精靈如何工作,我不完全明白的問題?下面是我對tilemap的SFML碰撞檢測只在原點工作?
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;
}