» »

[JavaScript] Spreminjanje zunanje spremenljivke

[JavaScript] Spreminjanje zunanje spremenljivke

Tineee ::

Pozdravljeni

Zanima me, če je možno sledeče.
Rad bi spreminjal spremenljivko "color" v datoteki Virus.js:8 iz datoteke PlayerCommands.js. V PlayerCommands.js:51 se začne moja funkcija, kjer uporabnik vpiše barvo. Ta del deluje in pri izpisu na konzolo (PlayerCommands.js:72) vidim barvo, ki jo je uporabnik vnesel. Sedaj bi pa rad to vrednost skopiral v spremenljivko v datoteki Virus.js:8.
Skopiral sem začetke kode iz obeh dokumentov in pa moj del kode v PlayerCommands.js. Dokumenta se nahajata v različnih direktorijih.

Upam, da je vprašanje razumljivo napisano, ker nisem programer.



Virus.js
var Cell = require('./Cell');

function Virus() {
    Cell.apply(this, Array.prototype.slice.call(arguments));
    this.cellType = 2;
    this.isSpiked = true;
    this.isMotherCell = false; // Not to confuse bots
    this.color = {
        r: 0x33,
        g: 0xff,
        b: 0x33
    };
}

module.exports = Virus;
Virus.prototype = new Cell();

// Main Functions

Virus.prototype.canEat = function(cell) {
    // cannot eat if virusMaxAmount is reached
    if (this.gameServer.nodesVirus.length < this.gameServer.config.virusMaxAmount)
        return cell.cellType == 3; // virus can eat ejected mass only
};

Virus.prototype.onEat = function(prey) {
    // Called to eat prey cell
    this.setSize(Math.sqrt(this.radius + prey.radius));

    if (this._size >= this.gameServer.config.virusMaxSize) {
        this.setSize(this.gameServer.config.virusMinSize); // Reset mass
        this.gameServer.shootVirus(this, prey.boostDirection.angle());
    }
};
.
.
.


PlayerCommands.js
var Logger = require('./Logger');
var UserRoleEnum = require("../enum/UserRoleEnum");

function PlayerCommand(gameServer, playerTracker) {
    this.gameServer = gameServer;
    this.playerTracker = playerTracker;
}

module.exports = PlayerCommand;

PlayerCommand.prototype.writeLine = function (text) {
    this.gameServer.sendChatMessage(null, this.playerTracker, text);
};

PlayerCommand.prototype.executeCommandLine = function (commandLine) {
    if (!commandLine) return;

    // Splits the string
    var args = commandLine.split(" ");

    // Process the first string value
    var first = args[0].toLowerCase();

    // Get command function
    var execute = playerCommands[first];
    if (typeof execute != 'undefined') {
        execute.bind(this)(args);
    } else {
        this.writeLine("ERROR: Unknown command, type /help for command list");
    }
};


PlayerCommand.prototype.userLogin = function (ip, password) {
    if (!password) return null;
    password = password.trim();
    if (!password) return null;
    for (var i = 0; i < this.gameServer.userList.length; i++) {
        var user = this.gameServer.userList[i];
        if (user.password != password)
            continue;
        if (user.ip && user.ip != ip && user.ip != "*") // * - means any IP
            continue;
        return user;
    }
    return null;
};


var playerCommands = {
    id: function (args) {
        this.writeLine("Your PlayerID is " + this.playerTracker.pID);
    },
    vcolor: function (args) {
        // Get colors
        var color = {
            r: 0,
            g: 0,
            b: 0
        };

        if (isNaN(args[1]) || isNaN(args[2]) || isNaN(args[3])) {
            this.writeLine("ERROR: missing color argument!");
            return;
        }

        color.r = Math.max(Math.min(parseInt(args[1]), 255), 0);
        color.g = Math.max(Math.min(parseInt(args[2]), 255), 0);
        color.b = Math.max(Math.min(parseInt(args[3]), 255), 0);

        console.log(color.r + " " + color.g + " " + color.b );  
        this.writeLine("Vcolor set to " + this.playerTracker._name + " to " + color.r + " " + color.g + " " + color.b);

    }
};

  • spremenilo: Tineee ()

swdev ::

Najprej exportaj objekt/funkcijo (v PlayerCommands.js), ki jo želiš uporabiti drugje. Nato pa ta objekt/funkcijo importaj v Virus.js.

Več o tem (Javascript ES6): https://hackernoon.com/import-export-de...

Tineee ::

Malo sem dopolnil kodo.

swdev
Hvala za pomoč, vendar enostavno kodo bi še znal spreminjati (kar sem že tudi naredil), tukaj je pa moja funkcija znotraj spremenljivke "var playerCommands" in ne vem kako nasloviti samo to mojo funkcijo. In tudi ne vem kako nasloviti spremenljivko, ki jo želim spreminjati.

V glavnem, prosim če je za koga zadeva zelo enostavna, če bi lahko napisal rešitev, ker jaz lahko cel teden študiram javo script pa ne bom ugotovil zadeve.

Hvala

P.S.
Ne učim se programiranja. Koda ni moja. To je samo stvar, ki bi jo rad dodal temu programu.

Zgodovina sprememb…

  • spremenilo: Tineee ()

swdev ::

Ena dummy delujoča rešitev:

Virus.js
https://es6console.com/jeyj6w2g/

PlayerCommands.js
https://es6console.com/jeyj7kro/

Na tem primeru se ti vrednost colorja iz PlayerCommand prepiše v color v Virus.

Zgodovina sprememb…

  • spremenilo: swdev ()

Tineee ::

Najlepša hvala za trud, ampak zadeva očitno ni tako enostavna kot sem upal ;((

Ko zaženem program mi javi sledečo napako: slika

V izpisu omenja to datoteko:

index.js
module.exports = {
    Cell: require('./Cell'),
    PlayerCell: require('./PlayerCell'),
    Food: require('./Food'),
    Virus: require('./Virus'),
    MotherCell: require('./MotherCell'),
    EjectedMass: require('./EjectedMass'),
};


Rešeno 8-)

Zgodovina sprememb…

  • spremenilo: Tineee ()


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

[javaScript] Preverjanje formata zapisa EMŠO

Oddelek: Programiranje
132802 (2422) win64
»

Razvrščanje števil v c#

Oddelek: Programiranje
61306 (1226) urosp
»

Javascript

Oddelek: Programiranje
61117 (930) amacar
»

[Visual C#] TableAdapter

Oddelek: Programiranje
61042 (918) detroit
»

Valve & STEAM & On-Line gaming (strani: 1 2 )

Oddelek: Igre
717918 (6340) Tilen

Več podobnih tem