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

时间:2015-07-06 06:45:11

标签: c++ if-statement sfml incompatibletypeerror

所以这里是细分:我正在学习SFML,并且我遇到了一个方法getSize()。x,它调用纹理/图像(代码中的texture.getSize()。x),返回宽度为整数,非常基本。我遇到的问题出现在下面的代码中。基本上,这个代码是在一个while循环中,它使变量“source.x”不断地从0变为1到2变为1到0然后无休止地重复(它的目的并不重要)。无论我做了什么,下面的代码都会无休止地减少source.x的值,因为它会在每次循环时执行if / else语句的第一部分,即使source.x的值远低于0由于某种原因,它点击了我将pTexture.getSize()。x转换为一个整数,只是为了检查它是否为1,然后代码神奇地起作用。令我感到困惑的是,为什么如果由于一些奇怪的原因,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;
}

完整代码:

#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(); 
        }
}

1 个答案:

答案 0 :(得分:0)

在纹理上调用getSize实际上会返回sf :: Vector2U。这是模板sf :: Vector2&lt;的typedef unsigned int&gt;

从技术上讲,你要检查source.x&lt; 0永远不会发生。你的source.x实际上是命中0xffffffff ...当你把它转换为int时,它是-1,但是作为unsigned int,它实际上是4294967295。