类中主要cpp错误中的变量

时间:2018-09-28 21:14:11

标签: c++ sfml

我创建了一个takeDommage类,用于应用数字或dmg并激活倒计时以创建无敌框架。

我在课堂上给我“ int pv;”,然后我检查了“ int pv;”他没有动。 怎么了 ? pv在main之前初始化并且正在工作,然后我做pv- = 1;手动Oo

主要:

    //{ Include
#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include<vector>
#include <collision.h>



//}

//{ Constantes
//Constantes ecran
    int tailleEcranX = 1280;
    int tailleEcranY = 720;

//Constantes perso
    int scalePerso = 3;
    int tailleSpriteX = 32;
    int tailleSpriteY = 48;
    int speed(4);
    int speedSprinte(10);
    int milieuSpriteX = (tailleSpriteX/2)*scalePerso;
    int milieuSpriteY = (tailleSpriteY/2)*scalePerso;

    int pv = 100;
    unsigned int pvMax = 100;

    int eSpeed = 1;
//}

//{ Initialisation

//Initiation des dessins
    sf::RenderWindow window;
    sf::RectangleShape rect;
    sf::Texture perso;
    sf::Texture ennemis;
    sf::Sprite sprite_perso;
    sf::Sprite sprite_ennemis;
    sf::View view;
    sf::RectangleShape rectCol;

    sf::RectangleShape pvBar;
    sf::RectangleShape pvMaxBar;

    enum Dir{Down,Left,Right,Up};

    sf::Vector2i anim (1,Down);

#include "gestion_clavier.h"
#include <takeDommage.h>




//}

//{ Main

int main()
{
    //{ Positionnement des objets

        window.create(sf::VideoMode(tailleEcranX , tailleEcranY), "The Game I");
        window.setPosition(sf::Vector2i(500,250));
        window.setFramerateLimit(120);

    //Fond d'ecran
        rect.setFillColor(sf::Color(110,155,255));
        rect.setSize(sf::Vector2f(tailleEcranX-10,tailleEcranY-10));
        rect.setPosition(5,5);
        rect.setOutlineColor(sf::Color(255,255,255));
        rect.setOutlineThickness(3);

    //rectangle de collision test
        rectCol.setFillColor(sf::Color(0,0,200));
        rectCol.setSize(sf::Vector2f(50,50));
        rectCol.setPosition(400,500);
        rectCol.setOutlineColor(sf::Color(255,255,255));
        rectCol.setOutlineThickness(1);



    //Bar pv
        pvBar.setFillColor(sf::Color(20,255,30));
        pvBar.setPosition(20,20);
        pvMaxBar.setFillColor(sf::Color(0,0,0));
        pvMaxBar.setPosition(20,20);
        pvMaxBar.setOutlineColor(sf::Color(255,255,255));
        pvMaxBar.setOutlineThickness(2);

    //Perso
        sprite_perso.setTexture(perso);
        sprite_perso.setPosition(tailleEcranX/2-milieuSpriteX,tailleEcranY/2-milieuSpriteY);
        sprite_perso.setScale(scalePerso,scalePerso);

    //Ennemis
        sprite_ennemis.setTexture(ennemis);
        sprite_ennemis.setPosition(tailleEcranX/2-milieuSpriteX,tailleEcranY/2-milieuSpriteY);
        sprite_ennemis.setTextureRect(sf::IntRect(anim.x*tailleSpriteX,anim.y*tailleSpriteY,tailleSpriteX,tailleSpriteY));
        sprite_ennemis.setScale(scalePerso,scalePerso);


    //Ennemis
        sf::RectangleShape enemy;
        enemy.setFillColor(sf::Color(200,0,0));
        enemy.setSize(sf::Vector2f(50.f, 50.f));

        takeDommage obj;

    //Clock
        sf::Clock time;
        //sf::Clock takeDammageClock;


    //}

    //{Chargement des Sprites


        if (!perso.loadFromFile("link/srpite.png",sf::IntRect(0,0,96,192)))
        {
            std::cout<<"erreur chargement player image"<<std::endl;
        }

        if (!ennemis.loadFromFile("link/srpite.png",sf::IntRect(288,0,96,192)))
        {
            std::cout<<"erreur chargement player image"<<std::endl;
        }


    //}




    //{ Game Loop

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

    //Stat avec rafrechisement

            //Bar pv
                pvMaxBar.setSize(sf::Vector2f(4*pvMax,10));
                pvBar.setSize(sf::Vector2f(4*pv,10));
            //Perso
                sprite_perso.setTextureRect(sf::IntRect(anim.x*tailleSpriteX,anim.y*tailleSpriteY,tailleSpriteX,tailleSpriteY));

            //Ennemy
                std::vector<sf::RectangleShape> enemies;
                enemies.push_back(sf::RectangleShape(enemy));
                int enemySpawnTimer = 0;


    ProcessInput();


    //gestion_clavier();


                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Z)||sf::Keyboard::isKeyPressed(sf::Keyboard::S)||sf::Keyboard::isKeyPressed(sf::Keyboard::D)||sf::Keyboard::isKeyPressed(sf::Keyboard::Q))
                {
                    if (time.getElapsedTime().asMilliseconds()>= 50)
                    {
                                anim.x++;
                        if(anim.x*tailleSpriteX >= perso.getSize().x)
                           anim.x=0;

                            time.restart();
                    }
                }


    //Boucle Pv

                if(pv>=pvMax)
                {
                    pv=pvMax;
                }

                if(pv<=0)
                {
                    pv=0;
                }



    //ENEMIES

                if(enemySpawnTimer < 50)
                    enemySpawnTimer++;

                if (enemySpawnTimer >= 50)
                {
                    enemy.setPosition((rand() % int(window.getSize().x - enemy.getSize().x)), 0.f);
                    enemies.push_back(sf::RectangleShape(enemy));

                    enemySpawnTimer = 0;
                }

                for (size_t i = 0; i < enemies.size(); i++)
                {
                    enemies[i].move(0, eSpeed);

                    if (enemies[i].getPosition().y > window.getSize().y)
                        enemies.erase(enemies.begin() + i);
                }



    //Collision
                if(Collision::PixelPerfectTest(sprite_perso,sprite_ennemis))
                {
                    //std::cout<<"collision pp"<<std::endl;
                     obj.prendreDegat(50 ,pv);
                    //std::cout<<pv<<std::endl;
                }


    //Dessinage
                window.draw(rect);
                window.draw(rectCol);
                window.draw(sprite_perso);
                window.draw(sprite_ennemis);

                window.draw(pvMaxBar);
                window.draw(pvBar);


    //Boucle dessinage

                for (size_t i = 0; i < enemies.size(); i++)
                {
                    window.draw(enemies[i]);

                }

                window.display();
                window.clear();


    }

    //}


    return 0;
}

takeDommage.h:

      #ifndef TAKEDOMMAGE_H
#define TAKEDOMMAGE_H

    #include <SFML/Graphics.hpp>
    #include <string>
    #include <iostream>
    #include <cstdlib>
    #include <cmath>


class takeDommage
{
    public:
        takeDommage();
        void prendreDegat(int Dommage, int pv);

        //virtual ~takeDommage();

    protected:
        sf::Clock takeDammageClock;
        int Dommage;

    private:
};

#endif // TAKEDOMMAGE_H

takeDommage.cpp:

#include "takeDommage.h"

takeDommage::takeDommage()
{

}

void takeDommage::prendreDegat(int Dommage, int pv)
{

    if(takeDammageClock.getElapsedTime().asSeconds()>3)
        {
            std::cout << "bite" << std::endl;
            pv -= Dommage;
            takeDammageClock.restart();
        }
}

1 个答案:

答案 0 :(得分:-1)

 if(Collision::PixelPerfectTest(sprite_perso,sprite_ennemis))
 {
     //std::cout<<"collision pp"<<std::endl;
     obj.prendreDegat(50 ,pv);
     //std::cout<<pv<<std::endl;
 }

您正在按值传递PV。

这会导致void takeDommage::prendreDegat(int Dommage, int pv)制作pv的本地副本,并将值减Dommage。

您要做的是通过引用传递它。在c ++中,您可以按Type&

因此您需要将takeDommage更改为void takeDommage::prendreDegat(int Dommage, int& pv)

由于pv现在已通过引用传递,因此您在takeDommage::prendreDegat中进行的任何操作都会影响全局pv

编辑:传递全局变量不被视为一种良好的代码习惯。但是由于您只是开始使用c ++,因此我不会对此进行详细介绍。尝试将std::cout<<pv<<std::endl从您的主体移至void takeDommage::prendreDegat(int Dommage, int pv),而不作任何更改,看看会发生什么。