Ich habe vor Kurzem begonnen, an einem textbasierten Rollenspiel in HTML und JS zu arbeiten. Ich habe aus mehreren Gründen eine Liste der verschiedenen Waffen erstellt (und eine Liste der Feinde, obwohl sie derzeit nur eine enthält) (sie befinden sich alle am selben Ort, sind einfacher zu finden usw.) und eine Funktion erstellt, um eine Waffe anhand des Namens aus der Liste auszuwählen. Aber wenn ich die von mir erstellte Funktion ausführe (
), um eine Waffe anhand des Namens aus der Liste abzurufen (zweimal, einmal in der Feindesliste und einmal im Button.AddEventListener), kann die Waffe nicht gefunden werden. Viele Dinge im Code sind unvollständig oder funktionieren nicht richtig, da ich zunächst nur versucht habe, Systeme einzurichten. Ich habe vor, sie später zu reparieren. Ich habe versucht, die Waffe zu wechseln, um zu sehen, ob es ein Problem mit der jeweiligen Waffe war, aber auch das hat nicht funktioniert. Ich vermute, dass es noch andere Probleme mit dem Code gibt, aber mir ist im Moment keins bekannt. Weiß jemand, was passiert?
Code (JS):
const pg = document.getElementById('par');
const input = document.getElementById('input');
const button = document.getElementById('button');
let curr = 0; // Tells game what is happening right now
let pressed = false; // If button is being pressed
const def = { // Starting values for player stats
hp: 100, // Max HP
weapons: [],
luck: 20,
maxLuck: 100,
money: 0,
armor: 0,
}
const p = { // Player stats
inv: { // Inventory
money: 0,
items: [], // Items usable by player
weapons: [], // Unlocked weapons
weapon: null, // Equipped weapon
},
name: '',
hp: 0, // Current hp
luck: 0, // Increases chances of getting better loot and getting critical hits
maxHp: def.hp, // Max hp
armor: 0, // Dampens damage taken
setup() {
this.maxHp = def.hp;
this.hp = def.hp;
this.inv.weapons = def.weapons;
this.inv.weapon = null;
this.inv.money = def.money;
this.luck = def.luck;
this.armor = def.armor;
},
attack(enemy) {
this.weapon.attack(enemy);
},
gain(item) { // Sorts out what to give player when receiving something
if (typeof item == 'number') this.inv.money += item.num;
else if (item instanceof Weapon) this.inv.weapons.push(item);
//else if (item instanceof Item) Haven't added yet
else console.log(`Item: ${item} was passed through p.gain() but did not match anything.`); // [IMPORTANT] I see this in console after the button is pressed to submit the name.
}
};
p.setup();
function weightChoose(loot, luck = 0) { // Chooses an item from a loot pool with weight
const totalWeight = loot.reduce((total, entry) => total + entry[1]);
let rand = Math.random() * totalWeight;
for (let i of loot) {
rand -= luck ? luck - i[1] : i[1]; // Subtract item weight from total (if luck, subtract weight from luck to get new weight)
if (rand attacker.luck / 150) target.hp -= this.damage * 2; // 100 luck = approx. 66% crit chance
else target.hp -= this.damage;
if (returnHit) return true;
} else if (returnHit) return false;
}
}
}
class Enemy {
constructor(maxhp, luck, wep, dmgMult, armor, loot = []) {
this.maxHp = maxhp;
this.hp = this.maxHp;
this.luck = luck; // Affects crit chance (planning to add other stuff)
this.weapon = wep;
this.dmgMult = dmgMult; // Damage multiplier (from enemy)
this.armor = armor; // Reduces amount of damage taken by enemy
this.loot = loot; // Pool of possible loot to get (type as: [[loot, chance], [loot, chance]])
}
attack(target) {
this.weapon.attack(target);
}
}
const weapons = [ // List of weapons
// Melee (basic stuff, not fancy)
new Weapon(2, 100, 'Stick'), // Starter weapon; basically useless
new Weapon(30, 80, 'Sword'),
new Weapon(25, 70, 'Dagger', 3),
new Weapon(50, 55, 'Longsword'),
// Guns (AMERICAAAAAAAAAA)
new Weapon(10, 75, 'Glock'),
new Weapon(8, 15, 'Shotgun', 6),
new Weapon(3, 30, 'Minigun', 20),
new Weapon(10, 20, 'Revolver', 6),
// Special stuff (with abilites)
new Weapon(5, 85, 'Torch', 1, ['fire', 'watervuln']),
new Weapon(10, 10, 'Flamethrower', 10, ['fire', 'watervuln']),
new Weapon(20, 60, 'Shiny Rock', 1, ['confusion']), // Enemy is distracted by rock (sounded better in my head honestly)
new Weapon(3, 80, 'Water Gun', 5, ['water']),
];
function getWeapon(name) { // [IMPORTANT] weapons.find is returning undefined.
const w = weapons.find((w) => w.name == name);
if (w) return w;
else console.log(`Tried to find weapon: ${name} but failed.`);
}
const enemies = [
new Enemy(25, 20, getWeapon('Stick'), 1.1, 1, [[5, 10]]),
];
function write(txt) { // For simplifying display of text because I ain't writing allat
pg.innerHTML = txt;
}
write('Enter your name ^^^');
button.addEventListener('mousedown', () => {
pressed = true;
if (curr === 0) { // Just started game; get name
if (input.value != '') {
p.name = input.value;
button.textContent = 'ok';
curr = 1;
write('You just left Grandma\'s house with a cookie. You found a stick in the woods and thought it looked cool, so you are also carrying it around right now.');
p.gain(getWeapon('Stick'));
}
} else if (curr === 1) { // Story
if (pressed) curr = 2;
}
});
button.addEventListener('mouseup', () => pressed = false);
Ich habe vor Kurzem begonnen, an einem textbasierten Rollenspiel in HTML und JS zu arbeiten. Ich habe aus mehreren Gründen eine Liste der verschiedenen Waffen erstellt (und eine Liste der Feinde, obwohl sie derzeit nur eine enthält) (sie befinden sich alle am selben Ort, sind einfacher zu finden usw.) und eine Funktion erstellt, um eine Waffe anhand des Namens aus der Liste auszuwählen. Aber wenn ich die von mir erstellte Funktion ausführe ([code]getWeapon[/code]), um eine Waffe anhand des Namens aus der Liste abzurufen (zweimal, einmal in der Feindesliste und einmal im Button.AddEventListener), kann die Waffe [url=viewtopic.php?t=22532]nicht gefunden[/url] werden. Viele Dinge im Code sind unvollständig oder funktionieren nicht richtig, da ich zunächst nur versucht habe, Systeme einzurichten. Ich habe vor, sie später zu reparieren. Ich habe versucht, die Waffe zu wechseln, um zu sehen, ob es ein Problem mit der jeweiligen Waffe war, aber auch das hat nicht funktioniert. Ich vermute, dass es noch andere Probleme mit dem Code gibt, aber mir ist im Moment keins bekannt. Weiß jemand, was passiert? Code (JS): [code]const pg = document.getElementById('par'); const input = document.getElementById('input'); const button = document.getElementById('button');
let curr = 0; // Tells game what is happening right now let pressed = false; // If button is being pressed
const def = { // Starting values for player stats hp: 100, // Max HP weapons: [], luck: 20, maxLuck: 100, money: 0, armor: 0, }
const p = { // Player stats inv: { // Inventory money: 0, items: [], // Items usable by player weapons: [], // Unlocked weapons weapon: null, // Equipped weapon }, name: '', hp: 0, // Current hp luck: 0, // Increases chances of getting better loot and getting critical hits maxHp: def.hp, // Max hp armor: 0, // Dampens damage taken
setup() { this.maxHp = def.hp; this.hp = def.hp; this.inv.weapons = def.weapons; this.inv.weapon = null; this.inv.money = def.money; this.luck = def.luck; this.armor = def.armor; }, attack(enemy) { this.weapon.attack(enemy); }, gain(item) { // Sorts out what to give player when receiving something if (typeof item == 'number') this.inv.money += item.num; else if (item instanceof Weapon) this.inv.weapons.push(item); //else if (item instanceof Item) Haven't added yet else console.log(`Item: ${item} was passed through p.gain() but did not match anything.`); // [IMPORTANT] I see this in console after the button is pressed to submit the name. } };
p.setup();
function weightChoose(loot, luck = 0) { // Chooses an item from a loot pool with weight const totalWeight = loot.reduce((total, entry) => total + entry[1]); let rand = Math.random() * totalWeight; for (let i of loot) { rand -= luck ? luck - i[1] : i[1]; // Subtract item weight from total (if luck, subtract weight from luck to get new weight) if (rand attacker.luck / 150) target.hp -= this.damage * 2; // 100 luck = approx. 66% crit chance else target.hp -= this.damage; if (returnHit) return true; } else if (returnHit) return false; } } }
class Enemy { constructor(maxhp, luck, wep, dmgMult, armor, loot = []) { this.maxHp = maxhp; this.hp = this.maxHp; this.luck = luck; // Affects crit chance (planning to add other stuff) this.weapon = wep; this.dmgMult = dmgMult; // Damage multiplier (from enemy) this.armor = armor; // Reduces amount of damage taken by enemy this.loot = loot; // Pool of possible loot to get (type as: [[loot, chance], [loot, chance]]) } attack(target) { this.weapon.attack(target); } }
const weapons = [ // List of weapons
// Melee (basic stuff, not fancy)
new Weapon(2, 100, 'Stick'), // Starter weapon; basically useless new Weapon(30, 80, 'Sword'), new Weapon(25, 70, 'Dagger', 3), new Weapon(50, 55, 'Longsword'),
// Guns (AMERICAAAAAAAAAA)
new Weapon(10, 75, 'Glock'), new Weapon(8, 15, 'Shotgun', 6), new Weapon(3, 30, 'Minigun', 20), new Weapon(10, 20, 'Revolver', 6),
// Special stuff (with abilites)
new Weapon(5, 85, 'Torch', 1, ['fire', 'watervuln']), new Weapon(10, 10, 'Flamethrower', 10, ['fire', 'watervuln']), new Weapon(20, 60, 'Shiny Rock', 1, ['confusion']), // Enemy is distracted by rock (sounded better in my head honestly) new Weapon(3, 80, 'Water Gun', 5, ['water']), ];
function getWeapon(name) { // [IMPORTANT] weapons.find is returning undefined. const w = weapons.find((w) => w.name == name); if (w) return w; else console.log(`Tried to find weapon: ${name} but failed.`); }
function write(txt) { // For simplifying display of text because I ain't writing allat pg.innerHTML = txt; }
write('Enter your name ^^^');
button.addEventListener('mousedown', () => { pressed = true; if (curr === 0) { // Just started game; get name if (input.value != '') { p.name = input.value; button.textContent = 'ok'; curr = 1; write('You just left Grandma\'s house with a cookie. You found a stick in the woods and thought it looked cool, so you are also carrying it around right now.'); p.gain(getWeapon('Stick')); } } else if (curr === 1) { // Story if (pressed) curr = 2; } }); button.addEventListener('mouseup', () => pressed = false); [/code] Code (HTML): [code]Input
Ich habe vor Kurzem begonnen, an einem textbasierten Rollenspiel in HTML und JS zu arbeiten. Ich habe aus mehreren Gründen eine Liste der verschiedenen Waffen erstellt (und eine Liste der Feinde,...
Ich versuche, eine Automatisierung zu erstellen, die die Anmeldung bei ermöglicht, erhalte jedoch weiterhin die folgende Fehlermeldung, da der Code das Anmeldeelement nicht finden kann....
Ich versuche, eine Automatisierung zu erstellen, die die Anmeldung bei ermöglicht, erhalte jedoch weiterhin die folgende Fehlermeldung, da der Code das Anmeldeelement nicht finden kann....
Ich versuche, eine Automatisierung zu erstellen, die die Anmeldung bei ermöglicht, erhalte jedoch weiterhin die folgende Fehlermeldung, da der Code das Anmeldeelement nicht finden kann....
Ich versuche, eine Automatisierung zu erstellen, die die Anmeldung bei ermöglicht, erhalte jedoch weiterhin die folgende Fehlermeldung, da der Code das Anmeldeelement nicht finden kann....