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.

Dark GDK / Switch Statement

Author
Message
ketchup mustard
16
Years of Service
User Offline
Joined: 31st Jan 2008
Location:
Posted: 2nd Jul 2010 00:35
How do I implement a switch statement in Dark GDK? I tried building it but I received a whole lot of errors.

Don't Clap, Just Throw Money!
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 2nd Jul 2010 00:56
it has nothing to do with darkGDK, it is the language, you are probably using it in a wrong way, here's how you do it:



braces after cases allows you to define local variables, don't forget the break so it doesn't jump to the next case

TechLord
21
Years of Service
User Offline
Joined: 19th Dec 2002
Location: TheGameDevStore.com
Posted: 2nd Jul 2010 19:20 Edited at: 2nd Jul 2010 19:21
GO HERE AS FAST AS YOU CAN!!!! --->http://www.cplusplus.com/doc/tutorial/.
Visit Control Structures, and every other section for that matter.

ketchup mustard
16
Years of Service
User Offline
Joined: 31st Jan 2008
Location:
Posted: 13th Jul 2010 04:57


above, its telling me that it does not recognize the switch statement...

here's the code outside of the main.cpp:



Don't Clap, Just Throw Money!
Freedom Fighters
15
Years of Service
User Offline
Joined: 2nd Nov 2009
Location:
Posted: 13th Jul 2010 07:07
..........you can't have switch case's out side of a functions. it make's no sense to the compiler.
he say's : "hay, im confused, there a switch case that doesn't belong to anything"

essentially, you can have switch case out side a function. the switch casing is testing a statement.

Problem Solution That Never Fails: "Build A Bridge And Get Over It"
SunDawg
19
Years of Service
User Offline
Joined: 21st Dec 2004
Location: Massachusetts
Posted: 13th Jul 2010 07:11
I might also note that you put a colon after case rather than the value. It should look like this:
ketchup mustard
16
Years of Service
User Offline
Joined: 31st Jan 2008
Location:
Posted: 16th Jul 2010 21:36
here is my new errorless code:


My only problem this moment is that I would like to name my cases; ( i.e. case Under: )

I noticed in the Dark Invaders source code, he uses enum to call to his switch function case names...? I tried that but it didnt work : (

Don't Clap, Just Throw Money!
Greg_C
14
Years of Service
User Offline
Joined: 22nd May 2010
Location:
Posted: 16th Jul 2010 22:28
That's not to hard there are a few ways you could do that. a simple way is declare four integers that you want the names of the case to be. It would look something like this.



Now when you want to switch to a different case you just equal Collision to whichever case you want it to be in like so:



Now say you start getting more cases later on, it would be easier to make an enumeration like in the dark invaders game. So it might look something like this:



Just like the example before when you want to change cases you just equal Collision to whatever enumeration name you specified like so:



Hope this helps.
SunDawg
19
Years of Service
User Offline
Joined: 21st Dec 2004
Location: Massachusetts
Posted: 17th Jul 2010 21:12
If your cases for a switch are variable I don't think it will compile, you have to use an enumerated type to explain it.
Bran flakes91093
16
Years of Service
User Offline
Joined: 13th Sep 2008
Location: Crazy Land
Posted: 18th Jul 2010 02:54 Edited at: 18th Jul 2010 02:55
Quote: "If your cases for a switch are variable I don't think it will compile, you have to use an enumerated type to explain it."


It will work if the variables are declared as const.

const int Under = 1;
...
case Under:
break;
...

“C++ : Where friends have access to your private members.”
-Gavin Russell Baker

Login to post a reply

Server time is: 2024-11-19 20:49:08
Your offset time is: 2024-11-19 20:49:08