...

View Full Version : Function not defined error message



lordofthesword8
07-05-2004, 11:13 AM
Hi,

Ive written a script and when ive run it on Netscape, it comes up with the function() not defined error message. Could someone please tell me what this is and how to fix it? :confused:

jbot
07-05-2004, 11:17 AM
Hi,

Ive written a script and when ive run it on Netscape, it comes up with the function() not defined error message. Could someone please tell me what this is and how to fix it? :confused:

because your page can't find the function that you've written. might just be a typo, or it's being called at the wrong time.

however, given that none of us are psychic (no really, we're not believe it or not), you'll have to supply some code for us to really decipher the problem. otherwise, we just can't help you. :rolleyes:

lordofthesword8
07-05-2004, 01:45 PM
Thanks a lot for that definition it really helps :thumbsup:

Here is the code, and I would appreciate if you would have a go at cracking it for me.

function attackcalc() {
if (document.fight.answer.value=="A") {
attackenemy()
enemyattack(0)
}
if (document.fight.answer.value=="B") {
enemyattack(parseInt(document.fight.herolevel.value))
}
if (document.fight.answer.value=="C") {
document.fight.location.value = 'magicwrite'
magiclist = new Array("Do you want to use:\rA. Fire Ball\r","Do you want to use:\rA. Fire Ball\rB. Meteor Ball\r","Do you want to use:\rA. Fire Ball\rB. Meteor Ball\rC. Shining Blade\r","Do you want to use:\rA. Fire Ball\rB. Meteor Ball\rC. Shining Blade\rD. Inferno\r","Do you want to use:\rA. Fire Ball\rB. Meteor Ball\rC. Shining Blade\rD. Inferno\rE. Final Blow")
level = parseInt(document.fight.herolevel.value.value)
document.fight.movelist.value = magicstring[level]
}
}

function fireball() {
if (parseInt(document.fight.yourcurrentmagic.value)>=5) {
alert("You cast a Fireball!")
var enemyhealth = parseInt(document.fight.enemycurrenthealth.value)
enemyhealth = enemyhealth-5
document.fight.enemycurrenthealth.value = enemyhealth
}
if (parseInt(document.fight.yourcurrentmagic.value)<5) {
alert("You do not have enough Magic left to cast this!")
}
enemyattack(0)
}

function magiccalc() {

if (document.fight.answer.value=="A") {
fireball()
}
if (document.fight.answer.value=="B") {
meteorball()
}
if (document.fight.answer.value=="C") {
shineingblade()
}
if (document.fight.answer.value=="D") {
inferno()
}
if (document.fight.answer.value=="E") {
finalblow()
}
}

function calccalc() {
var location = document.fight.location.value
if (location=="attackwrite") {
atackcalc()
}
if (location=="magicwrite") {
magiccalc()
}
}

function youwrite() {
var totalhealth = parseInt(opener.document.darkworlds.herolevel.value)
document.fight.heroname.value = opener.document.darkworlds.hero.value
document.fight.herolevel.value = opener.document.darkworlds.herolevel.value
document.images.youpic.src=opener.document.darkworlds.hero.value + ".gif"
document.fight.yourcurrenthealth.value = opener.document.darkworlds.health.value
document.fight.yourtotalhealth.value = totalhealth*20
totalmagic = parseInt(opener.document.darkworlds.herolevel.value)
document.fight.yourcurrentmagic.value = totalmagic*10
document.fight.yourtotalmagic.value = totalmagic*10
}

function enemywrite() {

enemyname = getenemyname()
enemylevel = getenemylevel()
document.fight.enemycurrenthealth.value = enemylevel*20
document.fight.enemytotalhealth.value = enemylevel*20
document.fight.enemyname.value = enemyname
document.fight.enemylevel.value = enemylevel
document.images.enemypic.src=enemyname + ".gif"

document.fight.enemycurrentmagic.value = enemylevel*10
document.fight.enemytotalmagic.value = enemylevel*10

}

function getenemyname() {
var loc = opener.document.darkworlds.location.value
var enemy = ""

switch(loc) {

case "mountain1write":
enemy = "Stone Snake"
break

case "mountain3write":
enemy = "Mountain Troll"
break

case "mountain6write":
enemy = "Elven Sentries"
break

}
return enemy
}

function getenemylevel() {
var loc = opener.document.darkworlds.location.value
var enemy = 0

switch(loc) {

case "mountain1write":
enemy = 1
break

case "mountain3write":
enemy = 2
break

case "mountain6write":
enemy = 3
break

}
return enemy
}

function attackwrite() {
document.fight.location.value = 'attackwrite'
document.fight.movelist.value = 'Do you want to:\r A. Attack \r B. Defend \r C. Cast a Spell'
}

function attackenemy() {
var yourattack = parseInt(document.fight.herolevel.value)
yourattack = yourattack*2
var enemyhealth = parseInt(docuemnt.fight.enemycurrenthealth.value)
enemyhealth = enemyhealth-yourattack
document.fight.enemycurrenthealth.value = enemyhealth
}

function start() {
youwrite()
enemywrite()
attackwrite()
}

The function im having problem with is the attackcalc() function. I dont htink its being called and I put it first in case it was a missing {} but with no luck.

Thanks for replying.

LOTS8

jbot
07-05-2004, 01:54 PM
ok, wot function are you trying to call, and upon what event or action?

lordofthesword8
07-05-2004, 02:03 PM
The function is calccalc() and the event is when the user clicks a button.

What I want to happen with this script is that, when the button is pressed, the func calccalc() will see where you are in the script. In the first step it will then either attack, defend or write in a text box what spells you have to choose from. If you asked for the spell list the next time you click on the button it will run th eapropriate spell function depending on what your input was.

Am I doing this in a roundabout way?

Thanks

LOTS8

jbot
07-05-2004, 02:11 PM
for a start, in your calccalc function, rename your location variable. it's an actual JS object and is a reserved word, so that's not gonna help. it may even be the source of the error.

also, you can redefine the if statements in the magiccalc() using a switch statement. that would be better written and would parse quicker.

also, use semi-colons at the end of each statement. it's good practice. like so:



function getenemyname()
{
var loc = opener.document.darkworlds.location.value;
var enemy = "";

switch(loc)
{
case "mountain1write": enemy = "Stone Snake"; break;
case "mountain3write": enemy = "Mountain Troll"; break;
case "mountain6write": enemy = "Elven Sentries"; break;
}

return enemy ;
}

lordofthesword8
07-06-2004, 08:49 AM
Thanks a lot for all this help.

I did not realise location was a reserved word, and I will go through my script replacing it with loc. And thanks for the bit about using a switch statement, as it takes about five minutes trying to see the logic I used in the code. Also I did not realise you should put a ; after each line or statement. Can someone say why??

LOTS8

jbot
07-06-2004, 10:12 AM
Also I did not realise you should put a ; after each line or statement. Can someone say why??
LOTS8

you don't have to put a semi-colon at the end of each line, but you do after each statement.

however, i still think it's good practice to end a line with one, if only to err on the side of safety

lordofthesword8
07-07-2004, 10:12 AM
Thanks for all this help, as my script is finally working!! Thanks you very much!! :D :thumbsup: :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum