SFML冲突从不在我的系统中注册

问题描述 投票:0回答:1

[尝试不使用教程而在SFML中首次在sfml中创建碰撞系统,而是使用基于数组的方法,例如:

    bool moright, moleft, moup, xcollision, ycollision;
float xvel, yvel;
int position, predictx, predicty, cm, arraynum;
class playerClass{
public:
    playerClass(){
    }
    void update()
{
        if (moright == true){
            xvel = 2;}
        if (moleft == true){
            xvel = -2;}
        if (!(moright || moleft)){
            if (xvel < 0)
                xvel = 0;
            if (xvel > 0)
                xvel = 0;}
}

};

int main()
{
    playerClass playerObject;
    // Create the main window
    RenderWindow window(VideoMode(1080, 720), "SFML window");

    // Load a sprite to display
    Texture texture;
    if (!texture.loadFromFile("gsquare100x100.png"))
        return EXIT_FAILURE;
    Sprite sprite(texture);
    Sprite floor(texture);
    Sprite wall(texture);
    floor.setPosition(Vector2f(0.f, 498.f));
    wall.setPosition(Vector2f(598.f,0.f));


    floor.setColor(Color(0, 255, 0));
    floor.setScale(12.f, 12.f);

    wall.setScale(12.f, 12.f);
    wall.setColor(Color(0, 0, 255));
  int collisions[2][4]{
 {0, 400, 500, 600},
 };

// Start the game loop
    while (window.isOpen())
{

        Vector2f position = sprite.getPosition();
        cout << position.y << endl;
        predictx = position.x + xvel;
        predicty = position.y + yvel;
        yvel = 1;
       for (arraynum = 0; arraynum < 2; arraynum++){
            if ((predictx > collisions[arraynum][0])&&(predictx < collisions[arraynum][1])&&(predicty > collisions[arraynum][2])&&(predicty < collisions[arraynum][3])){
                if ((position.y > collisions[arraynum][3])||(position.y < collisions[arraynum][2])){
                    xcollision = true;}
                if ((position.x > collisions[arraynum][1])||(position.x < collisions[arraynum][0])){
                    ycollision = true;}
                }
            }
        if (xcollision == true)
            xvel = 0;
            xcollision = false;
        if (ycollision == true)
            yvel = 0;
            ycollision = false;
        sprite.move(sf::Vector2f(0.f+xvel, 0.f+yvel));





        // Process events
        sf::Event event;
        while (window.pollEvent(event))
        {

          if (event.type == Event::KeyPressed)
              {if (event.key.code == Keyboard::D)
                   moright = true;
                if (event.key.code == Keyboard::A)
                   moleft = true;}

          if (event.type == Event::KeyReleased)
          {if (event.key.code == Keyboard::D)
               moright = false;
            if (event.key.code == Keyboard::A)
               moleft = false;}
         playerObject.update();

}

但是,碰撞永远不会发生,删除检查精灵从哪个方向移动的位无济于事。对于c ++来说,这是一个非常新的知识,如果这是一个愚蠢的问题,并且对于我可能过于复杂的碰撞系统,则表示歉意。

c++ sfml
1个回答
0
投票

由于信誉低,我无法发表评论。

根据显示的代码,似乎您从未在任何地方将xcollisionycollision设置为true

© www.soinside.com 2019 - 2024. All rights reserved.