SFML community forums
Help => General => Topic started by: Demian on May 15, 2010, 02:54:59 am
-
Hey
I have a fight with sprite-pointers.
I want to display multiple Sprites using pointers depending on score-condition.
Something like that:
If your score points are above 100 create one "box".
If your score points are above 200 create two "box" and so on.
My solution so far :
/// Game - Class ///////////////////////////
//... Here somewhere I define the pointer
sf::Sprite *ptrSprite;
// end Game - Class
int Game_run()
{
//...
if (Score >= 100 )
{
if (ptrSprite == 0)
{
// Create the new sprite
ptrSprite = new sf::Sprite(IMG_Plop);
// Set the position in y-axis and it's velocity to random.
// I don't know if it's alright, but I have more than one rand because it
// appears to me that the y is always quite the same or it is close by the last number.
short int randY=rand() % 480 + 1;
randY=rand() % 480 + 1;
randY=rand() % 480 + 1;
randY=rand() % 480 + 1;
randY=rand() % 480 + 1;
ptrSprite->SetPosition(810, randY);
// Set a random velocity between -50 and -200.
ptrSpriteVelocity = rand() % 200;
ptrSpriteVelocity -= 200;
}
}
// Below is the delete command
if (Score > 100 && ptrSprite != 0)
{
ptrSprite->Move(ptrSpriteVelocity * ElapsedTime,0);
// If the Sprite reaches the end delete it.
if (ptrSprite->GetPosition().x < -10)
{
delete ptrSprite;
ptrSprite = 0;
}
}
return 0 ;
}
I don't know how to do this.
My only idea is to make this for each pointer I define, so that this code appears as often as the number of pointers plus the condition of points.
But this wouldn't be a clear and well code.
Thanks : D
-
Why using pointers ?
You want to display a certain amount of boxes according to the score, right ?
Create a class to manage this.
(pseudocode)
class SCORE-BOXES
int score
setScore(int)
display/render/...
/*internal stuffs*/
With some inheritance (sf::Drawable) you can display you "SCORE-BOXES" the same way you display a sf::Sprite.
-
Why using pointers ?
You want to display a certain amount of boxes according to the score, right ?
Create a class to manage this.
(pseudocode)
class SCORE-BOXES
int score
setScore(int)
display/render/...
/*internal stuffs*/
With some inheritance (sf::Drawable) you can display you "SCORE-BOXES" the same way you display a sf::Sprite.
Hey thanks : D
Sometimes I have to think more about it.
Is the member Score now the condition for how many boxes should be displayed ?
And I didn't mention that I want a collision function.
Would it be right if it looks like that?
class Score-Boxes
{
int collision(sf::Sprite &Sprite)
//...
sf::sprite sprite_one;
sf::sprite sprite_two;
sf::sprite sprite_three;
sf::sprite sprite_four;
}
in the main.cpp it would look like this:
for (int i = 0;i < count of Sprites;i++)
{
if (i == 0)
collision(sprite_one)
else
if (i == 1)
collision(sprite_two)
// and so on ?
}
-
maybe.
I don't know, I haven't fought a lot about it. There are so many solutions, chose a good one.
-
for (int i = 0;i < count of Sprites;i++)
{
if (i == 0)
collision(sprite_one)
else
if (i == 1)
collision(sprite_two)
// and so on ?
}
Ugh. Why would you do that instead of using arrays?
sf::Sprite sprites[4];
for(int i; i < 4; ++i)
{
collision(sprites[i]);
}
-
Thank you all : D
I will definitely post the game later in the project forum as a open source : D
Ah, it's working well.