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

Author Topic: Code isn't working. Can't seem to find why. Sprite problems  (Read 2725 times)

0 Members and 1 Guest are viewing this topic.

KasHKoW

  • Newbie
  • *
  • Posts: 41
    • View Profile
Basically it's a "computer" that handles the paddle and watches the ball to move where the ball is going.

When the ball reaches GetPosition().x >= 400, it will start to move to where it's going. It works going down, but when it's needed to go up, it doesn't move at all.

Paddle & gBall inherit sprite characteristics

dir is a variable used within gBall to find out which direction it is going.
2 = right down 3 = right up

Code: [Select]


class Computer
{
public:
Paddle * IControl;
gBall * IWatch;
float BPD;

void Set(Paddle * CPaddle, gBall * IWatchIt);
void SetBall(gBall * IWatchIt){ IWatch = IWatchIt;}
void SetPlayer(Paddle * CPaddle){ IControl = CPaddle;}
void play();
float crunch();
void move();
};

void Computer::play()
{
if ( IWatch->GetPosition().x > 400 && IWatch->dir == 3 )
{
BPD = crunch();
if ( BPD > 50)
move();
}

if ( IWatch->GetPosition().x > 400 && IWatch->dir == 3 )
{
BPD = crunch();
if ( BPD < -50)
move();
}

if (IWatch->GetPosition().x > 400 && IWatch->dir == 2 )
{
BPD = crunch();
if(BPD > 50)
move();
}
if (IWatch->GetPosition().x > 400 && IWatch->dir == 2 )
{
BPD = crunch();
if (BPD < -50)
move();
}
}

float Computer::crunch ()
{
float copy;
if (IWatch->dir == 3 && IControl->GetPosition().y <= 300.f)
copy = IWatch->GetPosition().y - IControl->GetPosition().y;

else if (IWatch->dir ==  3 && IControl->GetPosition().y >= 300.f)
copy = IWatch->GetPosition().y - IControl->GetPosition().y;

else if (IWatch->dir == 2 && IControl->GetPosition().y <= 300.f)
copy = IWatch->GetPosition().y - IControl->GetPosition().y;

else if (IWatch->dir == 2 && IControl->GetPosition().y >= 300.f)
copy = IWatch->GetPosition().y - IControl->GetPosition().y;
std::cout << BPD << "        " << IControl->GetPosition().y << "        " << IWatch->GetPosition().y << "    " << IWatch->dir << std::endl;
return copy;
}

void Computer::move ()
{

if(IWatch->dir == 3)
if (BPD >= (-50))
IControl->Move(0.f, -.5f);  // doesn't work
if (BPD >= 50)
IControl->Move(0.f, .5f);


 
if(IWatch->dir == 2)
if (BPD >= (-50))
IControl->Move(0.f, -.5f); // doesn't work
if (BPD >= 50)
IControl->Move(0.f, .5f);

}

Vit

  • Newbie
  • *
  • Posts: 14
    • View Profile
Code isn't working. Can't seem to find why. Sprite problems
« Reply #1 on: June 28, 2011, 11:45:06 am »
You've used >= instead of < (or <=).

GatorQue

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: Code isn't working. Can't seem to find why. Sprite probl
« Reply #2 on: June 28, 2011, 04:06:27 pm »
I think you might also be missing some {}'s here:

Quote from: "KasHKoW"

Code: [Select]



void Computer::move ()
{

if(IWatch->dir == 3)
   {
if (BPD >= (-50))
IControl->Move(0.f, -.5f);  // doesn't work
if (BPD >= 50)
IControl->Move(0.f, .5f);
   }

 
if(IWatch->dir == 2)
   {
if (BPD >= (-50))
IControl->Move(0.f, -.5f); // doesn't work
if (BPD >= 50)
IControl->Move(0.f, .5f);
   }
}

KasHKoW

  • Newbie
  • *
  • Posts: 41
    • View Profile
Code isn't working. Can't seem to find why. Sprite problems
« Reply #3 on: June 28, 2011, 08:25:28 pm »
Quote from: "Vit"
You've used >= instead of < (or <=).


Hmm?
Why is that a problem?
And just flipping around these two values comprising of the expression would fix this?

EDIT:Oic what you're talking about.

Thanks  both of you :D