我正在尝试在SFML中创建一个关卡编辑器。我尝试使用2D对象数组使整个屏幕充满它们,一个挨着另一个。问题在于它只会创建一条垂直线。
我尝试更改循环的方式,使某些变量变为全局变量。没什么我能想到的帮助。
LevelEditor.h:
#pragma once
#include "../StateBase.h"
#include "../Gameloop.h"
#include "Air.h"
namespace Engine
{
class LevelEditor : public StateBase
{
sf::RectangleShape rect;
Block *block = new Air[20, 15];
gameDataPtr prData;
public:
LevelEditor(gameDataPtr data);
virtual void run();
virtual void update(float dt);
virtual void draw(sf::RenderWindow &window);
sf::Vector2f checkWhere();
};
}
LevelEditor.cpp:
#include "LevelEditor.h"
#include "../engineData.h"
namespace Engine
{
LevelEditor::LevelEditor(gameDataPtr data) : prData(data) {}
void LevelEditor::run()
{
this->rect.setFillColor(sf::Color(255, 255, 255, 100));
this->rect.setSize(sf::Vector2f(50, 50));
float x=0;
int i = 0;
while (i < 20)
{
float y = 0;
int j = 0;
if (i != 0)
x += 50;
while (j < 15)
{
this->block[i, j].init();
this->block[i, j].setPos(sf::Vector2f(x, y));
y += 50;
j++;
}
i++;
}
}
void LevelEditor::update(float dt)
{
sf::Event event;
while (this->prData->window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
this->prData->window.close();
}
this->rect.setPosition(this->checkWhere());
}
void LevelEditor::draw(sf::RenderWindow &window)
{
int i = 0;
window.draw(this->rect);
while (i < 20)
{
int j = 0;
while (j < 15)
{
this->block[i, j].draw(window);
j++;
}
i++;
}
}
sf::Vector2f LevelEditor::checkWhere()
{
sf::Vector2f tempVec(0, 0);
while (sf::Mouse::getPosition(this->prData->window).x - tempVec.x >= 50)
tempVec.x += 50;
while (sf::Mouse::getPosition(this->prData->window).y - tempVec.y >= 50)
tempVec.y += 50;
return tempVec;
}
}
Block.h:
#pragma once
#include "../Assetmanager.h"
namespace Engine
{
class Block
{
protected:
bool isDestroyable;
sf::Sprite sprite;
sf::RectangleShape rect;
Assetmanager assets;
public:
virtual void init() = 0;
virtual void update(float dt) = 0;
virtual void draw(sf::RenderWindow &window) = 0;
void setPos(sf::Vector2f pos) { this->rect.setPosition(pos); }
sf::Sprite returnSprite() { return this->sprite; }
};
}
Air.h:
#pragma once
#include "Block.h"
#include "../engineData.h"
namespace Engine
{
class Air : public Block
{
public:
virtual void init() { rect.setFillColor(sf::Color::Green); rect.setSize(sf::Vector2f(50, 50)); }
virtual void update(float dt){}
virtual void draw(sf::RenderWindow &window) { window.draw(this->rect); }
};
}
这就是我得到的: Just a vertical line
但是我希望屏幕完全是绿色的。