Hey so i’m making a function that gathers up sprites that you wanna keep on screen, and
then “Check()s” them to make shure they stay, making them bounce of the edge of the screen otherwise.
I have defined a struct inside my class (‘Object’) which holds a refrence to the sprite, and whether it’s still outside the screen or not.
(as so the reflect() function isn’t called again before the sprite actually returns to being inside the screen)
I have three questions regarding the following code:
-
Will saving a refrence to a sprite in my Object struct allow me to reach the original sprite, and not just a copy when calling reflect()?
-
Why is the ‘Object’ Struct underlined in red, saying
Error: implicitly generated constructor for class "OnScreenCheck::Object" cannot initialize. -
In the Constructor, i create an Object instance, and pass it on into the Objects vector. I would think since i pass it on to the vector the vector would make a copy of it and save it, but i’m also wondering if the instance would be destroyed after the Constructors done, screwing something up. Will it Work?
Thanks for any help! Here’s the Functor:
class OnScreenCheck
{
public:
struct Object{
mySprite& Sprite;
bool OffScreen;};
void AddObject(mySprite& aSprite){
Object newObject;
newObject.Sprite= aSprite;
newObject.OffScreen= false;
Objects.push_back(newObject);
};
void Check(){
// make sure box1 stays on the screen
for(int I=0; I< Objects.size(); I++){
//sprite has gone to high or to low
bool yColl (Objects[I].Sprite.Side(mySprite::top)>=ScreenHeight
|| Objects[I].Sprite.Side(mySprite::bottom)<=0);
//sprite has gone to far left or right
bool xColl (Objects[I].Sprite.Side(mySprite::left)<=0
|| Objects[I].Sprite.Side(mySprite::right)>ScreenWidth);
if(Objects[I].OffScreen==false){
if(yColl){
Objects[I].OffScreen= true;
Objects[I].Sprite.Reflect(180);}
if(xColl){
Objects[I].OffScreen= true;
Objects[I].Sprite.Reflect(90);}
}
}
};
EDIT: also, my intentions with struct Object are not to have a public data type that belongs to OnScreenCheck, but to make a data type only usable by OnScreenCheck, as it saves these “objects” into the vector Objects. It is simply meant to organize the Data in OnScreenCheck. How can i make it like this, and so i don’t have to initialize a Object rightaway in the OnScreenCheck constructor?
Will saving a refrence to a sprite in my Object struct allow me to reach the original sprite, and not just a copy when calling reflect()?
References need to be initialize at the time of creation and they remain bound to the type object they are alias to So Saving a reference to
spritewill always keep pointing to the samespriteobject, any attempt to make it alias to something else will result in an error.Why is the ‘Object’ Struct underlined in red, saying
Error: implicitly generated constructor for class "OnScreenCheck::Object" cannot initialize.You need to provide a constructor for
OnScreenCheckwhich will appropriately initializeObjectmember, especially the reference member tosprite. The error is specifically because references MUST be initialized at time of creation.In the Constructor, i create an Object instance, and pass it on into the Objects vector. I would think since i pass it on to the vector the vector would make a copy of it and save it, but i’m also wondering if the instance would be destroyed after the Constructors done, screwing something up. Will it Work?
The vector will save a copy of the object and will not be destroyed unless you explicitly ask the vector to do so through vector functions, unless ofcourse your object has some pointer types.