A Shorter Way?

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

A Shorter Way?

Post by Epiales »

Is there a way to shorten this so I don't have to write it everytime before each else?

The first code right below this is what I have to repeat.....the next code is the actual code (battle), and the last is the forms it uses to display the results.

Code: Select all

	$outputList .= 'Your critical attack hits and ' . $arena_char_name . ' suffers ' . $player_critical . ' health'; ?>
	
	<script>
	setTimeout('document.status.msgbx.value = "<?php echo $outputList; ?>";', 2000);
	</script>

Code: Select all

<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'; ?>
    
    <script>
    setTimeout('document.status.msgbx.value = "<?php echo $outputList; ?>";', 2000);
    </script>

<?php

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

    $outputList .= 'Your attack hits and ' . $arena_char_name . ' suffers ' . $player_atkdam . ' health'; ?>
    
    <script>
    setTimeout('document.status.msgbx.value = "<?php echo $outputList; ?>";', 2000);
    </script>

<?php

}else{

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

<?php

    }

Code: Select all

<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>

<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>
Nothing fancy, but a work in progress!

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

Re: A Shorter Way?

Post by MikuzA »

Hello,

Since you are using an IF sentence, you could do it like this.

Code: Select all

IF ( CRIT ) {
$Output = 'Crit';
}
ELSEIF ( HIT ) {
$Output = 'Hit';
}
ELSE{
$Output ='Miss';
}
?>
<script>js_here[echo $Output]</script>
<?PHP
(And notice the above is not real code :D)

What I am saying is that during the IF-ELSE process, there is only one way out. and after that you can display the generic stuff using the happening inside the IF.
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: A Shorter Way?

Post by Jackolantern »

This code doesn't really have a point:

Code: Select all

	$outputList .= 'Your critical attack hits and ' . $arena_char_name . ' suffers ' . $player_critical . ' health'; ?>
	
	<script>
	setTimeout('document.status.msgbx.value = "<?php echo $outputList; ?>";', 2000);
	</script>
The PHP variable $outputlist is only going to be evaluated once on the server, and each time the timer goes off, it will be overwriting with the same value. Basically, you will be putting a literal constant into status.msgbx every 2 seconds.
The indelible lord of tl;dr
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: A Shorter Way?

Post by Epiales »

Jackolantern wrote:This code doesn't really have a point:

Code: Select all

	$outputList .= 'Your critical attack hits and ' . $arena_char_name . ' suffers ' . $player_critical . ' health'; ?>
	
	<script>
	setTimeout('document.status.msgbx.value = "<?php echo $outputList; ?>";', 2000);
	</script>
The PHP variable $outputlist is only going to be evaluated once on the server, and each time the timer goes off, it will be overwriting with the same value. Basically, you will be putting a literal constant into status.msgbx every 2 seconds.
I went a different route with this in my other threads. I got rid of the output and such. Thx :)
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Post Reply

Return to “Advanced Help and Support”