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

Author Topic: Multiple new commands with pointer(s)  (Read 2825 times)

0 Members and 1 Guest are viewing this topic.

Demian

  • Newbie
  • *
  • Posts: 16
    • View Profile
Multiple new commands with pointer(s)
« 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 :
Code: [Select]

/// 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

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Multiple new commands with pointer(s)
« Reply #1 on: May 15, 2010, 11:39:53 am »
Why using pointers ?

You want to display a certain amount of boxes according to the score, right ?
Create a class to manage this.
Code: [Select]
(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.
SFML / OS X developer

Demian

  • Newbie
  • *
  • Posts: 16
    • View Profile
Multiple new commands with pointer(s)
« Reply #2 on: May 15, 2010, 04:46:28 pm »
Quote from: "Hiura"
Why using pointers ?

You want to display a certain amount of boxes according to the score, right ?
Create a class to manage this.
Code: [Select]
(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?

Code: [Select]
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:

Code: [Select]

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 ?
}

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Multiple new commands with pointer(s)
« Reply #3 on: May 15, 2010, 04:55:09 pm »
maybe.

I don't know, I haven't fought a lot about it. There are so many solutions, chose a good one.
SFML / OS X developer

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Multiple new commands with pointer(s)
« Reply #4 on: May 15, 2010, 09:27:31 pm »
Quote from: "Demian"

Code: [Select]

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?

Code: [Select]

sf::Sprite sprites[4];

for(int i; i < 4; ++i)
{
  collision(sprites[i]);
}

Demian

  • Newbie
  • *
  • Posts: 16
    • View Profile
Multiple new commands with pointer(s)
« Reply #5 on: May 17, 2010, 11:43:45 am »
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.

 

anything