I am making a game in sfml and at the moment when all of the enemies die. They are set to respawn however when this is happening they are respawning with one extra sprite than before.
The code for loading in the sprites is
unsigned int orcNumber = 5;
for (int i = 0; i < orcNumber; i++)
{
SpriteVector.push_back(ogreSprite);
SpriteVector[i].setPosition(spawnPointX[i], spawnPointY[i]);
}
The code for removing the enemies if they are offscreen or shot is similar to below using erase.
for (unsigned j = 0; j < SpriteVector.size(); j++)
{
if (this->SpriteVector[j].getPosition().x < 0 - 80 )
{
//this succesfully removes the object from the vector
SpriteVector.erase(SpriteVector.begin() + j);
std::cout << "Container size..." << SpriteVector.size() << "\n";
}
}
The statement for redrawing them is:
unsigned int orcNumberRespawn = 5;
if (SpriteVector.size() <= 1)
{
for (int i = 0; i < orcNumberRespawn; i++)
{
SpriteVector.push_back(ogreSprite);
SpriteVector[i].setPosition(spawnPointX[i], spawnPointY[i]);
}
}
window.draw(SpriteVector[i]);
Can anyone identify why when the sprites need to be redrawn it draws with + 1 sprite everytime?
The issue was with a loop outside of these vector loops.
Related
I'm having some difficulty applying a texture to a sprite array. I'm trying to apply the same texture to all, so that I can later setTextRect to decide which part of the texture is used as a tile in my game.
The declaration of the sprite array is in a different class and declared as follows:
sf::Sprite tileSprites[30][40]
Going line by line and debugging the stumbling block is the for loop. The response from the window is just to close and crash out with no errors.
The game crashes once the line where I try to apply the texture.
tileSprites[idx][idy].setTexture(tileMap);
std::cout << "Creating Map... \n";
// load the image to be used as map/the spritesheet.
if (!tileMap.loadFromFile("Data/Maps/tilemap.png"))
{
std::cout << "Tilemap PNG did not load";
}
//load the generated tilemap
if(!map.load("Data/Maps/test_map.tmx"))
{
std::cout << "TMX map file failed to load";
}
// access the layers in the map
const auto& layers = map.getLayers();
const auto layer = layers[0]->getLayerAs<tmx::TileLayer>();
const auto tiles = layer.getTiles();
int idx = 0;
int idy = 0;
for (int j = 0; j < tiles.size(); ++j)
{
idx = j / 30;
idy = j % 30;
tileSprites[idx][idy].setTexture(tileMap); // <-
}
std::cout << tiles.size();
}
Any advice would be really appreciated.
I have been trying to make asteroids in the console, I know that the console isn't an ideal way to do this, but I wanted to challenge myself.
The problem I am having is with printing to the screen. At first, my screen was flickering because I wasn't updating certain screen parts and instead redrawing every frame by appending the whole array to a string and outputting that string.
So I changed my code to do that, but my current problem now is that the fps of my game is really low because I can't batch call it to one cout function.
To print my code to the screen I have a 2d array of characters with width and height as its size. Then I copy the array, change the output, and compare it to the previous output to see if the pixel needs to be changed, then change it.
Here is my draw function
void draw() {
char prevOutput[ArrayBorder][ArrayBorder];
copyOutput(outputBuffer, prevOutput);
innitOutput();
plotPolygons();
for (int y = 0; y < GameBorder; y++) {
for (int x = 0; x < GameBorder; x++) {
if (outputBuffer[x][y] == prevOutput[x][y]) {continue;}
setCursorPosition(x, y);
cout << outputBuffer[x][y];
}
}
cout.flush();
}
Here is my previous draw function
void draw() {
innitOutput();
plotPolygons();
system("CLS");
string output = "";
for (int y = 0; y < GameBorder; y++) {
for (int x = 0; x < GameBorder; x++) {
output += outputBuffer[x][y];
}
output += "\n";
}
cout << output;
}
I'm trying game development. I tried a sample from a tutorial site like this:
int number = 10;
std::vector<std::unique_ptr<enemy> > enemies1;
for (unsigned i = 0; i < number; ++i) {
enemies1.emplace_back(std::unique_ptr<enemy>(new enemy(sf::Vector2f(100 * i + 1500, 1000), sf::Vector2f(15, 10), sf::Color(255, 255, 255, 0))));
}
//update
for (unsigned i = 0; i < number; ++i) {
enemies1[i]->update();
}
//render
for (unsigned i = 0; i < number; ++i) {
window.draw(enemies1[i]->enemysprite);
}
I'm using vector for creating multi enemies from my enemy class. Everything is ok I can draw 10 enemies on screen and they are moving and such. But if I try to delete an enemy like this:
if (!enemies1.empty())
{
for (unsigned i = 0; i < number; ++i) {
if (enemies1[i]->rect.getGlobalBounds().intersects(sprite.getGlobalBounds()))
enemies1.erase(enemies1.begin() + 1);
}
}
when the program starts a collision occurs everything is stopping. My question is what's the best way to create multiple enemies because I need to spawn and delete them in my game and I'm still learning.
I don't know whether my implementation correct or not. I wonder what should I do? In this code, I can't erase enemies from this vector.
I appreciate every piece of advice.
This loop:
for (unsigned i = 0; i < number; ++i) {
if (enemies1[i]->rect.getGlobalBounds().intersects(sprite.getGlobalBounds()))
enemies1.erase(enemies1.begin() + 1);
}
is flawed, because when you call erase, all the subsequent array entries move down 1, so next time round the loop you access the wrong element. Also, you run off the end of the array.
Instead, you can use the erase / remove idiom:
enemies.erase (std::remove_if (enemies.begin(), enemies.end(),
[sprite] (const std::unique_ptr <enemy> &e)
{ return e->rect.getGlobalBounds().intersects(sprite.getGlobalBounds()); }, enemies.end());
My enemy class is this like on shared picture
I'm currently writing a tetris in C++ and I am at the last stage, I need to delete a row when it is full. Once a piece falls it is stored in a boolean array grid[20][10]. For example I check which row is full (or true), if it is I call a method deleteRow, where n is a number of row:
void Grid::deleteRow(int n)
{
for (j = 0; j < WIDTH; j++)
{
grid[n][j] = false;
}
}
Once the row is deleted I call a method moveRowDown:
void Grid::moveRowDown()
{
for (i = 0; i < HEIGHT; i++)
{
for (j = 0; j < WIDTH; j++)
{
grid[i+1][j]=grid[i][j];
}
}
}
So this method does not work, and all of the pieces disappear. I know I am missing the logic. Thanks for the help in advance!
They disappear because you copy 1st empty row to 2nd, then to 3rd and etc.
You need to rewrite your first loop in Grid::moveRowDown() to work from the bottom of a glass:
for (i = HEIGHT-2; i>=0; i--)
I'm making a Asteroids game but I can not get to play more than one explosion at a time. Just get to do one at a time ...
This is my code I call in the main loop:
for(i = 0; i < MAX_SHOTS; i++) {
for(j = 0; j < MAX_ASTEROIDS; j++) {
if(shot[i].CheckCollision(asteroide[j])) {
shot[i].SetPos(-100, 0);
explosionSnd.Play();
numAst = j;
explosion[numExp++].Enable(true);
if(numExp == MAX_EXPLOSIONS-1) {
numExp = 1;
}
}
}
}
for(i = 1; i < MAX_EXPLOSIONS; i++) {
if(explosion[i].Enable()) {
explosion[i].SetPos(asteroide[numAst].GetX(), asteroide[numAst].GetY());
explosion[i].Draw();
if(explosion[i].GetFrame() == 5) {
explosion[i].Enable(false);
}
}
}
If I shot to an asteroid and after I shot to another, the animation is cut and goes to the new asteroid.
Any help?
Thank you.
Inside your second loop, you're moving each explosion to the location of the asteroid asteroide[numAst] - you're playing all the explosions, just all at the same place!
You should only position the explosion once after you Enable(true) it, when it's created in the first loop, not each time you draw it.
Hope that helps.