Javascript Help

Discussion in 'Miscellaneous' started by Y0urWaifuisTrash, Feb 5, 2014.

  1. Hey :)
    I've been trying to write a battle script in JS for a little while, and I'm kinda stuck >.<
    I'm designing it to be a turn-based battle system, similar to Pokémon, and I can't figure out how to get it to loop until you or the monster are dead :/
  2. Does it 'play itself' or when a button is pushed?

    If it plays it self, you would do something like this:

    Code:
    var player, enemy; // define these somehow
    function playRound() {
       // do stuff
       if (player.health <= 0) {
           enemyWins();
       } else if (enemy.health <= 0) {
           playerWins();
       } else {
          setTimeout(playRound, 5000); // 5 seconds till next round
       }
    }
    playRound(); // start the game
    
    Do not use a while() or for() loop as it needs time to breathe or the browser will be locked up.
    Choongjae likes this.
  3. Edit: yeah I got rid of my loop code because I was right, you dont want to use loops XD
    threads or what Aikar mentioned
  4. Sorry, should have mentioned >.< I'm making it using alerts, etc, and it's going to be an executable JS file >.< sorry *slaps self*
  5. Let's see how relatable lua is to java. :D

    var player, enemy; Creating a variable
    function playRound() { Declaring a function
    // do stuff
    if (player.health <= 0) { Conditional statement
    enemyWins();
    } else if (enemy.health <= 0) { Conditional statement
    playerWins();
    } else {
    setTimeout(playRound, 5000); // eh..timer thing magic?
    }
    }
    playRound(); // Calling the function

    I assume that the names must be different though?
    (Conditional statement isn't in javascript, is it?)
    princebee likes this.
  6. (js, nat java)
    Choongjae likes this.
  7. JavaScript is a turing complete language... so yes it has conditionals lol. You can do almost anything in JS.
  8. Like create ICC eggnog?
    Cchiarell6914 likes this.
  9. This is just something general i'd do... having event options that will occur if something else happens using small videos, and health, and options. :)

    //Set your id and source of the videos
    var avid = document.getElementById("ArenaStart");
    var pvid = document.getElementById("PlayerMove");
    var evid = document.getElementById("EnemyMove");
    var pvids = document.getElementById("PlayerHit");
    var pvidn = document.getElementById("PlayerMiss");
    var pvidd = document.getElementById("PlayerDie");
    var evids = document.getElementById("EnemyHit");
    var evidn = document.getElementById("EnemyMiss");
    var evidd = document.getElementById("EnemyDie");
    var pvid2 = document.getElementById("PlayerWin");
    var evid2 = document.getElementById("EnemyWin");

    //User and Enemy Select aren't videos, they will be where your user and enemy selection come from.
    var actionp = document.getElementById("UserSelect");
    var actione = document.getElementById("EnemySelect");

    Var r = 0; //keeps track of order of events and number should change
    Var option = 0; //will be for randomized attack success.
    Var optione = 0; //will be for randomized enemy attack.

    Set Me.Health = 100;
    Set Enemy.Health = 100;

    function gamestart() {avid.play();}
    function enemyturn() {evid.play();}
    function enemyhit() {evids.play();}
    function enemymiss() {evidn.play();}
    function enemydie() {evidd.play();}
    function enemywin() {evid2.play();}
    function playerturn() {pvid.play();}
    function playerhit() {pvids.play();}
    function playermiss() {pvidn.play();}
    function playerdie() {pvidd.play();}
    function playerwin() {pvid2.play();}
    function userselect() {actionp().options[0-3];}
    function enemyselect() {actione().random[0-3];} //I honestly don't know about the Select ones...

    If (r == 0) {
    gamestart();
    r = 1;}

    If ((r == 1) && (Me.Health > 0)) {
    playerturn();
    r = 2;}

    If (r == 2) {
    var rnum=Math.floor(Math.random()*3) //since it's number 3 that means it allows for a random number between 0-3 (so 4 numbers)
    option = userselect();{
    If (option == rnum){
    playerhit();
    Enemy.health = (Enemy.Health - 10); //change 10 to whatever
    r = 3;}
    else {playermiss();
    r = 3;}
    }

    If ((r == 3) && (Enemy.Health > 0)) {
    enemyturn();
    r = 4;}

    If (r == 4) {
    var rnuma=Math.floor(Math.random()*3) //since it's number 3 that means it allows for a random number between 0-3 (so 4 numbers)
    optione = enemyselect();{
    If (optione == rnuma){
    enemyhit();
    Me.Health = (Me.Health - 10); //change 10 to whatever
    r = 1;}
    else {enemymiss();
    r = 1;}

    If ((r == 1) && (Player.Health == 0)) {playerdie();
    r = 5;}

    If (r == 5) {enemywin();
    r = 7;}

    If ((r == 3) && (Enemy.Health == 0)) {enemydie();
    r = 6;}

    If (r == 6) {playerwin();
    r = 8;}

    If (r == 7) {//Maybe a restart option?}

    If (r == 8) {//A possible Next Level?}

    Edit: ps this honestly took me forever to think of and write haha
    just_five_fun likes this.
  10. There's an easy way... RPG Maker VX Ace (I have atm :D )
  11. holy crap...
    I feel really bad that I don't really understand most of that ; - ;
    I'll probably use that for another project i'm working on

    right now what I really need is how to figure out how to loop something UNTIL either playerhp or monsterhp <= 0
  12. Are you using Canvas to do this? To make the game loop continually, use something like...
    Code:
    if (playerhp>0 || monsterhp>0){
    var frameRate;
    setInterval( function(){
    //include the functions called here that will repeat every time.
    }, 1000/frameRate);
    }
    
    If that doesn't work, remove the if statement but keep the rest. Put the if statement in the function being called instead.