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.

DarkBASIC Professional Discussion / "If" statement touble

Author
Message
midniteferret
15
Years of Service
User Offline
Joined: 14th Oct 2010
Location: where ever you are not
Posted: 16th Oct 2010 23:12
hi guys,
I've been trying to make a lightsaber simulator in darkbasic.
however, when I run it, it repeats several print statements and then doesn't let me access any commands that I programmed in. I think this is because of the If statements that I used?
here is the code

please help, thanks!

-Sharpie, not even once...
gbark
20
Years of Service
User Offline
Joined: 14th Oct 2005
Location: US - Virginia
Posted: 17th Oct 2010 01:29 Edited at: 17th Oct 2010 01:32
This is one example where I think the BASIC syntax actually makes reading code more complicated than with a C style syntax.
But basically, the way it works in DBPro:

When you follow an IF statement with a THEN statement, then only the commands on that line of code are associated with the IF statement - the following lines of code are considered "outside of" the IF block. Conversely, when you DON'T follow an IF with a THEN statement, then the IF block continues until you explicitly use an ENDIF command.

So, where you have


Then the only parts that are associated with the IF statements are the commands to play or stop the sounds. The PRINT statements will execute regardless, because they are on different lines than the IF statements. There are two ways to correct this:

1) Use a colon (":") to allow both the print and sound statements to share the same line as the IF statement, e.g.,


2) Remove the THEN statement, split each command so they're on separate lines, and end your block with an ENDIF, e.g.,


Personally I prefer the second method, but of course it's a matter of opinion and up to you. Hope this helps.
midniteferret
15
Years of Service
User Offline
Joined: 14th Oct 2010
Location: where ever you are not
Posted: 17th Oct 2010 04:37
Great, I'll try it out

-Sharpie, not even once...
midniteferret
15
Years of Service
User Offline
Joined: 14th Oct 2010
Location: where ever you are not
Posted: 17th Oct 2010 04:57
sorry to bother you once more but how do you keep text from repeating its self even when the key is held

-Sharpie, not even once...
gbark
20
Years of Service
User Offline
Joined: 14th Oct 2005
Location: US - Virginia
Posted: 17th Oct 2010 19:56
Well, you basically want to make it so that your game only responds to the press during the very first loop cycle that the press was detected (rather than every loop, which causes your text to repeat). To keep track of this, you'll need a second flag that basically stores the state of the press during the previous cycle.

So as you can see, if you determine that you currently have a press, and your previous cycle did not have a press, then you have detected the first game cycle of the press, and can now respond accordingly.

That's just so you get a feel for the concept, the code for such a thing is pretty simple.

Login to post a reply

Server time is: 2026-07-22 12:41:38
Your offset time is: 2026-07-22 12:41:38