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

Author Topic: Sprite.SetImage problem[solved]  (Read 3490 times)

0 Members and 1 Guest are viewing this topic.

Devil0150

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Sprite.SetImage problem[solved]
« on: June 16, 2009, 07:40:41 pm »
This is my code, im trying to make my tetris game and i cant rotate my pieces. I am doing it by changing the image of the sprite when i press my up key but it doesnt work.

Code: [Select]
sf::Sprite* show;
int random = sf::Randomizer::Random(1, 5);
switch(random)
{
case 1:
{
show = &(myCube.mySprite);
break;
}
case 2:
{
show = &(myL.mySprite);
break;
}
case 3:
{
show = &(myBackL.mySprite);
break;
}
case 4:
{
show = &(myI.mySprite);
break;
}
case 5:
{
show = &(myT.mySprite);
break;
}
}
while (myClock.GetElapsedTime() <= Tetris.GetHeight())
{
while (Tetris.GetEvent(Termino))
{
if (Termino.Type == sf::Event::Closed)
Tetris.Close();

if ((Termino.Type == sf::Event::KeyPressed) && (Termino.Key.Code == sf::Key::Up))
{
if (random = 1);
else if (random = 2)
(*show).SetImage(myL.NextImage());
else if(random = 3)
(*show).SetImage(myBackL.NextImage());
else if (random = 4)
(*show).SetImage(myI.NextImage());
else
(*show).SetImage(myT.NextImage());
}

if ((Termino.Type == sf::Event::KeyPressed) && (Termino.Key.Code == sf::Key::Escape))
Tetris.Close();
}


Now here is the function to find the next image.

Code: [Select]
public:
sf::Image ImageT;
int rotation;

sf::Image T1;
sf::Image T2;
sf::Image T3;
sf::Image& NextImage()
{
T1.LoadFromFile("D:\\C++\\T1.jpg");
T2.LoadFromFile("D:\\C++\\T2.jpg");
T3.LoadFromFile("D:\\C++\\T3.jpg");
if (rotation == 0)
{
return T1;
++rotation;
}
else if (rotation == 1)
{
return T2;
++rotation;
}
else if (rotation == 2)
{
return T3;
++rotation;
}
else
{
return ImageT;
rotation = 0;
}
}


Can u help me?

P.S. I initialized my ImageT image in the constructor.

Meldron

  • Newbie
  • *
  • Posts: 1
    • View Profile
Sprite.SetImage problem[solved]
« Reply #1 on: June 17, 2009, 04:02:37 am »
Code: [Select]
if (rotation == 0)
      {
         return T1;
         ++rotation;
      }
      else if (rotation == 1)
      {
         return T2;
         ++rotation;
      }
      else if (rotation == 2)
      {
         return T3;
         ++rotation;
      }
      else
      {
         return ImageT;
         rotation = 0;
      }


you should try:

Code: [Select]
if (rotation == 0)
      {
        ++rotation;
         return T1;
         
      }
      else if (rotation == 1)
      {
         ++rotation;
        return T2;
         
      }
      else if (rotation == 2)
      {
         ++rotation;
          return T3;
         
      }
      else
      {
         rotation = 0;
         return ImageT;
         
      }

 :D

Devil0150

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Sprite.SetImage problem[solved]
« Reply #2 on: June 17, 2009, 09:54:03 am »
Still the same...

Devil0150

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Sprite.SetImage problem[solved]
« Reply #3 on: June 17, 2009, 10:23:03 am »
Ok, i fixed it. I just replaced all the (*show).SetImage with (*show).Rotate.
The piece rotates but 1 second after i press the button. Maybe it is because of this part of the code here:

Code: [Select]
while (myClock.GetElapsedTime() <= Tetris.GetHeight())
{
while (Tetris.GetEvent(Termino))
{
if (Termino.Type == sf::Event::Closed)
Tetris.Close();

if ((Termino.Type == sf::Event::KeyPressed) && (Termino.Key.Code == sf::Key::Up))
(*show).Rotate(90);

if ((Termino.Type == sf::Event::KeyPressed) && (Termino.Key.Code == sf::Key::Escape))
Tetris.Close();
}



// This is the part I was talking about, I made it for the piece to move down 1 block per second.

Tetris.Clear();
(*show).Move(0, 10);
while (myClock.GetElapsedTime() < 1.f);
myClock.Reset();
Tetris.Draw(*show);
Tetris.Display();
}

K-Bal

  • Full Member
  • ***
  • Posts: 104
    • View Profile
    • pencilcase.bandcamp.com
    • Email
Sprite.SetImage problem[solved]
« Reply #4 on: June 17, 2009, 10:30:48 am »
Quote from: "Devil0150"

Code: [Select]
while (myClock.GetElapsedTime() <= Tetris.GetHeight())


Why don't you use Tetris.IsOpened()?
Listen to my band: pencilcase.bandcamp.com

Devil0150

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Sprite.SetImage problem[solved]
« Reply #5 on: June 17, 2009, 10:34:09 am »
because i need to get another piece when this one gets to the bottom of the window.

Here is my main()

Code: [Select]
int main()
{
Cube myCube("D:\\C++\\cube.jpg");

L myL("D:\\C++\\L.jpg");

backL myBackL("D:\\C++\\LBack.jpg");

I myI("D:\\C++\\I.jpg");

T myT("D:\\C++\\T.jpg");

sf::Event Termino;
while (Tetris.IsOpened())
{
sf::Clock myClock;

sf::Sprite* show;
int random = sf::Randomizer::Random(1, 5);
switch(random)
{
case 1:
{
show = &(myCube.mySprite);
break;
}
case 2:
{
show = &(myL.mySprite);
break;
}
case 3:
{
show = &(myBackL.mySprite);
break;
}
case 4:
{
show = &(myI.mySprite);
break;
}
case 5:
{
show = &(myT.mySprite);
break;
}
}
while (myClock.GetElapsedTime() <= Tetris.GetHeight())
{
while (Tetris.GetEvent(Termino))
{
if (Termino.Type == sf::Event::Closed)
Tetris.Close();

if ((Termino.Type == sf::Event::KeyPressed) && (Termino.Key.Code == sf::Key::Up))
(*show).Rotate(90);

if ((Termino.Type == sf::Event::KeyPressed) && (Termino.Key.Code == sf::Key::Escape))
Tetris.Close();
}
sf::Clock my2Clock
Tetris.Clear();
(*show).Move(0, 10);
while (my2Clock.GetElapsedTime() < 1.f);
my2Clock.Reset();
Tetris.Draw(*show);
Tetris.Display();
}
myClock.Reset();
}
return 0;
}
[/code]

K-Bal

  • Full Member
  • ***
  • Posts: 104
    • View Profile
    • pencilcase.bandcamp.com
    • Email
Sprite.SetImage problem[solved]
« Reply #6 on: June 17, 2009, 11:19:24 am »
Code: [Select]

while (Tetris.IsOpened())
while (myClock.GetElapsedTime() <= Tetris.GetHeight())
while (Tetris.GetEvent(Termino))



This way you are blocking your game loop.
Listen to my band: pencilcase.bandcamp.com

Devil0150

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Sprite.SetImage problem[solved]
« Reply #7 on: June 19, 2009, 06:31:11 pm »
I fixed it. I put an if statement instead of the second.

if (my2Clock.GetElapsedTime() >= 1.f)
         {
            (*show).Move(0, 10);
            my2Clock.Reset();
         }

 

anything