Cannot create an instance of a defined class in Javascript Cannot create an instance of a defined class in Javascript
I have defined a class which represent a Pokemon:
class Pokemon
{
constructor(id,nick,level,a1,a2,a3,a4)
{
this.id=id;
this.name=setPokemonById(id);
this.nick=nick;
this.typeId=setTypeById(id);
this.type=setElementById(this.typeId);
this.level=level;
this.attack1=a1;
this.attack2=a2;
this.attack3=a3;
this.attack4=a4;
this.attack1CurrPP=this.attack1.getPP();
this.attack2CurrPP=this.attack2.getPP();
this.attack3CurrPP=this.attack3.getPP();
this.attack4CurrPP=this.attack4.getPP();
this.hp=generateHP(id,level);
this.currHP=this.hp;
}
getId() {return this.id;};
getName() {return this.name;};
getNick() {return this.nick;};
getTypeId() {return this.typeId;};
getType() {return this.type;};
getLevelr() {return this.level;};
getEvo() {return getEvolution(this.id); };
hasEvo() {return canEvolve(this.id); };
getAttack1() {return attack1; };
getAttack2() {return attack2; };
getAttack3() {return attack3; };
getAttack4() {return attack4; };
getAttack1CurrPP() {return attack1CurrPP; };
getAttack2CurrPP() {return attack2CurrPP; };
getAttack3CurrPP() {return attack3CurrPP; };
getAttack4CurrPP() {return attack4CurrPP; };
getHP() {return hp; };
getCurrHP() {return currHP; };
}
Additionally I have a class representing an attack:
class Attack
{
constructor(id,name,typeId,power,acc,pp,forgetable)
{
this.id=id;
this.name=name;
this.typeId=typeId;
this.type=setElementById(typeId);
this.power=power;
this.acc=acc;
this.pp=pp;
this.forgetable=forgetable;
}
getId() { return this.id;};
getName() {return this.name;};
getTypeId() {return this.typeId;};
getType() {return this.type;};
getPower() {return this.power;};
getAcc() {return this.acc;};
getPP() {return this.pp;};
getForgetable() {return this.forgetable; };
}
Also, I have global variables, representing a Pokemon.
var pokemon1;
From some strange reason, creating an instance of Attack
class works fine:
var water_gun=new Attack(1,"Water Gun",1,40,1,25,true);
However, creating an instance of Pokemon
class fails.
function addPokemon(pID,pLVL,pATK1,pATK2,pATK3,pATK4)
{
if(!(pokemon1)) // If the first pokemon slot is available
{
pokemon1 = new Pokemon(pID,"Champion",pLVL,pATK1,pATK2,pATK3,pATK4);
}
}
Why does defining an instance of Attack
works, but not of Pokemon
? The only difference I see is that pokemon1
receives a value inside a function. Should this be a problem?
Edit: Code
from Stackoverflow
Comments
Post a Comment