你好,只是一个简单的问题。想知道与我设置的 map 图 block 实现碰撞的最佳方法是什么。我打算为播放器设置一个矩形并检查它是否与实心矩形相交。我唯一的问题是,出于某种原因,如果我在头文件中声明我的矩形,它会给我一个错误。这意味着我无法检查我的播放器是否与我的 map 图 block 相交,因为我不能在播放器类中使用我的 map 类中的矩形,反之亦然。任何帮助将不胜感激。
这是我的 map 类:
#include "Map.h"
#include "Block.h"
#include <sstream>
using namespace std;
Map::Map()
{
//map ctor;
}
Map::~Map()
{
// map dtor
}
sf::Shape rect = sf::Shape::Rectangle(0, 0, BLOCKSIZE, BLOCKSIZE, sf::Color(255, 255, 255, 255));
void Map::Initialise(const char *filename)
{
MapImage.LoadFromFile("Images/block.png");
std::ifstream openfile(filename);
std::vector <int> tempvector;
std::string line;
while(std::getline(openfile, line))
{
for(int i =0; i < line.length(); i++)
{
if(line[i] != ' ') // if the value is not a space
{
char value = line[i];
tempvector.push_back(value - '0');
}
}
mapVector.push_back(tempvector); // push back the value of the temp vector into the map vector
tempvector.clear(); // clear the temp vector readt for the next value
}
}
void Map::DrawMap(sf::RenderWindow &Window)
{
sf::Color rectCol;
sf::Sprite sprite;
for(i = 0; i < mapVector.size(); i++)
{
for(j = 0; j < mapVector[i].size(); j++)
{
if(mapVector[i][j] == 0)
rectCol = sf::Color(44, 117, 255);
else if(mapVector[i][j] == 1)
rectCol = sf::Color(255, 100, 17);
rect.SetPosition(j * BLOCKSIZE, i * BLOCKSIZE);
rect.SetColor(rectCol);
Window.Draw(rect);
}
}
}
最佳答案
您可以创建另一个类来处理 map 和玩家类。 对于碰撞,我建议您使用 AABB(轴对齐边界框)并验证每一帧碰撞。
关于c++ - SFML 平台游戏与 map 图 block 发生碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15920062/