Search code examples
javascriptbabylonjs

Uncaught TypeError: Cannot read property 'value' of undefined in prototype functions


Good afternoon. At the first look, this question looks that it already answered although, I'm not beeing able to solve my problem with the answers I've found.

I've got one class, and one of the functions should call another function (passing the "self" mesh has argument), that is the problem, I'm loosing the reference somewhere and I can't find where.

Here's the code, and thanks in advance :)

//classe unit.
function unit(unitId, mesh, unitName, shieldSize, armor, armorTechLvl, shieldTechLvl, employeesNeeded, description, imageFilePath, structureNeeded, storageSize, unitSpeed, attackPower, weaponTech) {
    var unitId;
    var unitName;
    var shieldSize;
    var armor;
    var armorTechLvl;
    var shieldTechLvl;
    var employeesNeeded;
    var description;
    var imageFilePath;
    var structureNeeded;
    var storageSize;
    var unitSpeed;
    var attackPower;
    var weaponTech;
    var shipposx;
    var shipposz;
    var mesh;
    this.unitId = unitId;
    this.unitName = unitName;
    this.shieldSize = shieldSize;
    this.armor = armor;
    this.armorTechLvl = armorTechLvl;
    this.shieldTechLvl = shieldTechLvl;
    this.employeesNeeded = employeesNeeded;
    this.description = description;
    this.imageFilePath = imageFilePath;
    this.structureNeeded = structureNeeded;
    this.storageSize = storageSize;
    this.unitSpeed = unitSpeed;
    this.attackPower = attackPower;
    this.weaponTech = weaponTech;
    this.shipposx = 0;
    this.shipposz = 0;
    this.mesh = mesh;


unit.prototype.setMesh = function (mesh){
    this.mesh = mesh;
    this.mesh.position.x = unit.prototype.getShipPosx();
    this.mesh.position.z = unit.prototype.getShipPosz();
    this.mesh.position.y = 0;
    this.mesh = this;
};

unit.prototype.setShipPos = function (posx,posz){
    this.shipposx = posx;
    this.shipposz = posz;
    //self.position.x =posx;
    //self.position.z =posz;
    //self.position.y =0;
    unit.prototype.setMesh(this.mesh);
};

unit.prototype.getShipPosx = function (){
    return this.shipposx;
};

unit.prototype.getShipPosz = function (){
    return this.shipposz;
};


//método que define o tamanho do shield inicial.
unit.prototype.setShieldSize = function (){

};
//método que altera o armor e shield mediante o damage até à destruição da unidade.
unit.prototype.setDamage = function(damage) {
    var armorCheck = false;
    if (this.shieldSize > 0){
        this.shieldSize = this.shieldSize - damage;
    }
    else if (this.shieldSize < 0){
        this.shieldSize = 0;
        armorCheck = true;
    }
    if (armorCheck === true){
        this.armor = this.armor - damage;
    }
    else if (this.armor < 0){
        this.armor = 0;
        delete unit; 
        //não esquecer, se houver tempo implementar chamada à explosão da unidade ex: explode().
    }
};
//possivel método de explosão.
unit.prototype.explosion = function(posx,posy){
  //carrega uma explosão na posição x e posição y. 
};

//método para carregar objeto 3D da unidade.
unit.prototype.load3D = function(name,imageFileName){
    BABYLON.SceneLoader.ImportMesh(name, "Assets/babylonreadyfiles/", imageFileName, scene, function(newMeshes) {


    newMeshes[0].scaling.x = 0.2;
    newMeshes[0].scaling.y = 0.2;
    newMeshes[0].scaling.z = 0.2;

    unit.prototype.setMesh(newMeshes[0]);
    //if (selfchecker === 0) {
    //selfchecker = 1;
    /*$.get("Communications/getUnit.php", function(data) {
        //console.log(data);
        var name = jQuery.parseJSON(data);

    });*/
    /*$.get("Communications/getPosition.php", function(data) {
        //console.log(data);
        var pos = jQuery.parseJSON(data);
        newMeshes[0].position.x = pos.posx;
        newMeshes[0].position.z = pos.posy;
        newMeshes[0].position.y = 0;
    });*/

    //};


    });

};

}


Solution

  • Change unit.prototype.setMesh(...) to this.setMesh(...). Then read up on prototype inheritance.