0

下午好。乍一看,这个问题似乎已经回答了,但我无法用我找到的答案解决我的问题。

我有一个类,其中一个函数应该调用另一个函数(传递“self”网格有参数),这就是问题所在,我在某处丢失了引用,我找不到在哪里。

这是代码,在此先感谢:)

//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;
    });*/

    //};


    });

};

}

4

1 回答 1

1

更改unit.prototype.setMesh(...)this.setMesh(...)。然后阅读原型继承

于 2014-05-19T15:27:09.847 回答