SitePoint Sponsor |
|
User Tag List
Results 1 to 2 of 2
Thread: Function expected
-
May 30, 2004, 17:54 #1
- Join Date
- Apr 2003
- Location
- PA
- Posts
- 518
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Function expected
Line 195; Capitolized comment for your convenience (its on the far right)
Supposedly the Function expected error occurs when a function hasn't been defined yet. This makes 0 sense because I'm calling a function that has been defined, and I'm not even running the script until the page has loaded.
Code:<html><head><title>Test Game</title><script language="JavaScript"> var fightData = new Array(); attacker_name = "Attacker"; defender_name = "Defender"; max_attacker_health = 100; max_defender_health = 100; fightData[0] = new Array(); fightData[0]["round"] = 5; fightData[0]["attacker"] = "attacker"; fightData[0]["defender"] = "defender"; fightData[0]["min_roll"] = "334"; fightData[0]["this_roll"] = "655"; fightData[0]["attack_type"] = "casting"; fightData[0]["attack_damage"] = 15; fightData[0]["defender_health"] = 85; fightData[1] = new Array(); fightData[1]["round"] = 8; fightData[1]["attacker"] = "attacker"; fightData[1]["defender"] = "defender"; fightData[1]["min_roll"] = "334"; fightData[1]["this_roll"] = "141"; fightData[1]["attack_type"] = "melee"; fightData[1]["attack_damage"] = -1; fightData[2] = new Array(); fightData[2]["round"] = 8; fightData[2]["attacker"] = "defender"; fightData[2]["defender"] = "attacker"; fightData[2]["min_roll"] = "334"; fightData[2]["this_roll"] = "832"; fightData[2]["attack_type"] = "melee"; fightData[2]["attack_damage"] = 22; fightData[2]["defender_health"] = 78; fightData[3] = new Array(); fightData[3]["round"] = 8; fightData[3]["attacker"] = "defender"; fightData[3]["defender"] = "attacker"; fightData[3]["min_roll"] = "334"; fightData[3]["this_roll"] = "508"; fightData[3]["attack_type"] = "casting"; fightData[3]["attack_damage"] = 32; fightData[3]["defender_health"] = 46; fightData[4] = new Array(); fightData[4]["round"] = 10; fightData[4]["attacker"] = "attacker"; fightData[4]["defender"] = "defender"; fightData[4]["min_roll"] = "334"; fightData[4]["this_roll"] = "963"; fightData[4]["attack_type"] = "casting"; fightData[4]["attack_damage"] = 18; fightData[4]["defender_health"] = 67; fightData[5] = new Array(); fightData[5]["round"] = 15; fightData[5]["attacker"] = "attacker"; fightData[5]["defender"] = "defender"; fightData[5]["min_roll"] = "334"; fightData[5]["this_roll"] = "765"; fightData[5]["attack_type"] = "casting"; fightData[5]["attack_damage"] = 20; fightData[5]["defender_health"] = 47; fightData[6] = new Array(); fightData[6]["round"] = 16; fightData[6]["attacker"] = "defender"; fightData[6]["defender"] = "attacker"; fightData[6]["min_roll"] = "334"; fightData[6]["this_roll"] = "816"; fightData[6]["attack_type"] = "melee"; fightData[6]["attack_damage"] = 31; fightData[6]["defender_health"] = 15; fightData[7] = new Array(); fightData[7]["round"] = 16; fightData[7]["attacker"] = "defender"; fightData[7]["defender"] = "attacker"; fightData[7]["min_roll"] = "334"; fightData[7]["this_roll"] = "566"; fightData[7]["attack_type"] = "casting"; fightData[7]["attack_damage"] = 24; fightData[7]["defender_health"] = 0; setTimeout("result_area.innerHTML += 'You lose. ';", 17000); function attacker_name(attacker) { if (attacker == "attacker") { // Initiator is attacking attackerName = "You"; } else { // Otherwise set it to the current attacker's name (the defender of the fight) attackerName = defender_name; } alert(attackerName); } function attacker_name_possessive(attacker) { if (attacker == "attacker") { // Initiator is attacking attackerPossessive = "Your"; } else { // Otherwise set it to the current attacker's name (the defender of the fight) attackerPossessive = defender_name + "'s"; } return attackerPossessive; } function defender_name(defender) { if (defender == "attacker") { // Initiator is defending defenderName = "You"; } else { // Otherwise set it to the current defender's name (the defender of the fight) defenderName = defender_name; } return defenderName; } function defender_name_possessive(defender) { if (defender == "attacker") { // Initiator is defending defenderPossessive = "Your"; } else { // Otherwise set it to the current attacker's name (the defender of the fight) defenderPossessive = defender_name + "'s"; } return defenderPossessive; } function doFight() { // Loop through all of the events for (var i = 0; i < fightData.length; i++) { // Construct the roll_string roll_string = "[Roll: " + fightData[i]["this_roll"] + "/" + fightData[i]["min_roll"] + "]"; // Construct the attack_string switch (fightData[i]["attack_type"]) { case "melee": if (fightData[i]["attack_damage"] >= 0) { attack_string = "<font color=red><b>" + attacker_name(fightData[i]["attacker"]) + " hit " + defender_name(fightData[i]["defender"]) + " for " + fightData[i]["attack_damage"] + " damage!</b></font>"; } else { attack_string = "<font color=red>" + attacker_name(fightData[i]["attacker"]) + " swung at " + defender_name(fightData[i]["defender"]) + " but <b>missed!</b></font>"; } break; case "casting": if (fightData[i]["attack_damage"] >= 0) { attack_string = "<font color=blue><b>" + attacker_name(fightData[i]["attacker"]) + " cast on " + defender_name(fightData[i]["defender"]) + " for " + fightData[i]["attack_damage"] + " damage!</b></font>"; // THIS ONE RIGHT HERE! } else { attack_string = "<font color=blue>" + attacker_name_possessive(fightData[i]["attacker"]) + " spell <b>fizzled!</b></font>"; } break; } // Construct the result_string, which will be echoed in "results" result_string = roll_string + " " + attack_string + "<br><br>"; code = "result_area.innerHTML += \"" + result_string + "\";"; delay = fightData[i]["round"] * 1000; setTimeout(code, delay); } } </script></head><body onLoad="doFight();"> <table border="0" cellpadding="5" cellspacing="0"> <tr> <td> <h1> <table id="attackerStats" border="0" cellpadding="0" cellspacing="0" width="250"> <tr> <th colspan="2">Attacker</th> </tr> <tr> <td id="health" width="100%" height="15" bgcolor="red" align="center"></td> <td width="*"></td> </tr> </table> </h1> </td> <td valign="middle"> vs. </td> <td> <h1> <table id="defenderStats" border="0" cellpadding="0" cellspacing="0" width="250"> <tr> <th colspan="2">Defender</th> </tr> <tr> <td id="health" width="100%" height="15" bgcolor="red" align="center"></td> <td width="*"></td> </tr> </table> </h1> </td> </tr> </table> <span id="result_area"></span></body></html>
-
May 30, 2004, 19:44 #2
- Join Date
- Apr 2003
- Location
- PA
- Posts
- 518
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Solved, function name was the same as a variable name. Didn't know you couldn't do that in JavaScript.
Bookmarks