Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

2D All the way! / Help with Scoring Function

Author
Message
Dum_Bass
11
Years of Service
User Offline
Joined: 20th Mar 2013
Location: Hiding in your closet.
Posted: 20th Mar 2013 06:38
I am trying to create a simple scoring function using sprite collision. I have my sprite ids defined under a seperate header and my class is in order. LEFT_SCORE is animated using a sprite sheet (obviously). I am only using DarkGDK and no other libraries.

int Ball::leftGoal( )
{
int leftScore = 1;
if (dbSpriteCollision (BALL, 12))
{++leftScore;}
dbSetSpriteFrame (LEFT_SCORE, leftScore);
return 0;
}

The output I am getting is that leftScore increases as expected and the LEFT_SCORE sprite acts appropriately... But, the score never tallies. It works only while the two sprites (BALL and 12) are actually touching. After that the value of leftScore returns to 1. I am close to killing myself with a rubber mallet because I just can't think my way through how to structure the code.

I am obviously a noob and my comfort zone falls short of using pointers and other such complex concepts. Any help would be greatly appriciated.
xCatalyst
11
Years of Service
User Offline
Joined: 5th Dec 2012
Location: Pittsburgh, PA
Posted: 20th Mar 2013 20:28 Edited at: 20th Mar 2013 20:29
Well, from what I can tell, every time that function is called, you are setting 'int leftScore = 1' thus, resetting leftScore to 1 which could be the reason that value is being outputted. Here's a hint, make leftScore a global variable (ie. at the start of your code, after your includes) and take out 'int leftScore = 1' from that function. This way, everytime you call the command 'leftScore++', it will add incrementally as it should.

Dum_Bass
11
Years of Service
User Offline
Joined: 20th Mar 2013
Location: Hiding in your closet.
Posted: 20th Mar 2013 23:24
Absolutely right.

I came to the same conclusion after attacking the problem with a proper nap. I just Must not have had enough sleep.

For anyone who stumbles onto this thread with a similar problem; I had to initialize my scoring intiger (leftScore) at the beginning of Ball.cpp before all other functions. Then I had to move and hide one of the sprites upon collision to keep the score from incrimenting infinitely. After that it, all worked as I had planned.

Thanks for the input xCatalyst.
xCatalyst
11
Years of Service
User Offline
Joined: 5th Dec 2012
Location: Pittsburgh, PA
Posted: 27th Mar 2013 01:59
No problem man, glad to help

Login to post a reply

Server time is: 2024-04-19 16:21:18
Your offset time is: 2024-04-19 16:21:18