www.webdeveloper.com
Results 1 to 12 of 12

Thread: javascript character simulator

  1. #1
    Join Date
    Sep 2009
    Posts
    7

    javascript character simulator

    I need help with this character simulator.
    Here's the code i'm working with. I can't seem to get it to start working
    Code:
    <html>
    <title>Character Simulator</title>
    <head>
    <SCRIPT LANGUAGE="javascript" type="text/javascript">
    Function Simulate_Char(){
    Var Vit_Base = 5
    Var Int_Base = 6
    Var Dex_Base = 10
    Var Str_Base = 4
    Var Race1 = "Wood Elf"
    }
    If (race.Value == 1){
    Var Vit_Base = 6
    Var Str_Base = 7
    Var Int_Base = 6
    Var Dex_Base = 6
    Var Race1 = "Human"
    }
    If (Class.Value == 1){
    Var Dex_add = 9
    Var Str_add = 3
    Var Vit_add = -2
    Var Int_add = 0
    Var HP_Vit = 12
    Var MP_Int = 10
    Var Ddg_Dex = 7
    Var Acc_Dex = 10
    Var Def_Vit = 3
    Var Dmg_Dex = 3
    Var Dmg_Int = 0
    Var Dmg_Str = 0.5
    Var Crit_Dex = 0.02
    Var Crit_Str = 0
    Var Crit_Int = 0
    Var Base_Crit= 5
    Var Base_HP = 36
    Var Base_MP = 30
    Var Class1 = "Archer"
    }
    Var HP = (Base_HP+((Vit_Base+Vit_add+Vit)*HP_Vit)+((HP_Vit*2)*level))
    Var MP = (Base_MP+((Int_Base+Int_add+Int)*MP_Int)+((MP_Int*2)*level))
    Var Acc = ((Acc_Dex*3)+((Dex_Base+Dex_add+Dex+Dex)*Acc_Dex)+((Acc_Dex*1.2)*level))
    Var Def = ((Def_Vit*2)+((Vit_Base+Vit_add+Vit)*Def_Vit)+((Vit_Def*1.1)*level))
    Var Dmg = ((Dmg_Dex*2)+(Dmg_Str*2)+(Dmg_Int*2)+((Int_add+Int_Base+Int)* 
    (Dmg_Int))+((Dex_add+Dex_Base+Dex)*(Dmg_Dex))+((Str_add+Str_Base)* 
    (Dmg_Str))+(((Dmg_Str+Dmg_Int+Dmg_Dex)*1.1)*level))
    Var Ddg = ((Dex_Base+Dex_add+Dex)*Ddg_Dex)+(Ddg_Dex*3)+((Ddg_Dex*1.1)*level))
    Var Crit = (((Dex_Base+Dex_add+Dex)*Crit_Dex)+((Int_Base+Int_add+Int) 
    *Crit_Int)+((Str_add+Str_Base+Str)*Crit_Str))
    document.texts.sim.value= 'Race:'
    +Race1.value
    +'level:'
    +level.value
    +'Class:'
    +Class1.value 
    +'/n'
    +'HP:'
    +HP.value 
    +'/n'
    +'MP:'
    +MP.value
    +'Accuracy:'
    +Acc.value
    +'/n'
    +'Dodge:'
    +Ddg.value
    +'/n'
    +'Damage:'
    +Dmg.value
    +'/n'
    +'Critical Hit Chance:'
    +Crit.value
    +'/n'
    }
    </script>
    </head>
    <body>
    <FORM ACTION="#">
    Race:
    <SELECT NAME="race">
    <option selected>race</option>
    <option Value= '1'>Human</option>
    </SELECT>
    </FORM>
    <FORM ACTION="#">
    Class:
    <SELECT NAME="Class">
    <option selected>Class</option>
    <option Value= '1'>Archer</option>
    </select>
    </form>
    <FORM ACTION="#" NAME="stats">
    level:
    <INPUT TYPE=TEXT NAME="level" SIZE=3>
    Dex:
    <INPUT TYPE=TEXT NAME="Dex" SIZE=3>
    Str:
    <INPUT TYPE=TEXT NAME="Str" SIZE=3>
    Int:
    <INPUT TYPE=TEXT NAME="Int" SIZE=3>
    Vit:
    <INPUT TYPE=TEXT NAME="vit" SIZE=3>
    </form>
    <BR><BR>
    <INPUT type="button" Value="Simulate Character" name="button1"  onClick="Simulate_Char();">
    <BR><BR>
    <TEXTAREA NAME="sim" COL=30 ROW=100></TEXTAREA>
    </body>
    </html>
    I've been working on this thing for the past 2 days but i can't seem to find anything wrong with it. And also neither can my friend who i had look over it who's also a pretty good javascript coder. I say i'm pretty good since i can do somethings but if anyone can help me fix this so that it'd work that'd be GREATLY appreciated.

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,443

    Question Consider this ...

    What is it supposed to do?

    Couple of changes recommended:

    1. Remove multiple <FORM> tags
    2. JS is case sensitive, change Var to var
    3. Put in some alerts to test what's what at critical places in code.
    4. Add some comments about what it is supposed to do!

    Good Luck!

  3. #3
    Join Date
    Sep 2009
    Posts
    7
    Ok i just updated teh code. And have found that the problem lies in teh button click.

  4. #4
    Join Date
    Sep 2009
    Posts
    7
    Code:
    <html>
    <title>Character Simulator</title>
    <head>
    <SCRIPT LANGUAGE="javascript" type="text/javascript">
    <!--this is supposed to be the primary function in the program -->
    Function Simulate_Char(){
    <!-- This is where it checks on the persons' input -->
    <!-- It also sets the various variables that will be used later -->
    If (race.Value == 1){
    var Vit_Base = 6
    var Str_Base = 7
    var Int_Base = 6
    var Dex_Base = 6
    var Race1 = "Human"
    }
    alert(Race1.value)
    If (Class.Value == 1){
    var Dex_add = 9
    var Str_add = 3
    var Vit_add = -2
    var Int_add = 0
    var HP_Vit = 12
    var MP_Int = 10
    var Ddg_Dex = 7
    var Acc_Dex = 10
    var Def_Vit = 3
    var Dmg_Dex = 3
    var Dmg_Int = 0
    var Dmg_Str = 0.5
    var Crit_Dex = 0.02
    var Crit_Str = 0
    var Crit_Int = 0
    var Base_Crit= 5
    var Base_HP = 36
    var Base_MP = 30
    var Class1 = "Archer"
    }
    alert(Class1.value)
    <!-- This is the beast of it all. This is where it's supposed to do all the number crunching 
    based upon what the user has inputted earlier.->
    var HP = (Base_HP+((Vit_Base+Vit_add+Vit)*HP_Vit)+((HP_Vit*2)*level))
    var MP = (Base_MP+((Int_Base+Int_add+Int)*MP_Int)+((MP_Int*2)*level))
    var Acc = ((Acc_Dex*3)+((Dex_Base+Dex_add+Dex+Dex)*Acc_Dex)+((Acc_Dex*1.2)*level))
    var Def = ((Def_Vit*2)+((Vit_Base+Vit_add+Vit)*Def_Vit)+((Vit_Def*1.1)*level))
    var Dmg = ((Dmg_Dex*2)+(Dmg_Str*2)+(Dmg_Int*2)+((Int_add+Int_Base+Int)* 
    (Dmg_Int))+((Dex_add+Dex_Base+Dex)*(Dmg_Dex))+((Str_add+Str_Base)* 
    (Dmg_Str))+(((Dmg_Str+Dmg_Int+Dmg_Dex)*1.1)*level))
    var Ddg = ((Dex_Base+Dex_add+Dex)*Ddg_Dex)+(Ddg_Dex*3)+((Ddg_Dex*1.1)*level))
    var Crit = (((Dex_Base+Dex_add+Dex)*Crit_Dex)+((Int_Base+Int_add+Int) 
    *Crit_Int)+((Str_add+Str_Base+Str)*Crit_Str))
    <!-- This is where the various values that we've recievd earlier 
    will be written into the text area below. -->
    document.texts.sim.value= 'Race:'
    +Race1.value
    +'level:'
    +level.value
    +'Class:'
    +Class1.value 
    +'/n'
    +'HP:'
    +HP.value 
    +'/n'
    +'MP:'
    +MP.value
    +'Accuracy:'
    +Acc.value
    +'/n'
    +'Dodge:'
    +Ddg.value
    +'/n'
    +'Damage:'
    +Dmg.value
    +'/n'
    +'Critical Hit Chance:'
    +Crit.value
    +'/n'
    }
    </script>
    </head>
    <body>
    <!-- This is where all the input from the players is taken.-->
    <!-- This is where we gather the value for Race from a drop down menulist.-->
    Race:
    <SELECT NAME="race">
    <option selected>race</option>
    <option Value= '1'>Human</option>
    </SELECT>
    <!-- This is where the input for the class goes.-->
    <br>
    Class:
    <SELECT NAME="Class">
    <option selected>Class</option>
    <option Value= '1'>Archer</option>
    </select>
    <!-- This is where we gather up all the various "stats" from players using text input fields.-->
    level:
    <INPUT TYPE=TEXT NAME="level" SIZE=3>
    Dex:
    <INPUT TYPE=TEXT NAME="Dex" SIZE=3>
    Str:
    <INPUT TYPE=TEXT NAME="Str" SIZE=3>
    Int:
    <INPUT TYPE=TEXT NAME="Int" SIZE=3>
    Vit:
    <INPUT TYPE=TEXT NAME="vit" SIZE=3>
    <BR><BR>
    <!-- This is the big problem i believe. I don't believe on the button click 
    that the function is being called.-->
    <INPUT type="button" Value="Simulate Character" name="button1"  onClick="Alert("I worked")">
    <BR><BR>
    <TEXTAREA NAME="sim" COL=30 ROW=100></TEXTAREA>
    </body>
    </html>
    i can't seem to find an edit button anywhere.... so sorry for the multi-response.

  5. #5
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,443
    You need to review the syntax of JS.

    There is no 'Function', only 'function'
    There is no 'If', only 'if'
    There is no 'Alert()', only 'alert()'

    You can get away with mixed case in HTML, but not JS.

    Also comments in JS are either
    // for a single line
    /*
    or for
    multilines
    use
    */
    The <!-- HTML comments --> may not always work
    Last edited by JMRKER; 09-05-2009 at 11:22 PM.

  6. #6
    Join Date
    Sep 2009
    Posts
    7
    ok i used notepad++ and did a quick replace i'll try all the things you said. Not used to have to watch my casing w/o it being auto-replaced as notepad++ didn't tell me it wasn't right.

  7. #7
    Join Date
    Sep 2009
    Posts
    7
    Ok now i'm having a problem with getting it to have more than 1 form that's named.
    what i did was i went back in there and made it so that it read the index value(seems that was what i was missing all along). Then i tried to make it read the index value from 2 seperate drop down menus in the same form. That didn't work. I tried 2 seperate forms. That too didn't work. I get "not defined" when i set it to alert each step now. When i'm trying to read from the second one. The first reads just fine.

  8. #8
    Join Date
    Sep 2009
    Posts
    3
    I've taken the syntax errors out but without knowing what its supposed to do who knows if you can believe the alert on the click, cause nothing sensible happens? You need to describe the desired effect and whats not happening.


    HTML Code:
    <html>
    <title>Character Simulator</title>
    <head>
    <SCRIPT LANGUAGE="javascript" type="text/javascript">
    function Simulate_Char(){
        if (race.Value == 1){
            var Vit_Base = 6;
            var Str_Base = 7;
            var Int_Base = 6;
            var Dex_Base = 6;
            var Race1 = "Human";
        }
        alert(Race1.value);
        if (Class.Value == 1){
            var Dex_add = 9;
            var Str_add = 3;
            var Vit_add = -2;
            var Int_add = 0;
            var HP_Vit = 12;
            var MP_Int = 10;
            var Ddg_Dex = 7;
            var Acc_Dex = 10;
            var Def_Vit = 3;
            var Dmg_Dex = 3;
            var Dmg_Int = 0;
            var Dmg_Str = 0.5;
            var Crit_Dex = 0.02;
            var Crit_Str = 0;
            var Crit_Int = 0;
            var Base_Crit= 5;
            var Base_HP = 36;
            var Base_MP = 30;
            var Class1 = "Archer";
        }
        alert(Class1.value);
        var HP = (Base_HP+((Vit_Base+Vit_add+Vit)*HP_Vit)+((HP_Vit*2)*level));
        var MP = (Base_MP+((Int_Base+Int_add+Int)*MP_Int)+((MP_Int*2)*level));
        var Acc = ((Acc_Dex*3)+((Dex_Base+Dex_add+Dex+Dex)*Acc_Dex)+((Acc_Dex*1.2)*level));
        var Def = ((Def_Vit*2)+((Vit_Base+Vit_add+Vit)*Def_Vit)+((Vit_Def*1.1)*level));
        var Dmg = ((Dmg_Dex*2)+(Dmg_Str*2)+(Dmg_Int*2)+((Int_add+Int_Base+Int)* 
        (Dmg_Int))+((Dex_add+Dex_Base+Dex)*(Dmg_Dex))+((Str_add+Str_Base)* 
        (Dmg_Str))+(((Dmg_Str+Dmg_Int+Dmg_Dex)*1.1)*level));
        var Ddg = ((Dex_Base+Dex_add+Dex)*Ddg_Dex)+(Ddg_Dex*3)+((Ddg_Dex*1.1)*level);
        var Crit = (((Dex_Base+Dex_add+Dex)*Crit_Dex)+((Int_Base+Int_add+Int) 
        *Crit_Int)+((Str_add+Str_Base+Str)*Crit_Str));
        document.texts.sim.value= 'Race:'
                                    +Race1.value
                                    +'level:'
                                    +level.value
                                    +'Class:'
                                    +Class1.value 
                                    +'/n'
                                    +'HP:'
                                    +HP.value 
                                    +'/n'
                                    +'MP:'
                                    +MP.value
                                    +'Accuracy:'
                                    +Acc.value
                                    +'/n'
                                    +'Dodge:'
                                    +Ddg.value
                                    +'/n'
                                    +'Damage:'
                                    +Dmg.value
                                    +'/n'
                                    +'Critical Hit Chance:'
                                    +Crit.value
                                    +'/n';
    }
    </script>
    </head>
    <body>
    <!-- This is where all the input from the players is taken.-->
    <!-- This is where we gather the value for Race from a drop down menulist.-->
    Race:
    <SELECT NAME="race">
    <option selected>race</option>
    <option Value= '1'>Human</option>
    </SELECT>
    <!-- This is where the input for the class goes.-->
    <br>
    Class:
    <SELECT NAME="Class">
    <option selected>Class</option>
    <option Value= '1'>Archer</option>
    </select>
    <!-- This is where we gather up all the various "stats" from players using text input fields.-->
    level:
    <INPUT TYPE=TEXT NAME="level" SIZE=3>
    Dex:
    <INPUT TYPE=TEXT NAME="Dex" SIZE=3>
    Str:
    <INPUT TYPE=TEXT NAME="Str" SIZE=3>
    Int:
    <INPUT TYPE=TEXT NAME="Int" SIZE=3>
    Vit:
    <INPUT TYPE=TEXT NAME="vit" SIZE=3>
    <BR><BR>
    <!-- This is the big problem i believe. I don't believe on the button click 
    that the function is being called.-->
    <INPUT type="button" Value="Simulate Character" name="button1"  onClick="alert('I worked')">
    <BR><BR>
    <TEXTAREA NAME="sim" COL=30 ROW=100></TEXTAREA>
    </body>
    </html>

  9. #9
    Join Date
    Sep 2009
    Posts
    7
    I've already said that the desired outcome is. I'm wanting it to do the math i've set it up to do then write the outcomes to a text area also defined in the document. also i think javascript's got a set number of "if"s it can have. As EVERYTIME i try to do more than 2 ifs. My code never works. I don't get it and it's killing me.

  10. #10
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,443

    Exclamation

    You still have a number of syntax errors:

    1. You cannot use a variable in an equation until you define it.
    If the IF statement is not true, then none of you 'var' statements get set
    Then undefined values are used in the equation.

    2. You don't have a <form> tag so you will not be able to determine
    the values of document.race.value or document.Class.value at they are invalid

    3. In your equations, you are using variable that have not ever been defined, see:
    Vit, Int, Dex, etc.

    4. You cannot set the value of the <TEXTAREA> in the way you are trying. See #2 above.

    I would suggest using the error console of FF to see the errors. There may be more, I just quit looking.

    And, no, you have not "already said that the desired outcome is."
    If you had, we would not be questioning what it is that you're trying to do! Drop the attitude if you want to continue to be helped.

    If you make certain entries in the textboxes and choose certain values
    from the drop down selections, what should the output be?

  11. #11
    Join Date
    Sep 2009
    Posts
    7
    I thought by naming all the input text boxes with names such as "vit" etc. would set the values oh well I guess that's pointless then.... I'm done with javascript then. I'll never code in this language again.

  12. #12
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,443

    resolved

    Quote Originally Posted by 133794m3r View Post
    I'm done with javascript then. I'll never code in this language again.
    Based upon your previous postings, I don't think you programmed in this language yet.

    Hope that's not the same attitude you have for other languages.

    Anyway, good luck ... you may need it!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles