SFML -- Why won't this code work?

I feel like shit having to pose for help on this code but oh well.

Also, does forward declarations of classes include its members too, or just the class ClassName;

if not the latter, then can you post that declaration of it again in the code... just not post its definitions twice?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

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);
		
}

Last edited on
What is the problem you're having?

And a forward declaration is the class name only, as you have in the snippet.
Well, i'm in a 800x600 grid. This grid has an object moving randomly. vertically 1 meaning the top 600 meaning the bottom. as an object lays on the right side it will follow the randomly moving object.

It will only move down, as it won't, for some bizarre reason, move upward. And on top of that sometimes it gets caught, the moving object that is.... but i haven't posted that, as I will fix that.

Like I said, it will only move down... and if the direction allows it. Which is direction 3 I believe.

Lines 26 and 39 are checking the exact same conditions as lines 19 and 33 respectively, I am unsure if this is intentional, but it doesn't look like it would be.
Hmm, yeah I probably noticed that some time ago... but forgot ha. But would that fix the problem?
I know I can make it more concise by not checking the same thing twice.... But they're all running on one thread. So I don't see it really slowing down or needing that optimization. Or am I missing something completely?
I could just reading it wrong. It just looks like you would mean to check greater than on line 19, less than on line 26, greater than on line 33, and less than on line 39. Where as currently you're checking greater than for every line.
No that's a limitation. The computer can only read it when it's at 400... or greater than 400.
Hmm.. Well I feel I should point out that I have no experience with SFML, so I'm just looking through the code to see if there's perhaps anything I can spot that wouldn't be library specific.

I'm still looking through it, but I have spotted one thing:
under float Computer::crunch(), if by any chance all of those if / else if statements fail, it will return copy uninitialized.
Last edited on
Ikaron, I see what you're saying, but how I set up how the game is played... that is the only way the ball move, therefore it won't.

Well, typically you don't want to leave bugs like that open, even if you're sure it will never happen. Especially when something like that would be easily solved with a final 'else' statement, which could even throw an error if by some chance all other statements fail.

For example, what if it wasn't programmed with a 0% chance of happening like you're expecting and instead had a .1% chance of happening? If that .1% occurs and you had it accounted for, you could know where the bug occured as soon as it happened. Otherwise you may never be able to debug it out and find it. Heck, some times a game can even go on for a little while with a bad return like that without any noticable effects before it crashes.

I'm not saying this may happen in this specific case, just saying that it's always good practice to account for such possibilities. Why leave a potential bug, no matter how remote, when you don't have to? Just a good habit to get into, imo.
Last edited on
Yeah I will definitely start doing that.

thanks
Topic archived. No new replies allowed.