SFML中的getPosition()问题

时间:2018-07-04 15:32:48

标签: c++ sfml

我是c ++的新手,所以我对此有些困惑。我试图让播放器产生,然后当用户按下“ C”时,播放器切换为汽车。当前正在发生的情况是,玩家最初在正确的位置生成,然后在玩家最初生成的位置(而不是当前位置)生成了汽车。但是,由于我使用的是函数getPosition(),因此我认为它会在播放器的当前位置生成。

Player.h:

#pragma once
#include <SFML\Graphics.hpp>
#include <string>
class player {

public:
    sf::Texture normalTexture;
    sf::Sprite normalSprite;
    sf::Texture carTexture;
    sf::Sprite carSprite;
    sf::Texture springTexture;
    sf::Sprite springSprite;
    sf::Texture rocketTexture;
    sf::Sprite rocketSprite;
    sf::IntRect normalRect[1];
    sf::IntRect carRect[1];
    sf::IntRect springRect[1];
    sf::IntRect rocketRect[1];


bool rocket=false;
bool car=false;
bool spring=false;
bool normal=false;

player();
player(sf::Vector2f position, std::string normalFileLoc, std::string carFileLoc, std::string springFileLoc, std::string rocketFileLoc);

void update();
};

Player.cpp:

#include "Player.h"

player::player() : player::player(sf::Vector2f(100, 0), "player.jpg", "car.png", "spring.png", "rocket.png") {
}

player::player(sf::Vector2f position, std::string normalFileLoc, std::string 
carFileLoc, std::string springFileLoc, std::string rocketFileLoc)
{
normalTexture.loadFromFile(normalFileLoc);
normalRect[0] = sf::IntRect(0, 0, 64, 128);
normalSprite.setTexture(normalTexture);
normalSprite.setTextureRect(normalRect[0]);
normalSprite.setOrigin(0,0);
normalSprite.setScale(1, 1);
normalSprite.setPosition(position.x, position.y);

carTexture.loadFromFile(carFileLoc);
carRect[0] = sf::IntRect(0, 0, 64, 64);
carSprite.setTexture(carTexture);
carSprite.setTextureRect(carRect[0]);
carSprite.setOrigin(0, -64);
carSprite.setScale(1, 1);
carSprite.setPosition(normalSprite.getPosition().x, normalSprite.getPosition().y);

}

Main.cpp我知道它看起来很粗糙,我打算以后再使其更好看:

#include <SFML/Graphics.hpp>
#include <SFML\Graphics\Rect.hpp>
#include "player.h"
int main()
{
sf::RenderWindow window(sf::VideoMode(750, 750), "SFML Works!");

player thePlayer;

const float gravity = .001;
int groundheight = 500;
sf::Vector2f velocity(sf::Vector2f(0, 0));

thePlayer.normal = true;


while (window.isOpen())
{
    sf::Event event;
    while (window.pollEvent(event))
    {
        if (event.type == sf::Event::Closed)
            window.close();

        if (event.type == sf::Event::KeyPressed)
        {
            if (thePlayer.car && event.key.code == sf::Keyboard::C)
            {
                thePlayer.car = false;
                thePlayer.spring = false;
                thePlayer.rocket = false;
                thePlayer.normal = true;
                break;
            }
            if (thePlayer.spring && event.key.code == sf::Keyboard::S)
            {
                thePlayer.car = false;
                thePlayer.spring = false;
                thePlayer.rocket = false;
                thePlayer.normal = true;
                break;
            }
            if (thePlayer.rocket && event.key.code == sf::Keyboard::R)
            {
                thePlayer.car = false;
                thePlayer.spring = false;
                thePlayer.rocket = false;
                thePlayer.normal = true;
                break;
            }
            if (event.key.code == sf::Keyboard::C)
            {
                thePlayer.car = true;
                thePlayer.spring = false;
                thePlayer.rocket = false;
                thePlayer.normal = false;
                break;
            }
            if (event.key.code == sf::Keyboard::R)
            {
                thePlayer.car = false;
                thePlayer.spring = false;
                thePlayer.rocket = true;
                thePlayer.normal = false;
                break;
            }
            if (event.key.code == sf::Keyboard::S)
            {
                thePlayer.car = false;
                thePlayer.spring = true;
                thePlayer.rocket = false;
                thePlayer.normal = false;
                break;
            }

        }

    }



    if (thePlayer.normal)
    {
        velocity.x = 0;
        thePlayer.normalSprite.move(velocity.x, velocity.y);
        if (thePlayer.normalSprite.getPosition().y < groundheight)
        {
            velocity.y += gravity;
        }
        else
        {
            thePlayer.normalSprite.setPosition(thePlayer.normalSprite.getPosition().x, groundheight);
            velocity.y = 0;
        }

    }

    if (thePlayer.car)
    {
        thePlayer.carSprite.move(velocity.x, velocity.y);
        if (thePlayer.carSprite.getPosition().y < groundheight)
        {
            velocity.y += gravity;
        }
        else
        {
            thePlayer.carSprite.setPosition(thePlayer.carSprite.getPosition().x, groundheight);
            velocity.y = 0;
        }

    }

    window.clear();
    if (thePlayer.normal)
    {
        window.draw(thePlayer.normalSprite);
    }
    if (thePlayer.car)
    {
        window.draw(thePlayer.carSprite);
    }

    window.display();
}
}

非常感谢!

2 个答案:

答案 0 :(得分:1)

在绘制汽车之前,您应该将其位置调整到玩家的位置:

if (thePlayer.car)
{
    thePlayer.carSprite.setPosition(thePlayer.normalSprite.getPosition());
    window.draw(thePlayer.carSprite);
}

但是正如ractiv所说,您应该以不同的方式组织代码。我建议您在尝试进行此类游戏之前,先了解有关c ++“良好实践”的更多信息。

答案 1 :(得分:0)

我们错过了main函数,无法确切知道汽车产生时会发生什么,但是我认为您是在Player构造函数中初始化汽车的,而在玩家按下'C'时不会移动它。

您应该有这样的东西:

if(event.key.code == sf::Keyboard::C) {
    player.spawnCar();
}

以及您的spawnCar()函数中:

carSprite.setPosition(normalSprite.getPosition());

编辑

正如我在评论中所说,您应该使用getter和setter来访问您的属性。根据您当前的main.cpp,您应该具有以下内容:

if (event.key.code == sf::Keyboard::C)
{
    thePlayer.car = true;
    thePlayer.spring = false;
    thePlayer.rocket = false;
    thePlayer.normal = false;
    carSprite.setPosition(normalSprite.getPosition());
    break;
}