Trying to receive a vector2f from an object pointer

90 views Asked by At

My main function checks if a key has been fired, and if it is left arrow/right arrow it calls a function in an object named Game that I created. When the code reaches the function inside Game I get an "access violation reading location" error.

This is my main function:

int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "PongZzZZzzZ");

Ball *b = new Ball();
Paddle *p = new Paddle();
Game *game = new Game(&window, b, p);

while (window.isOpen())
{
    sf::Event event;
    while (window.pollEvent(event))
    {
        if (event.type == sf::Event::Closed)
            window.close();
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
            game->ParseKey('r');
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
            game->ParseKey('l');
    }
    window.clear(sf::Color::White);

    window.draw(*p);

    window.display();
}

return 0;
}

This is my function inside game:

void Game::ParseKey(char dir)
{
if (dir == 'r' && (this->p->GetPosition().x + this->p->getSize().x) < 795)
{
    std::cout << "right key fired";
}
else if (dir == 'l' && (this->p->getPosition().x) > 5)
{
    std::cout << "left key fired";
}
}

I know the problem is when I try to get the vector and get the x here:

this->p->GetPosition().x

I tried just getting the vector and it worked, but when I try to get the 'x' it crashes.

0

There are 0 answers