Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED]Drawing & Moving a player/sprite object.  (Read 1448 times)

0 Members and 1 Guest are viewing this topic.

owenmin

  • Newbie
  • *
  • Posts: 6
    • View Profile
[SOLVED]Drawing & Moving a player/sprite object.
« on: March 10, 2013, 03:41:01 am »
I have a player class setup with each object having it's own sprite. It draws to the screen like i want it to and to the position i set it to, but when i try to move the object around like you normally would by using something like 'player.move(x, y);', it just refuses to move.

I'm quite new to programming and game development in general so i'm quite hesitant to share my code in fear of looking like an idiot, but here goes.

This is my player class.

class Player
{
private:
        int health;
        bool isAlive;
        Sprite playerSprite;
public:
        Player(int h, Sprite &pSprite){ health = h; playerSprite = pSprite; }
        ~Player(){}

        void drawSprite();
        void setPos(float x, float y);
        void makeMove(float velx, float vely);
};

Excuse all the common errors you will see in my class, it will be refined over time.

Anyway, the way i have it setup is that, i call a function in the main game loop called 'playerControl' and pass my player object to it, then inside the function, i have this:

void playerControl(Player player)
{
        if(Keyboard::isKeyPressed(Keyboard::Left)){
                player.makeMove(-1.5f, 0);
        }
        else if(Keyboard::isKeyPressed(Keyboard::Right)){
                player.makeMove(1.5f, 0);
        }
        else if(Keyboard::isKeyPressed(Keyboard::Space)){
                player.makeMove(0, -2.5f);
        }
}

.makeMove is a function from the 'Player' class:

void Player::makeMove(float velx, float vely){
        playerSprite.move(velx, vely);
}

Using this setup, i though i would be able to move the Player object that i passed to the function around. Nothing happens though. It draws to the screen but after that, it won't move.

Am i going about this the wrong way? Well, i know i am but how wrong?
« Last Edit: March 10, 2013, 04:19:42 am by owenmin »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Drawing & Moving a player/sprite object.
« Reply #1 on: March 10, 2013, 04:12:39 am »
In C++, void playerControl(Player player) makes a copy of the passed argument, everything you do with this copy is made only on the copy, not on the "real" object.
Do you already know references in C++? If not, you should learn it. If you already do, use one here. ;)

owenmin

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Drawing & Moving a player/sprite object.
« Reply #2 on: March 10, 2013, 04:19:15 am »
Ah, i see now. I completely forgot that when i was passing the object to the function, i was only passing a copy.

Thank you.

 

anything