简体   繁体   中英

C++ Game Accessing separate classes variables

I've been trying to make a game in C++, using the graphics library SDL 2.0 but I've ran into a problem.

I have two classes User and Enemy. Here are cut down versions of both, just ask and I'll paste the whole thing

class User
{
    public:
       //Friends:
       friend class Enemy;


    protected:
    private:
        //The sprite position
        SDL_Rect SpritePos = {10, 100};
};



class Enemy
{
   public:
        //Friends:
        friend class User;

        Enemy();
       int CheckEnemy(SDL_Surface* &ScreenSurface);
       //Loads enemy images
       bool LoadEnemyMedia();

    protected:
    private:
        //Controls the AI movement
        void Movement();

        //Wizard 1's location on screen
        SDL_Rect Wizard1Pos = {50, 70};
};

The function Enemy::Movement finds out the location of the sprite and then moves itself accordingly, however it has can't get the current value of Spritepos, only the the value it initializes with, in this case 10. Here's the function Enemy::Movement's code. How can I fix this, bear in mind that the these functions are running in a loop.

void Enemy::Movement()
{
    User Player;


    if (Wizard1Pos.x < Player.SpritePos.x)
    {
        Wizard1Pos.x ++;

    }else if (Wizard1Pos.x > Player.SpritePos.x)
    {
        Wizard1Pos.x  --;
    }
}

Player.SpritePos.x will return the member x of the member SpritePos of the variable Player

If you only get the initialized value instead of the updated value, you are probably reading from a different copy than you are writing to. Until there is more info on how and where the data is written, I suggest you look into "Reference" and "Pointer".

Edit: I see your real code looks the same as the example, which I had assumed was simplified:

void Enemy::Movement()
{
    User Player;
    printf("%u\n", Player.RefTest.x);

    if (Wizard1Pos.x < Player.RefTest.x)
    {
        Wizard1Pos.x ++;
    }else if (Wizard1Pos.x > Player.RefTest.x )
    {
        Wizard1Pos.x  --;
    }
}

The line User Player; creates a new object of type User on the stack, which is then read from. At the end of the Movement() function, the stack variable Player is deleted. Player is never written to.

Creating a variable called Player in one place and then creating another variable called Player in another place does not in any way link the 2 (exception: global variables). Variable names have no influence on the behaviour of the code.

If you want Movement to refer to an already existing User you need to tell the Movement() method which Instance of User it is referring to. You do this by adding an argument to the method as follows:

void Enemy::Movement(const User & someCompletelyArbitraryVariableName)
{
    printf("%u\n", someCompletelyArbitraryVariableName.RefTest.x);

    if (Wizard1Pos.x < someCompletelyArbitraryVariableName.RefTest.x)
    {
        Wizard1Pos.x ++;
    }else if (Wizard1Pos.x > someCompletelyArbitraryVariableName.RefTest.x )
    {
        Wizard1Pos.x  --;
    }
}

The & tells the program to not make a copy. The const tells the program it's not allowed to change anything in player from within the Movement method. The const adds no value beyond preventing silly mistakes, which - as you will learn - is extremely beneficial.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM