Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    May 2011
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Passing arguments

    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
    Code:
    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...
    Code:
    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

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,650
    Thanks
    80
    Thanked 4,638 Times in 4,600 Posts
    Code:
    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.


    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,650
    Thanks
    80
    Thanked 4,638 Times in 4,600 Posts
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New Coder
    Join Date
    May 2011
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    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
    Last edited by nuBee; 12-12-2011 at 11:32 PM.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,650
    Thanks
    80
    Thanked 4,638 Times in 4,600 Posts
    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 [b]n[/] (yes, the syntax shown is necessary) and in each 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?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    New Coder
    Join Date
    May 2011
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    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 [b]n[/] (yes, the syntax shown is necessary) and in each 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:
    Code:
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •