www.webdeveloper.com
Results 1 to 7 of 7

Thread: Noob question about a code

  1. #1
    Join Date
    Feb 2014
    Posts
    4

    Noob question about a code

    Here's the thing, guys, I'm just starting to learn javascript, I mean, I've learnt the basics, but nothing too deep, and I want to make a small game just to train my skills. here is the javascript code for it:
    P.S. start must equal true to start it, just change the value, actually I wanted to make a button so I could start it, but I don't know how to do it just yet. My problem here is: it works just to a certain point, after that it doesn't run anymore, I can't see why. Can someone help me?
    Another problem, I was using console.log to print what I wanted, but I realized it doesn't work so I changed it all to prompt, don't know if there's something better. btw, there's a lot to work, that's just the beginning, but I want to understand why it is not working.
    Code:
    var start=false;
    var char1;
    function char(name,classe,gender,str,vit,int,skill1,skill2,skill3,skill4,skill5){
        this.name=name;
        this.level=1;
        this.classe=classe;
        this.gender=gender;
        this.str=str;
        this.vit=vit;
        this.int=vit;
        this.hp=hp;
        hp=vit*10;
        this.skill1=skill1;
        skill1.name=null;
        this.skill2=skill2;
        skill2.name=null;
        this.skill3=skill3;
        skill3.name=null;
        this.skill4=skill4;
        skill4.name=null;
        this.skill5=skill5;
        skill5.name=null;
    }
    function charinv(weapon,shirt,legs,dress,helmet,shoes){
        this.weapon=weapon;
        this.shirt=shirt;
        this.dress=dress;
        this.legs=legs;
        this.helmet=helmet;
        this.shoes=shoes;
    }
    function charbag(gold,itemslot1,itemslot2,itemslot3,itemslot4,itemslot5,itemslot6,itemslot7,itemslot8){
        this.gold=gold;
        this.itemslot1=itemslot1;
        this.itemslot2=itemslot2;
        this.itemslot3=itemslot3;
        this.itemslot4=itemslot4;
        this.itemslot5=itemslot5;
        this.itemslot6=itemslot6;
        this.itemslot7=itemslot7;
        this.itemslot8=itemslot8;
        
    }
    function mob(name,level,str,vit,int,gold,item,mobskill1,mobskill2,mobskill3){
        this.name=name;
        this.level=level;
        this.str=str;
        this.vit=vit;
        this.int=int;
        this.gold=gold;
        this.golddrop=gold*Math.random();
        this.item=item;
        this.itemdrop=item*Math.random();
        this.hp=hp;
        hp=vit*10;
        this.mobskill1=mobskill1;
        mobskill1.name=null;
        this.mobskill2=mobskill2;
        mobskill2.name=null;
        this.mobskill3=mobskill3;
        mobskill3.name=null;
    }
    function item(id,name,lvlreq,str,vit,int){
        this.id=id;
        this.name=name;
        this.lvlreq=lvlreq;
        this.str=str;
        this.vit=vit;
        this.int=int;
    }
    function fight(mobfunction){
        var answerfight=prompt("Would you like to fight "+mobfunction.name+"?Type yes or no.").toUpperCase;
        for(;answerfight!="YES"&&answerfight!="NO";){
            console.log("Invalid answer.");
            answerfight=prompt("Would you like to fight "+mobfunction.name+"?Type yes or no").toUpperCase;
        var escapeanswer=false;
        if(answerfight=="NO"){
            var escapechance=char1.level-mobfunction.level+2*Math.random()*100;
            if(escapechance>=100){
                escapeanswer=true;
            }
            }
            if(escapeanswer){
                console.log("You ran from the monster.");
            }else{
                if(answerfight=="NO"){
                    console.log("You couldn't run from the monster.");
                }
                for(;mobfunction.hp<=0||char1.hp<=0;){
                    console.log("It's your turn.");
                    var charturn=prompt("What would you like to do?Use "+char1[skill1].name+", "+char1[skill2].name+", "+char1[skill3].name+", "+char1[skill4].name+", "+char1[skill5].name+" or hit?");
                    switch(charturn){
                        case 'char1.skill1':
                            break;
                        case 'char1.skill2':
                            break;
                        case 'char1.skill3':
                            break;
                        case 'char1.skill4':
                            break;
                        case 'char1.skill5':
                            break;
                        case 'hit':
                            break;
                    }
                    if(charturn!=char1[skill1].name&&charturn!=char1[skill2].name&&charturn!=char1[skill3].name&&charturn!=char1[skill4].name&&charturn!=char1[skill5].name&&charturn!="hit"){
                        
                    }
                }
            }
        }
    }
    if(start){
          //insert story here...
    var charname=prompt("What is your character's name?");
    var answer1=prompt("There are 3 main classes: Warrior, Archer and Mage.");
    prompt("Please type the name of the class you want.").toUpperCase();
    for(; charclasse!="WARRIOR"&&charclasse!="ARCHER"&&charclasses!="MAGE";){
        prompt("Invalid class.");
    charclasse=prompt("Please type the name of the class you want.").toUpperCase();
    }
    var charstr;
    var charvit;
    var charint;
    switch(charclasse){
        case 'WARRIOR':
            charstr=8;
            charvit=10;
            charint=3;
            break;
        case 'ARCHER':
            charstr=10;
            charvit=6;
            charint=5;
            break;
        case 'MAGE':
            charstr=5;
            charvit=6;
            charint=10;
            break;
    }
    var chargender=prompt("Are you male or female?").toUpperCase();
    for(; chargender!="MALE"&&chargender!="FEMALE";){
        console.log("Invalid gender.");
        chargender=prompt("Are you male or female?").toUpperCase();
    }
    char1=new char(charname,charclasse,chargender,charstr,charvit,charint);
    //insert more story here...
    prompt("Okay, now let`s decide the features of your character");
    var chardress;
    var charshirt;
    var charlegs;
    var charshoes;
    var charhair;
    if(char1.gender=="FEMALE"){
        //female char
        //show the char with the 3 possible hairs
        charhair=prompt("Do you want your hair to be:black,blonde or red?").toUpperCase();
    for(;hair!="BLACK"&&hair!="BLONDE"&&hair!="RED";){
        console.log("Invalid hair color.");
        hair=prompt("Do you want your hair to be:black,blonde or red?").toUpperCase();
        //show the char with the 3 possible dresses
        chardress=prompt("Choose one of the dresses and type 1,2 or 3.");
        for(;chardress!=1&&chardress!=2&&chardress!=3;){
            console.log("Invalid choice.");
                chardress=prompt("Choose one of the dresses and type 1,2 or 3.");
        }
        //show the char with 3 possible shoes
        charshoes=prompt("Choose one of the three shoes and type 1,2 or 3");
        for(;charshoes!=1&&charshoes!=2&&charshoes!=3;){
            console.log("Invalid choice.");
            charshoes=prompt("Choose one of the three shoes and type 1,2 or 3");
        }
    }
    }else{
            //show the char with the 3 possible hairs
        charhair=prompt("Do you want your hair to be:black,blonde or red?").toUpperCase();
    for(;hair!="BLACK"&&hair!="BLONDE"&&hair!="RED";){
        console.log("Invalid hair color.");
        hair=prompt("Do you want your hair to be:black,blonde or red?").toUpperCase();
    }
        //show the char with 3 possible shirts
        charshirt=prompt("Choose one of the shirts and type 1,2 or 3");
        for(;charshirt!=1&&charshirt!=2&&charshirt!=3;){
            console.log("Invalid choice.");
            charshirt=prompt("Choose one of the shirts and type 1,2 or 3");
        }
        //show the char with 3 possible leggings
        charlegs=prompt("Choose one of the leggings and type 1,2 or 3");
        for(;charlegs!=1&&charlegs!=2&&charlegs!=3;){
            console.log("Invalid choice.");
            charlegs=prompt("Choose one of the leggings and type 1,2 or 3");
        }
        charshoes=prompt("Choose one of the three shoes and type 1,2 or 3");
        for(;charshoes!=1&&charshoes!=2&&charshoes!=3;){
            console.log("Invalid choice.");
            charshoes=prompt("Choose one of the three shoes and type 1,2 or 3");
        }
        chardress=char1.chardress;
        charshirt=char1.charshirt;
        charlegs=char1.charlegs;
        charhair=char1.charhair;
        charshoes=char1.charshoes;
        //tell some story here...
        //show the map
        var question1=prompt("What do you want to do next? Go north, go south, go east, go west or talk to that guy?");
        for(;question1!="go south"&&question1!="go north"&&question1!="go east"&&question1!="go west"&&question1!="talk to that guy";){
            console.log("Invalid choice.");
            question1=prompt("What do you want to do next? Go north, go south, go east, go west or talk to that guy?");
        }
            switch(question1){
            case 'go north':
                break;
            case 'go south':
                break;
            case 'go east':
                break;
            case 'go west':
                break;
            case 'talk to that guy':
                break;
        }
    }
    }

  2. #2
    Join Date
    Jan 2014
    Location
    Boston, Massachusetts
    Posts
    66
    What is the last prompt that you see when you run it?

  3. #3
    Join Date
    Feb 2014
    Posts
    4
    this prompt is the last one I see:
    Code:
    prompt("Please type the name of the class you want.").toUpperCase();
    after that I tried to create a for loop to stop someone from typing an invalid answer, but it seems like it didn't work out.

  4. #4
    Join Date
    Jan 2014
    Posts
    28
    Code:
    if(start){
    Right above choosing your class.

    If the loop runs while start is true, It will continue to run. You need to use something else instead of start unless you set start to false inside that loop somewhere.

  5. #5
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    P.S. start must equal true to start it, just change the value, actually I wanted to make a button so I could start it, but I don't know how to do it just yet
    If I don't modify your code (and I ain't gonna do that), this is one way to accomplish it. Add this to the body section of your web document:

    HTML Code:
    <button onclick="startGame()">Start</button>
    then create one more function and name it startGame. Putt all of the code from if (start) (including if statement) to the last closing curly braces inside of it. This goes at the top of the function:

    [code]
    start = true;
    [code]


    I was using console.log to print what I wanted, but I realized it doesn't work...
    This depends on the browser you're using. I'm using google-chrome. In google-chrome you can right-click the web page and choose inspect element. When the small window appears at the bottom of the screen you choose console. There you can see all the errors produced by your code or stuff you print with the console.log function. Other browsers have similar way of accessing the console. Usually you'll have to find something like Developer tools inside a menu and choose JavaScript console or something like that...


    If you manage to get to the console window you'll see that right after you choose your class you get an error. This error pops up because you are referencing a variable (charclasse) before you had declare it. You could get away with this before but I think that in ECMAScript5 and in so called strict mode you have to declare your variables prior to referencing them. When you correct this you have one more error inside of the testing section of your for loop. It's a typo! After the last logical AND operator you have charclasses instead of charclasse. So this will pop up another error and halt further execution of your code.

    I said that I'm not going to change any of your code but I'm going to give you one tip. Instead of using a lot of logical AND operations in your code and making JS to perform a lot of unnecessary job you can do something like this:

    Code:
    var charclasse;
    
    for(;isNaN(charclasse);){
        charclasse=prompt("Please enter one of the available numbers to select your class. \n 1. Warrior \n 2. Archer \n 3. Mage", "");
        charclasse = (charclasse === "") ? undefined : charclasse; //prevents user from not entering anything to the dialog box
    }
    This:

    Code:
     charclasse = (charclasse === "") ? undefined : charclasse;
    is the same as:

    Code:
    if (charclasse === "") charclasse = undefined;
    You can find more about conditional operator at https://developer.mozilla.org/en-US/...ional_Operator if you are interested. Basically it is used as a shortcut for the if statement.

    Also note that instead of using the prompt function to alert the user with a message you can use alert function (hence the name).

    Code:
    alert("Hello world");
    Note that I didn't analyse your code any further than this.

    Good luck!
    Last edited by tech_soul8; 02-03-2014 at 05:09 AM.

  6. #6
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    This is spam! (cannot delete original post!)
    Last edited by tech_soul8; 02-03-2014 at 05:57 AM.

  7. #7
    Join Date
    Feb 2014
    Posts
    4
    Quote Originally Posted by tech_soul8 View Post
    If I don't modify your code (and I ain't gonna do that), this is one way to accomplish it. Add this to the body section of your web document:

    HTML Code:
    <button onclick="startGame()">Start</button>
    then create one more function and name it startGame. Putt all of the code from if (start) (including if statement) to the last closing curly braces inside of it. This goes at the top of the function:

    [code]
    start = true;
    [code]




    This depends on the browser you're using. I'm using google-chrome. In google-chrome you can right-click the web page and choose inspect element. When the small window appears at the bottom of the screen you choose console. There you can see all the errors produced by your code or stuff you print with the console.log function. Other browsers have similar way of accessing the console. Usually you'll have to find something like Developer tools inside a menu and choose JavaScript console or something like that...


    If you manage to get to the console window you'll see that right after you choose your class you get an error. This error pops up because you are referencing a variable (charclasse) before you had declare it. You could get away with this before but I think that in ECMAScript5 and in so called strict mode you have to declare your variables prior to referencing them. When you correct this you have one more error inside of the testing section of your for loop. It's a typo! After the last logical AND operator you have charclasses instead of charclasse. So this will pop up another error and halt further execution of your code.

    I said that I'm not going to change any of your code but I'm going to give you one tip. Instead of using a lot of logical AND operations in your code and making JS to perform a lot of unnecessary job you can do something like this:

    Code:
    var charclasse;
    
    for(;isNaN(charclasse);){
        charclasse=prompt("Please enter one of the available numbers to select your class. \n 1. Warrior \n 2. Archer \n 3. Mage", "");
        charclasse = (charclasse === "") ? undefined : charclasse; //prevents user from not entering anything to the dialog box
    }
    This:

    Code:
     charclasse = (charclasse === "") ? undefined : charclasse;
    is the same as:

    Code:
    if (charclasse === "") charclasse = undefined;
    You can find more about conditional operator at https://developer.mozilla.org/en-US/...ional_Operator if you are interested. Basically it is used as a shortcut for the if statement.

    Also note that instead of using the prompt function to alert the user with a message you can use alert function (hence the name).

    Code:
    alert("Hello world");
    Note that I didn't analyse your code any further than this.

    Good luck!
    Thank you so much, man, that's what I needed, the console thing, now that I can see what is wrong I can easily fix it.

Thread Information

Users Browsing this Thread

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

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