即使没有错误,我的 Sprites 也没有绘制?

My Sprites are not drawing even though there is no error?

这是我的 code.I 只是不知道为什么我的精灵不绘制。我在二维精灵数组的嵌套 for 循环中使用了 window.draw() 函数,但它没有绘制 it.Just 看看我的名为 Board 的二维精灵数组的嵌套循环。帮助将不胜感激。

#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include <stdlib.h>
#include<time.h>
#include<cstdlib>
using namespace std;

using namespace sf;
int main()
{
    
    Music music;
    music.openFromFile("mainmusic.wav");
    music.play();

    
    
    


    

    bool drawRed = true;


    bool deletePlay = false;

    //Creating window for game
    sf::RenderWindow window(sf::VideoMode(1280, 720), "SFML", sf::Style::Close | Style::Titlebar);

    //creating rect of exact sem size as window to use it for a bg
    sf::RectangleShape player(sf::Vector2f(1280.0f, 720.0f));

    //creating a texture for starting background
    sf::Texture bg_texture;
    bg_texture.loadFromFile("candy_crushbg.jpg");
    player.setTexture(&bg_texture);

    //creating texture for play button
    Texture play;
    play.loadFromFile("cstartbtn.png");

    //creating sprite for play button texture 
    Sprite start;
    start.setTexture(play);
    start.setPosition(Vector2f(530, 490));
    start.setScale(Vector2f(0.2f, 0.2f));
    start.setOrigin(Vector2f(265, 245));
    
    //creating texture for playing background
    Texture startbg;
    startbg.loadFromFile("start_bg.jpg");
    


    int k, lower = 1, upper = 5, x = 400, y = 100;
    Texture RCandy, BCandy, OCandy, GCandy, YCandy;
    RCandy.loadFromFile("red_candy.png");
    BCandy.loadFromFile("blue_candy.png");
    OCandy.loadFromFile("orange_candy.png");
    GCandy.loadFromFile("green_candy.png");
    YCandy.loadFromFile("yellow_candy.png");
    Sprite Board[8][8];
    srand(time(0));
    int i, j, random;

    //loop for opening window
    while(window.isOpen()) {
        
        //creating event for game functions
        sf::Event evnt;
        Event play;


        //calling event in a loop
        while (window.pollEvent(evnt)) {

            switch (evnt.type) {
                //event for when we click close button
            case sf::Event::Closed:
                window.close();
                break;
                //event when mouse is clicked on PLAY button
            case Event::MouseButtonPressed:
                //getting global boundaries of play button
                sf::Vector2f Mousepos = window.mapPixelToCoords(sf::Mouse::getPosition(window));
                switch(evnt.key.code) {
                case Mouse :: Left:
                    //checking if mouse click is in the boundary of play button
                    if (start.getGlobalBounds().contains(Mousepos)) {
                        cout << "Sprite Clicked\n";
                        //deleting play button
                        deletePlay = true;
                        window.clear();
                        player.setTexture(&startbg);
                        `
                        for (i = 0; i < 8; i++) {
                            x = 400;
                            for (j = 0; j < 8; j++) {
                                random = rand() % ((upper - lower) + lower) + 1;
                                cout << random << " ";
                                x = x + 50;
                                if (random == 1) {
                                    Board[i][j].setTexture(RCandy);
                                    Board[i][j].setPosition(x, y);
                                    Board[i][j].setScale(Vector2f(0.2f, 0.2f));
                                }
                                else if (random == 2) {
                                    Board[i][j].setTexture(OCandy);
                                    Board[i][j].setPosition(x, y);
                                    Board[i][j].setScale(Vector2f(0.2f, 0.2f));
                                }
                                else if (random == 3) {
                                    Board[i][j].setTexture(YCandy);
                                    Board[i][j].setPosition(x, y);
                                    Board[i][j].setScale(Vector2f(0.2f, 0.2f));
                                }
                                else if (random == 4) {
                                    Board[i][j].setTexture(GCandy);
                                    Board[i][j].setPosition(x, y);
                                    Board[i][j].setScale(Vector2f(0.2f, 0.2f));
                                }
                                else if (random == 5) {
                                    Board[i][j].setTexture(BCandy);
                                    Board[i][j].setPosition(x, y);
                                    Board[i][j].setScale(Vector2f(0.2f, 0.2f));
                                }
                                window.draw(Board[i][j]);
                                window.display();`
                            }
                            cout << endl;
                            y = y + 50;
                        }
                        
                            
                    }
                }
                break;


            }
        }
        

        
        window.draw(player);
        if (!deletePlay) {
            window.draw(start);
        }
        window.display();
    }

}

您必须在事件循环之外绘制您的板。 现在,您的看板只显示一次(当您单击鼠标左键时)。你应该在 'window.clear();':

之后写类似的东西
for (i = 0; i < 8; i++) 
   for (j = 0; j < 8; j++) 
      window.draw(Board[i][j]);

此外,您必须仅在游戏循环结束时使用 'window.display();' 一次。 如果你想只要按下鼠标左键就可以绘制棋盘,你可以使用:

if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
}