...

View Full Version : Passing arguments



nuBee
12-12-2011, 07:52 PM
Should be simple, but I'm having a complete brain fart. I need to call a function, with a parameter that triggers something relative to that parameter.

Pseudo Example

function respawn(n) {
case 1
//respawn monster1 code
case 2
//respawn monster 2 code
case 3
//respawn monster 3 code
}

if(killed monster1) {respawn(1)} //respawn(monster1) is what I want to do
if(killed monster2) {respawn(2)}//respawn(monster2) is what I want to do
if(killed monster3) {respawn(3)}//respawn(monster3) is what I want to do



I can...

function respawn1(){
//respawn monster1 code
}
function respawn2() {
//respawn monster2 code
}
function respawn3() {
//respawn monster3 code
}
if(killed monster1) {respawn1()}
if(killed monster2) {respawn2()}
if(killed monster3) {respawn2()}


but if I have 10+ monsters I just figured a case structure would be good, or passing the monster to respawn to a single respawn() function......but all the switch examples I could find were based on dateTime which gave case 1-7 based on what day it was, and didn't show how you set your own cases like monster1, monster2, monster3....

I know this is a basic concept...I've read about functions here: http://www.quirksmode.org/js/function.html and at w3schools of course but can't figure out how to apply it to my situation :(

Thanks

Old Pedant
12-12-2011, 10:21 PM
function respawn(n) {
switch (n) {
case 1:
//respawn monster1 code
break; // don't forget this!
case 2:
//respawn monster 2 code
break;
case 3:
//respawn monster 3 code
break;
default:
... you will get here if n is not 1,2, or 3
}
}

The colons after the cases *ARE* required. The break statements are required unless you want the code from (say) case 1 to "fall through" to case 2.


}

Old Pedant
12-12-2011, 10:22 PM
Oh...and look here:
http://www.w3schools.com/js/

and especially here:
http://www.w3schools.com/js/js_switch.asp

nuBee
12-12-2011, 11:30 PM
Oh...and look here:
http://www.w3schools.com/js/

and especially here:
http://www.w3schools.com/js/js_switch.asp

Yep those are the scenario's I was mentioning in the original post that I read before posting....those automatically get the different cases from the day of the week...how would I trigger case 4 on day 2 though?

How do I send case1, case2, case3, etc to the switch? From my function that triggers the function with the switch in it?

Like, "n" in my case is monster1, monster2, monster3, monster(n)....

so when I call respawn(n) what should I reference "n" as?? 1, 2, 3, etc???

if(monster1 killed) {respawn(1)}
if(monster2 killed) {respawn(2)}
???

or
if(monster1 killed) {respawn(????)}

Currently I made respawnMonster1(), respawnMonster2(), etc then

if(monster1 killed){respawnMonster1();}
if(monster2 killed){respawnMonster2();}
etc

It works but I was hoping to a) clean it up and b) learn how to pass the different cases into the switch :)

Old Pedant
12-13-2011, 01:21 AM
Ummm...I just don't know how to make it clearer.

You pass a number to the function. The function receives it as the variable n.

You then create a switch that is based on n[/] (yes, the syntax shown is necessary) and in each [b]case of the switch you can do something different.

Without knowing what your spawn code does, I don't know what else I can add.

Maybe you need to just try some sample programming and see where you get to?

nuBee
12-13-2011, 01:55 AM
Ummm...I just don't know how to make it clearer.

You pass a number to the function. The function receives it as the variable n.

You then create a switch that is based on n[/] (yes, the syntax shown is necessary) and in each [b]case of the switch you can do something different.

Without knowing what your spawn code does, I don't know what else I can add.

Maybe you need to just try some sample programming and see where you get to?

OK it must be the way I'm trying to do it then. I just needed clarification if you just simply did spawn(1); because it doesn't work along with the other 100 ways I tried it.

Actually copying and pasting:

function respawn(n) {
switch (n) {
case 1:
//respawn monster1 code
break; // don't forget this!
case 2:
//respawn monster 2 code
break;
case 3:
//respawn monster 3 code
break;
default:

}
}

Kills it completely. By itself. Before even trying to call it.

I'll just stick to the spawnMonster1(){}, spawnMonster2(){}, etc now that I know the correct switch case structure doesn't work.

Thanks for the clarification.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum