SFML community forums
Help => General => Topic started by: zpawlo00 on March 24, 2018, 01:54:27 pm
-
How can i get two sprites from different classes to put them to function which is created in third class?
-
By doing it. :D
What's the issue?
-
I dont now how to do this
My classes:
#pragma once
#include <vector>
#include <SFML\Graphics.hpp>
class TBlocks:public sf::Drawable
{
public:
TBlocks(float t_Y);
TBlocks()=delete;
virtual ~TBlocks() = default;
void update_block();
void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
sf::Texture block_texture;
sf::Sprite blockSprite;
sf::Texture *bTexture = &block_texture;
private:
std::string blocks[3];
};
#ifndef TPLAYER_H
#define TPLAYER_H
#include <SFML/Graphics.hpp>
class TPlayer : public sf::Drawable
{
public:
TPlayer(float t_X, float t_Y);
TPlayer() = delete;
virtual ~TPlayer() = default;
void update();
float moveRight();
float moveLeft();
void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
sf::Texture playerTexture;
sf::Sprite playerSprite;
sf::Texture *pTexture = &playerTexture;
private:
const double playerVelocity{4};
sf::Vector2f velocity{ playerVelocity, 0.f };
};
#endif // TPLAYER_H
and i want to use this fuction from third class:
bool TGame::checkCollision()
{
if (SPRITE.getGlobalBounds().intersects(SPRITE2.getGlobalBounds())
}
-
Implement a getBounds or similar function on both classes each retuning the global bound of their specific sprite.
-
eXpl0it3r I will appreciate if you will show me how to do it
-
You know how to write a function, right?
You know how to get the global bound of a sprite, right?
Now put these two together in your block and player class.
-
sf::FloatRect TBlocks::blockBound()
{
return blockSprite.getGlobalBounds();
}
sf::FloatRect TPlayer::playerBound()
{
return playerSprite.getGlobalBounds();
}
bool TGame::checkCollision()
{
if (player->playerBound().instersects(block->blockBound()))
{
return true;
}
}
}
}
error C2039: 'instersects': is not a member of 'sf::Rect<float>'
How can i correct this?
-
the error is probably triggered by a "typo": try "intersects" instead of "instersects".
-
Oh i didn't realise that thanks