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

Author Topic: Problem with fast sprite displaying  (Read 1167 times)

0 Members and 3 Guests are viewing this topic.

Simsonomia

  • Guest
Problem with fast sprite displaying
« on: December 28, 2015, 01:08:42 am »
Hello people! I have two problems that are making my a little bit angry.
1. Problem with fast and dynamic background displaying. When I try to display it (while the sprites are colliding)... It doesn't appear as many time as it should appear. What should I do to improve this?
2. Problems with dynamic sprite movement. When I subtract some points (during the collision) the speed of moving of sprites is getting slower. How can I prevent that?

Code:
1.
void game1()
{
   srand(time(0));
   window.clear(Color::Black);

   RectangleShape collision1;
   collision1.setFillColor(Color(140, 10, 10, 105));
   collision1.setSize(Vector2f(1280, 720));
   collision1.setPosition(Vector2f(0, 0));
   
   player.drawPlayer(*playerXPointer, *playerYPointer);

   for (int i = 0; i < 20; i++)
   {
      if (g1_collision == true)
      {
         window.draw(collision1);
      }
   }
   
   for (int i = 0; i < 20; i++)
   {
      asteroidsX[i] = rand() % 1280 + 1;
      asteroidsY = rand() % 720 + 1;
      asteroidsSpeedX = rand() % 2 + 1;
   }

   for (int i = 0; i < 20; i++)
   {
      asteroids.setRadius(20);
      asteroids.setPosition(Vector2f(asteroidsSpeedX * asteroidsMoveX + asteroidsX, asteroidsY));
      asteroids.setFillColor(Color::Black);
      asteroids.setOutlineColor(Color::White);
      asteroids.setOutlineThickness(2);
      window.draw(asteroids);
   }
}

2.
if (g1_asteroids == true)
                {
                        for (int i = 0; i < 20; i++)
                        {
                                asteroidsMoveX[i] -= 1;
                        }

                        for (int i = 0; i < 20; i++)
                        {
                                if (asteroidsMoveX[i] + asteroidsX[i] < -1)
                                {
                                        asteroidsMoveX[i] = 1280;
                                }
                        }

                        for (int i = 0; i < 20; i++)
                        {
                                if (asteroidsX[i] + asteroidsMoveX[i] >= playerX && asteroidsX[i] + asteroidsMoveX[i] <= playerX + 80 && asteroidsY[i] >= playerY && asteroidsY[i] <= playerY + 80)
                                {
                                        g1_collision = true;
                                        *pointsPointer -= 1;
                                        cout << *pointsPointer << endl;
                                }
                                else
                                {
                                        g1_collision = false;
                                }
                        }
                }

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Problem with fast sprite displaying
« Reply #1 on: December 28, 2015, 01:27:21 am »
I don't understand what your questions mean. Could you clarify them?

Code 1:
What is this? This can't be an entire game as suggested by the function name. It looks like it might just be a rendering function/method but has some things that usually go in initialisations and done once (e.g. srand, setting random starting positions etc.)
You also don't explain what g1_collision, the player object, drawPlayer() or *playerXPointer and *playerYPointer are.

Code 2:
Again, some explanation and context would help here.

It would help everyone and speed up your solution if you read this forum thread.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*