C++ C++;SFML整数在if/else语句中不起作用

C++ C++;SFML整数在if/else语句中不起作用,c++,if-statement,sfml,incompatibletypeerror,C++,If Statement,Sfml,Incompatibletypeerror,下面是故障:我正在学习SFML,遇到了一个方法getSize().x,您在纹理/图像(代码中是texture.getSize().x)上调用该方法,该方法将宽度作为一个整数返回,非常基本。我遇到的问题在下面的代码中。基本上,这段代码是在一个while循环中,使变量“source.x”不断地从0变为1、从2变为1、从0变为0,然后无休止地重复(其目的无关紧要)。无论我做了什么,下面的代码都会无限地减少source.x的值,因为每次循环时它都会执行if/else语句的第一部分,即使source.x的

下面是故障:我正在学习SFML,遇到了一个方法getSize().x,您在纹理/图像(代码中是texture.getSize().x)上调用该方法,该方法将宽度作为一个整数返回,非常基本。我遇到的问题在下面的代码中。基本上,这段代码是在一个while循环中,使变量“source.x”不断地从0变为1、从2变为1、从0变为0,然后无休止地重复(其目的无关紧要)。无论我做了什么,下面的代码都会无限地减少source.x的值,因为每次循环时它都会执行if/else语句的第一部分,即使source.x的值远低于0。出于某种原因,它单击我将pTexture.getSize().x转换为一个整数,只是为了再次检查它是否为一,然后代码神奇地工作了。让我感到困惑的是,为什么出于某种奇怪的原因,如果pTexture.getSize().x不是整数(每当我打印它时都是整数,我尝试用int类型的方法返回它以获得额外的确定性),为什么在比较不兼容的类型时不会出现编译器错误,以及为什么它总是假定条件为真。另外,pTexture.getSize().x的值为96以供参考。提前感谢,并为这篇文章的篇幅感到抱歉

source.x += posNeg;
if(source.x*32 >= (int)pTexture.getSize().x){ 
    posNeg = -1;
    source.x -= 2;
    std::cout << "if called" << std::endl;
} else if(source.x < 0){
    posNeg = 1;
    source.x += 2;
    std::cout << "else called" << std::endl;
}
source.x+=posNeg;
如果(source.x*32>=(int)pTexture.getSize().x){
posNeg=-1;
来源:x-=2;

std::cout在纹理上调用getSize实际上返回sf::Vector2U。这是模板sf::Vector2


所以从技术上来说,你检查source.x<0是不会发生的。你的source.x实际上达到了0xffffffff…当你把它转换成一个int时,它是-1,但作为一个无符号int,它实际上是4294967295。

好吧,我的答案是错误的。你能发布一下你从这个运行中得到的控制台输出吗?
#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>

int main(){
    enum Direction { Down, Left, Right, Up}; 

    sf::Vector2i source(1, Down);

    float frameCounter = 0, switchFrame = 350, frameSpeed = 500;
    int posNeg = 1;

    sf::RenderWindow Window;
    Window.create(sf::VideoMode(800, 600), "A Window");

    Window.setKeyRepeatEnabled(false);

    sf::Texture pTexture;
    sf::Sprite playerImage;
    sf::Clock clock;

    if(!pTexture.loadFromFile("Pixel Dude.png")){
        std::cout << "Error: ould not load player image" << std::endl;
    }
    playerImage.setTexture(pTexture);

    std::cout << "Width:" << pTexture.getSize().x << std::endl;
    while(Window.isOpen()){

        sf::Event Event;
        while(Window.pollEvent(Event)){

            switch(Event.type){
            case sf::Event::Closed:
                Window.close();
                break;
            }
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){ 
            source.y = Down;
            playerImage.move(0, 1); 
        } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
            source.y = Left;
            playerImage.move(-1,0);
        } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
            source.y = Right;
            playerImage.move(1,0);
        } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
            source.y = Up;
            playerImage.move(0,-1);
        }


        frameCounter += frameSpeed*clock.restart().asSeconds(); 

        if(frameCounter >= switchFrame){
            frameCounter = 0;
        std::cout << "source.x at beginning: " << source.x << std::endl;
        std::cout << "posNeg at beginning: " << posNeg << std::endl;
            source.x += posNeg; 
            if(source.x*32 >= (int)pTexture.getSize().x){
                posNeg = -1;
                source.x -= 2;
                std::cout << "if called" << std::endl;
            } else if(source.x < 0){
                posNeg = 1;
                source.x += 2;
                std::cout << "else called" << std::endl;
            }else{

            }
        std::cout << "source.x at the end: " << source.x << std::endl;
        std::cout << "posNeg at the end: " << posNeg << std::endl;
        std::cout << "-------------------" << std::endl;
        }

        playerImage.setTextureRect(sf::IntRect(source.x*32, source.y*32, 32, 32)); 
        Window.draw(playerImage);
        Window.display();
        Window.clear(); 
        }
}