【问题标题】:C++ SFML sprite not showing/workingC++ SFML sprite 不显示/工作
【发布时间】:2017-03-01 16:58:07
【问题描述】:

我正在尝试制作一个程序,让一个红色块从屏幕顶部掉下来,玩家必须避开它。当我运行程序时,我可以看到并移动玩家(桨),但我看不到敌人或“东西”精灵。请帮助我,因为我已经用代码尝试了所有方法,但它仍然不起作用:/

东西.h

#include <SFML/Graphics.hpp>
#include <iostream>
using namespace sf;
using namespace std;
#pragma once

class Thing
{
public:
void thingspawn(RenderWindow &gameDisplay, int &enemystartx, int &enemystarty, int &enemywidth, int &enemyheight, int &blockcolor)
{
    RectangleShape thing(Vector2f(enemywidth, enemyheight));
    thing.setFillColor(Color(blockcolor));
    thing.setPosition(enemystartx, enemystarty);
    gameDisplay.clear();
    gameDisplay.draw(thing);



}
};

main.cpp

#include <iostream>
#include <SFML/Graphics.hpp>
#include "things.h"
using namespace std;
using namespace sf;

int main()
{
RenderWindow gameDisplay(VideoMode(1366, 768), "Game", Style::Fullscreen);
gameDisplay.setMouseCursorVisible(false);
gameDisplay.clear();
int enemystarty = -200;
int enemystartx = 300;
int enemyheight = 100;
int enemywidth = 100;
int enemyspeed = 0.3f;
int enemycount = 1;
int dodged = 0;
int blockcolor = (255, 0, 0);

RectangleShape player(Vector2f(300, 30));
player.setFillColor(Color(0, 0, 255));
player.setPosition(400, 728);


while (gameDisplay.isOpen())
{
    Event evnt;
    while (gameDisplay.pollEvent(evnt))
    {
        switch (evnt.type)
        {
        case Event::Closed:
            gameDisplay.close();
        case Event::KeyPressed:
            if (Keyboard::isKeyPressed(Keyboard::Q))
                gameDisplay.close();

        }
    }


    if (Keyboard::isKeyPressed(Keyboard::Right))
        if (player.getPosition().x < 1000)
            player.move(0.2f, 0.0f);
    if (Keyboard::isKeyPressed(Keyboard::Left))
        if (player.getPosition().x > 50)
            player.move(-0.2f, 0.0f);

    Thing thingobject;
    thingobject.thingspawn(gameDisplay, enemystartx, enemystarty, enemywidth, enemyheight, blockcolor);

    enemystarty += enemyspeed;


    gameDisplay.draw(player);
    gameDisplay.display();


}

}

【问题讨论】:

    标签: c++ sfml 2d-games


    【解决方案1】:

    问题是您在函数 Thing::thingspawn(...) 中创建了 RectangleShape 事物,并且它在结束后被销毁。 尝试将其声明为 Thing 类的成员

    【讨论】:

    • 不过应该​​是临死前画出来的。
    • 尝试删除 gameDisplay.clear();从函数返回主循环。
    • 虽然这对 C++ 来说是个坏习惯,但它仍然可以工作,因为它已经将需要传递的所有数据传递给隐藏缓冲区,它不像隐藏缓冲区持有对矩形对象。
    • 谢谢大家,但我已经尝试了每个人的建议,但“东西”仍然没有出现;/
    • 其实我整理出来了别担心:£
    【解决方案2】:

    thing 渲染正确,但只是一种坏习惯。就像 capi1500 所说的,

    但真正的问题是,当您将int blockcolor = (255, 0, 0); 传递给sf::Color 时,它会产生黑色。你不会在黑色画布上看到黑色绘图吧?

    您可以在此处了解如何从整数构造颜色:How to pack ARGB to one integer uniquely?

    现在使用 sf::Color 的预定义颜色更改 thing 的颜色,如 sf::Color::Green 或使用另一个构造函数 Color (Uint8 red, Uint8 green, Uint8 blue, Uint8 alpha=255) 传递每个组件

    SFML 的 sf::Color 的文档:http://www.sfml-dev.org/documentation/2.4.2/classsf_1_1Color.php

    【讨论】:

      猜你喜欢
      • 2018-05-19
      • 1970-01-01
      • 1970-01-01
      • 2014-03-16
      • 1970-01-01
      • 2018-01-21
      • 1970-01-01
      • 1970-01-01
      • 2013-02-04
      相关资源
      最近更新 更多