If statement help

I was tasked with taking some pre written code, tweaking it and making a game out of it. What I got working was to move my tank around the screen using the arrow keys, and to repeatedly fire a shot. what I cant get working is to have it fire the shot only on the spacebar up command. here are some code snippets, any ideas what im doing wrong?

POINT gamePlayerPosition;
POINT pballPosition;

bool Space;

const int speed = 5;

case WM_KEYUP:
switch(wParam)
{
case VK_SPACE:
Space = true;
break;
}
return 0;

pballPosition.x = gamePlayerPosition.x -5;
pballPosition.y = gamePlayerPosition.y;
int shotSpeed = 10;

if(Space = true)
{
vector < POINT > shotVector(2);
shotVector[0].x;
shotVector[0].y;

pballPosition.y = pballPosition.y - shotSpeed;
//moves the paintball and controls speed
Space = false;
}

endPoint.x = pballPosition.x + 10; // size in x
endPoint.y = pballPosition.y + 10; // size in y

thePen.lopnColor = RED;
theBrush.lbColor = RED;

thePen.lopnWidth.x = 2;
thePen.lopnWidth.y = 2;

CPaintball pball(thePen, theBrush, pballPosition, endPoint);

pball.draw(hdc);


if (pballPosition.y < 5)
{
shotSpeed = 0;
}
closed account (zb0S216C)
cameronx9 wrote:
if(Space = true)

...is not what you think it is. What this statement actually does is assign Space to true. Once the assignment is made, Space is evaluated. In this case, it evaluates to true.

To test for equality the correct way, use ==. For instance: if(x == y).

Wazzak
Huzzah, thank you so much! It now fires the paintball, but sadly only moves it upon repeated clicks of the spacebar, and it only moves about 20 pixels per tap.
closed account (zb0S216C)
cameronx9 wrote:
but sadly only moves it upon repeated clicks of the spacebar, and it only moves about 20 pixels per tap.

What's the expected distance in pixels? How is it supposed to behave?

Wazzak
Ok, I am so thrilled! It fires and stops where I want it to for now.

It fires from the initial start point of the barrel of my gun.

ie. pballPosition.x = gamePlayerPosition.x -5;
pballPosition.y = gamePlayerPosition.y;

and I have it stop at 5 pixels from the edge of the screen. (for testing purposes) It now fly's all the way across the screen about 600 pixels. But I cannot repeat the process. It's a one shot deal, so i tried setting the

bool Space; to int Space;

but i get the same result, I am not sure why this process cannot be repeated.

and i forgot to mention i only get the desired result when i commented out

Space = false;
Last edited on
closed account (zb0S216C)
cameronx9 wrote:
But I cannot repeat the process.

Chances are, you haven't handled the reset scenario. Maybe you could implement some sort of trigger that indicates the paintball has landed, which in turn, informs the tank to reload?

Wazzak
Your suggestion sounds about right, but I am starting to get over my head on this. :) not sure how to implement that. I might have bitten off more then I can chew...
Topic archived. No new replies allowed.