Suppose your writing a simple game code and you want to have the user select any number of stages and each stage has 10 enemies and you only get 2 shots per stage (that the user can input). Lets look at the if code part...

When you test it goes good after the first shot, but the 2nd shot screws up on the count, getShoot should come back with the 2nd shot, and comes back on the 1st shot. Should I add shoot++; to that if statement? The enemies are doing great till I get to the 3 shoot() call, enemies should reset back to 10 after the 2nd shot, but my test is going into the negative.

It's not clear how your project is set up, but it appears you have multiple opportunities to change important game state variables, like stage, enemies, shots, etc., and you've lost control or can't keep track of what's changing when. Rather than sprinkling those changes throughout your code, centralize them into methods like resetShots(), resetEnemies(), incrementShots(), incrementEnemies(), etc. that are called only when needed and do exactly as required to keep the game state under control.

October 14th, 2013, 11:16 AM

Silvanoshei

Re: Game w/ If Statements

Quote:

Originally Posted by GregBrannon

It's not clear how your project is set up, but it appears you have multiple opportunities to change important game state variables, like stage, enemies, shots, etc., and you've lost control or can't keep track of what's changing when. Rather than sprinkling those changes throughout your code, centralize them into methods like resetShots(), resetEnemies(), incrementShots(), incrementEnemies(), etc. that are called only when needed and do exactly as required to keep the game state under control.

I'm going to add a instance variable to help me keep track. If you use the shoot() method, and it produces a result, enemiesUp... how would you mathematically do that? Cause, enemiesUp = enemiesUp - enemies... lets call this new variable previousShot, you don't want to say previousShot = enemiesUp, that's just redundant. Is there a class I'm not realizing could help me? Like previousShot = getlastCall.shoot()? lol /:)

October 14th, 2013, 01:53 PM

GregBrannon

Re: Game w/ If Statements

It's hard to say without seeing more code. I have no idea what classes you have or don't have. My initial impression was that you've built the whole thing in one class run mostly from the main() method. Hopefully, you're actually beyond that, but who can tell?

Each stage, weapon, enemy, bullet, etc. should probably have its own class and draw itself and keep track of itself and all of its resources. Beyond that, I'm not sure what to tell ya.