Javascript Stop Loop

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Javascript Stop Loop

Post by Epiales »

I don't suppose this will be an easy question to fix/answer.... But the code I"ve worked on is below. As you'll notice, I've mixed PHP with Javascript for the battle. Sure it will probably give someone a headache searching through to see what I've done lmao... but for not ever doing JS before, it's not a bad start if I say so myself :mrgreen: :mrgreen:

Problem is, is where I started a loop with a max 100, it went through the entire 100 loops, bringing both player/NPC health to like -40K health lol.....

Where do I stop it? Since I've mixed it all, I have no idea where to place it...

Now everything works... I click attack... it displays the attack on both sides... when it reaches - 0 or below on HP for either player/NPC, it displays if u've lost or won... (I'll have to fix the hp going negative eventually, but it still works for now)... I just have to keep refreshing page to do each round until I win/lose..... As said, I have no idea where to put the command, or exactly what command to put, in order to loops this and then stop when you either win or lose... I won't expect an immediate answer, as it's a lot of code to wade through, but if anyone has the time, it would be appreciated. Thank you much!

CODE:

Code: Select all

<script>
function FocusOut()
{
document.status.msgbx.focus();
}

function FocusOut1()
{
document.weapons.life.focus();
}


function npc_check()
{

var enemyHp = "<?php echo $enemyHp; ?>";

if(enemyHp <= 0){
        document.status2.msgbx2.value = "Congratulations, you win!";
}else{

    }
}


function player_check()
{

var playerHp = "<?php echo $playerHp; ?>";

if(playerHp <= 0){
        document.status2.msgbx2.value = "You've lost the battle!";
}else{

    }
}

</script>


<script>
    setTimeout('document.weapons.life.value = "<?php echo $playerHp; ?>";', 0);
</script>


<?php 

    $player_accuracy = mt_rand($user_accuracy, 100);
    $player_atkdam = mt_rand(0,$user_strength);
    $player_critical = mt_rand(0,$user_strength)*2;

if($player_accuracy > 80 && $player_critical>=$user_strength){

    $outputList .= 'Your critical attack hits and ' . $arena_char_name . ' suffers ' . $player_critical . ' health';
    
        $npc_hp_lost = $player_critical;
        $npc_new_hpoints -= $npc_hp_lost;    

    
         $updateatkhp = "UPDATE npc_characters SET hpoints=hpoints +'$npc_new_hpoints' WHERE charid ='$character' AND userid = '$user_id'"; 
        $user_query = mysqli_query($db_conx, $updateatkhp);
    
 ?>    
    <script>
    setTimeout('document.status.msgbx.value = "<?php echo $outputList; ?>";', 2000);
    setTimeout('npc_check()', 6000);

    </script>

<?php

}elseif($player_accuracy < 80 || $player_accuracy > 80) {

    $outputList .= 'Your attack hits and ' . $arena_char_name . ' suffers ' . $player_atkdam . ' health'; 
    
        $npc_hp_lost = $player_atkdam;
        $npc_new_hpoints -= $npc_hp_lost;    

    
         $updateatkhp = "UPDATE npc_characters SET hpoints=hpoints +'$npc_new_hpoints' WHERE charid ='$character' AND userid = '$user_id'";
        $user_query = mysqli_query($db_conx, $updateatkhp);
?>
    
    <script>
    setTimeout('document.status.msgbx.value = "<?php echo $outputList; ?>";', 2000);
    setTimeout('npc_check()', 6000);
    </script>

<?php

}else{

    $outputList .= 'Your attacked missed!  You really suck !'; ?>
    
    <script>
    setTimeout('document.status.msgbx.value = "<?php echo $outputList; ?>";', 2000);
    setTimeout('npc_check()', 6000);
    </script>

<?php

    }

$outputList1 = '';

$sql = "SELECT * FROM characters where id ='$character' LIMIT $character"; 
$user_query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($user_query);
$row = mysqli_fetch_array($user_query, MYSQLI_ASSOC);


    $enemy_accuracy = mt_rand($characters_accuracy, 100);
    $enemy_atkdam = mt_rand(0,$row['attack']);
    $enemy_critical = mt_rand(0,$row['attack'])*2;

if($enemy_accuracy > 80 && $enemy_critical>=$row['attack']){

    $outputList1 .= '' . $enemyname . ' critical attack hit you.  You suffer ' . $enemy_critical . ' health'; 

        $player_hp_lost = $enemy_critical;
        $player_new_hpoints -= $player_hp_lost;    

    
         $updateplayerhp = "UPDATE users SET hpoints=hpoints +'$player_new_hpoints' WHERE id = '$user_id'"; 
        $user_query = mysqli_query($db_conx, $updateplayerhp);

?>

    <script>
    setTimeout('document.status1.msgbx1.value = "<?php echo $outputList1; ?>";', 4000);
    setTimeout('player_check()', 8000);

    </script>

<?php

}elseif($enemy_accuracy < 80 || $enemy_accuracy > 80) {

    $outputList1 .= '' . $enemyname . ' hits and you suffer ' . $enemy_atkdam . ' health';
    
    
        $player_hp_lost = $enemy_atkdam;
        $player_new_hpoints -= $player_hp_lost;    

         $updateplayerhp = "UPDATE users SET hpoints=hpoints +'$player_new_hpoints' WHERE id = '$user_id'"; 
        $user_query = mysqli_query($db_conx, $updateplayerhp);

?>

    <script>
    setTimeout('document.status1.msgbx1.value = "<?php echo $outputList1; ?>";', 4000);
    setTimeout('player_check()', 8000);
    </script>

<?php
    

}else{

    $outputList1 .= '' . $enemyname . ' missed.  They really suck !'; ?>
    
    <script>
    setTimeout('document.status1.msgbx1.value = "<?php echo $outputList1; ?>";', 4000);
    setTimeout('player_check()', 8000);
    </script>

<?php
            }
        }
    }
}
?>
<br />

<form name="weapons">
<font face="Times New Roman" size="3"><i>Your life: </i></font>
<input type="text" readonly="true" size="10" maxlength="10" name="life" onFocus="FocusOut1()" style="background-color:#575957; border:none"><br>
</form>
<br />
<form name="status">
<input type="text" readonly="true"  name="msgbx" size="80" maxlength="80" onFocus="FocusOut()" onKeyDown="javascript: alert('Don\'t do that!');" style="background-color:#575957; border:none">
</form>
<br />
<form name="status1">
<input type="text" readonly="true"  name="msgbx1" size="80" maxlength="80" onFocus="FocusOut()" onKeyDown="javascript: alert('Don\'t do that!');" style="background-color:#575957; border:none">
</form>
<br />
<form name="status2">
<input type="text" readonly="true"  name="msgbx2" size="80" maxlength="80" onFocus="FocusOut()" onKeyDown="javascript: alert('Don\'t do that!');" style="background-color:#575957; border:none">
</form>
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Sim
Posts: 412
Joined: Sat Dec 26, 2009 5:37 pm

Re: Javascript Stop Loop

Post by Sim »

I can't find the loop, but you can use the command "break;" to exit a loop.
oRPG Creator - Make Your Own Browser Game
oRPG Creator on Facebook
User avatar
MikuzA
Posts: 395
Joined: Thu Aug 08, 2013 8:57 am

Re: Javascript Stop Loop

Post by MikuzA »

I'm guessing this is PHP loop? but that also would be 'break;'

http://php.net/manual/en/control-structures.break.php
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Javascript Stop Loop

Post by Epiales »

Sim wrote:I can't find the loop, but you can use the command "break;" to exit a loop.
I didn't put a loop because it just kept going on and on and on so I removed it. I merely wondered where I put the loop and how to stop it basically...sorry.... I use both php and js, so I get confused on what to do...still learning both, but don't know js that much at all... I've been working on attack feature for a month or longer and this is where I've gotten lol... Ain't much but it's getting there...
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Javascript Stop Loop

Post by Epiales »

MikuzA wrote:I'm guessing this is PHP loop? but that also would be 'break;'

http://php.net/manual/en/control-structures.break.php
I know this is in the other thread, so if you've answered, please ignore...

for a break? I didn't think a php break would be enough, as the php will run and the js won't until after, so that's a problem. When I put a php break in there it wouldn't run the battle scenario at all, so i wrote a php break in a javascript function but it won't work either....

Code: Select all

<script>
function break(){

var break = "<?php break; ?>";

}


function npc_check()
{

var enemyHp = "<?php echo $enemyHp; ?>";

if(enemyHp <= 0){
        document.status2.msgbx2.value = "Congratulations, you win!";
        break();

}else{

    }
}


function player_check()
{

var playerHp = "<?php echo $playerHp; ?>";

if(playerHp <= 0){
        document.status2.msgbx2.value = "You've lost the battle!";
        break();
}else{

    }
}


</script>
I also tried a different name for the function, as break is common in scripting, so it might have been confused, so I tried function breakgame() but won't work either.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
MikuzA
Posts: 395
Joined: Thu Aug 08, 2013 8:57 am

Re: Javascript Stop Loop

Post by MikuzA »

You need to handle the break; of PHP inside PHP. You cannot call it with JS.
I also explained this in the other thread :)
MikuzA wrote: PHP is server-side language, is fully executed from top to bottom during page-load.

JavaScript is client-side, and can do stuff after page has loaded.

If you do this

Code: Select all

function break(){

var break = "<?php break; ?>";

}
PHP code will break when creating this row, due break will be executed during page load. Not when JS Break is called.
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: Javascript Stop Loop

Post by Jackolantern »

Yes, I still think you may be a bit confused of the difference between PHP and Javascript. Remember that, like Mikuza said, PHP and only PHP will completely run on the server, before the player has seen any bit of the page. Once the HTML is sent to browser, the JS is parsed and run on the browser's side. The only way that PHP can interact with Javascript is that PHP can generate Javascript code text on the server side before the Javascript code is sent to the user and runs. For example:

PHP (assume $phpVar = 9):

Code: Select all

<script>
   var jsvar = <?php echo $phpVar; ?>;
</script>
This is how the Javascript would look when it runs:

Code: Select all

<script>
   var jsvar = 9;
<script>
There is no connection between the $phpVar and the JS variable jsvar. Nothing that happens when the Javascript runs will affect any of the PHP variables (in fact, they have all been destroyed on the server long before the JS runs). You just used PHP to write literal Javascript code text. No different than using PHP to write HTML.
The indelible lord of tl;dr
Post Reply

Return to “Advanced Help and Support”