Click to See Complete Forum and Search --> : how can i call 3 functions with 1 click?


kev_will
09-25-2003, 03:40 AM
Hi, please click here (http://www.conflict-lesson.co.us/fistpalm.htm) for the file I'm working on. The game should function like this: the user clicks either the 'fist' or 'palm' button, the computer chooses randomly either fist of palm for itself. the score is calculated according to these rules
user fist - comp fist - 1/1
user fist - comp palm -1/3
user palm - comp fist - 3/1
user palm - comp palm 4/4
at the moment when the user clicks either of the buttons I use onMouseDown to display the user hand, onMouseUp chooses a random number and then displays the computer's hand.
<button name="fist" type="button" onMouseDown="userChoose(0)" onMouseUp="compChoose()">Fist</button>&nbsp;

which calls these functions:

function randomNumber(n) { // random number function
number = Math.round(Math.random()*n);
return number;
}

function compChoose() { // computer choses its hand
compVar = randomNumber(1);
if (compVar >= 0.5) {
compHand = 1;
document.images.comp.src = ("images/palm.gif");
}
else {compHand = 0;
compImg = fistImg;
document.images.comp.src = ("images/fist.gif");
}
return compImg;
}

function userChoose(n) { // user chooses their hand
if (n == 0) {
document.images.user.src = ("images/fist.gif");
}
else {
document.images.user.src = ("images/palm.gif");
}
return n;
}


I would also like it to then calculate the score, however the score needs to take into account both the user and computer choices.
If you need any more info please let me know and I'll let you know the details.
Any thoughts ?

Fang
09-25-2003, 03:48 AM
onclick="userChoose(0)"; compChoose(); calculate();"

kev_will
09-25-2003, 05:47 AM
thanks for the quick reply but how do I send the appropriate variables?
calculate(u,s) ?

Fang
09-25-2003, 05:57 AM
Show more/all the code. I can already see a problem in function compChoose

kev_will
09-25-2003, 06:09 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Fist and Palm</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script language="JavaScript" type="text/JavaScript">
if (document.images) { //preload images
var startImg = new Image();
startImg.src = "images/start.gif";
var palmImg = new Image();
startImg.src = "images/palm.gif";
var fistImg = new Image();
startImg.src = "images/fist.gif";
}
var number = 0; // variables
var compVar = 0;
var compHand = 0;
var userImg = startImg;
var compImg = startImg;
var userHand = 0;
var userScore = 0;
var compScore = 0;
var gameRound = 0;

function randomNumber(n) { // random number function
number = Math.round(Math.random()*n);
return number;
}

function compChoose() { // computer choses its hand
compVar = randomNumber(1);
if (compVar >= 0.5) {
compHand = 1;
document.images.comp.src = ("images/palm.gif");
}
else {compHand = 0;
compImg = fistImg;
document.images.comp.src = ("images/fist.gif");
}
return compImg;
}

function userChoose(n) { // user chooses their hand
if (n == 0) {
document.images.user.src = ("images/fist.gif");
}
else {
document.images.user.src = ("images/palm.gif");
}
return n;
}

function score(u, c) { // function to calculate the score
if (u = 0) {
if (c = 0) {
userScore = userScore + 1;
compScore = compScore + 1;
}
else {
userScore = userScore + 3;
compScore = compScore + 1;
}
}
else {
if (c = 0) {
userScore = userScore + 1;
compScore = compScore + 3;
}
else {
userScore = userScore + 4;
compScore = compScore + 4;
}
}
return;
}



</script>
</head>

<body bgcolor="#339900" text="#FFFFFF" link="#FFFFFF" vlink="#FFFFFF" alink="#FF0000">

<table width="85%" border="0" align="center" cellpadding="1" cellspacing="1">
<tr>
<td width="50%"><div align="center"><font face="Verdana, Arial, Helvetica, sans-serif">Your
hand</font></div></td>
<td width="50%"><div align="center"><font face="Verdana, Arial, Helvetica, sans-serif">The
computer's hand</font></div></td>
</tr>
<tr>
<td height="200"><div align="center">
<a href="#"><img name="user" height="200" width="200" border="0" src="images/start.gif"></a></div></td>
<td height="200"><div align="center">
<a href="#"><img name="comp" height="200" width="200" border="0" src="images/start.gif"></a></div></td>
</tr>
<tr>
<td><div align="center">Choose:
<button name="fist" type="button" onMouseDown="userChoose(0)" onMouseUp="compChoose()">Fist</button>&nbsp;
<button name="palm" type="button" onMouseDown="userChoose(1)" onMouseUp="compChoose()">Palm</button></div></td>
<td><div align="center"></div></td>
</tr>
<tr>
<td><div align="center"><font face="Verdana, Arial, Helvetica, sans-serif">Your
score =
<script language="JavaScript" type="text/JavaScript">
document.write(userScore);
</script>
</font></div></td>
<td><div align="center"><font face="Verdana, Arial, Helvetica, sans-serif">The
computer's score =
<script language="JavaScript" type="text/JavaScript">
document.write(compScore);
</script>
</font></div></td>
</tr>
</table>
</body>
</html>

Fang
09-25-2003, 01:15 PM
I've made a few changes:
Table layout removed, replaced with 2 column css layout.
Form corrected.
Replaced scores with form elements, document.write will not work here.
Added New game function.
Changed script tag.
Replaced font tag with css.
I didn't check function score, it appears to be bias.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Fist and Palm</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script type="text/JavaScript">
<!--
if (document.images) { //preload images
var startImg = new Image();
startImg.src = "images/start.gif";
var palmImg = new Image();
startImg.src = "images/palm.gif";
var fistImg = new Image();
startImg.src = "images/fist.gif";
}
/*
var number = 0; // variables
var compVar = 0;
var compHand = 0;
var userImg = startImg;
var compImg = startImg;
var userHand = 0;
var gameRound = 0;
*/
function randomNumber(n) { // random number function
number = Math.round(Math.random()*n);
return number;
}

function compChoose() { // computer choses its hand
if (randomNumber(1) >= 0.5) {
document.images.comp.src = "images/palm.gif";
return "palm";
}
else {
document.images.comp.src = "images/fist.gif";
return "fist";
}
}

function userChoose(n) { // user chooses their hand
if (n == "fist") {
document.images.user.src = "images/fist.gif";
}
else {
document.images.user.src = "images/palm.gif";
}
}

var userScore = 0;
var compScore = 0;
function score(u, c) { // function to calculate the score
if (u == "fist") {
if (c == "palm") {
userScore+= 1;
compScore+= 1;
}
else {
userScore+= 3;
compScore+= 1;
}
}
else {
if (c == "palm") {
userScore+= 1;
compScore+= 3;
}
else {
userScore+= 4;
compScore+= 4;
}
}
document.getElementById('user_score').value=userScore;
document.getElementById('comp_score').value=compScore;
}

function Play(hand) {
userChoose(hand);
score(hand, compChoose());
}

function NewGame() {
userScore=compScore=0;
document.images.user.src = "images/start.gif";
document.images.comp.src = "images/start.gif";
}
//-->
</script>
<style type="text/css">
<!--
body {color:#fff; background:#390; font-size:16px; font-family:Verdana, Arial, Helvetica, sans-serif;}
div {padding:5px;}
input {font-size:18px; font-weight:bold; text-align:center;}
-->
</style>
</head>

<body>

<div style="text-align:center;"><div style="margin:0 auto 0 auto; width:500px;">
<form id="game" action="#">
<div id="left" style="float:left;">
<div>Your hand</div>
<img id="user" height="200" width="200" src="images/start.gif" alt="user image">
<div>Choose:
<button id="fist" type="button" onclick="Play('fist');">Fist</button>&nbsp;
<button id="palm" type="button" onclick="Play('palm');">Palm</button>
</div>
<div>Your score = <input type="text" id="user_score" size="1" value="0" /></div>
</div>
<div id="right" style="float:right;">
<div>The computer's hand</div>
<img id="comp" height="200" width="200" src="images/start.gif" alt="comp image">
<div><button id="reset" type="reset" onclick="NewGame();">New game</button></div>
<div style="white-space:nowrap;">The computer's score = <input type="text" id="comp_score" size="1" value="0" /></div>
</div>
<p style="clear:both;">&nbsp;</p>
</form>
</div></div>

</body>
</html>

kev_will
10-20-2003, 03:01 PM
this is brilliant, thank you. The score is supposed to be biased so that's ok
:cool:

Jellybean
12-16-2004, 08:49 AM
Hi,

I am currently trying to do something like an email sys. So, whenever the reader wants to delete an email, the sys will prompt for confirmation through a dialog menu.

I would like to call 2 functions upon clicking the confirmation to delete button, such that one function will close the dialog menu and the other will redirect the other window to the inbox page (or any other pages).

This is my script for calling the 2 functions:

onClick="javascript:window.close(); refresh_page();"

And this is the script for refresh_page function:

<script language="Javascript" type="text/javascript">

function refresh_page(){
window.location.href = "inbox.php";
}

</script>

However, I couldn't get this working and I need help! :confused:

Any advise is most appreciated.

Thank you for reading.

Regards,
Jellybean :D