Reputation: 3
I'm new to javascript so I figured I would starts something completely out of my league.(Trial by fire) I'm working on a texted based combat system for a game using javascript. I'm trying to reference an object property from a function.
I've tried including the document.getElementById withing the object property and referencing it as a function with the object.
This is the first set I tried
function randomNumber(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
var creature = {
name: "Creature",
description: "Discription goes here.",
health: 20,
damage: document.getElementById('damageObject').innerHTML = randomNumber(4,8)
}
and this is the second
var creature = {
name: "Creature",
description: "Discription goes here.",
health: 20,
damage: creatureDamage(3,7)
}
function creatureDamage(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
In both cases I'm trying to create a property that can change the range of values in a Math.random based function. ex(max * min)
In the first one, I was trying to create the object "creature" and create the properties: name, description, health, and damage. Within the damage property, I was trying to provide information to the function randomNumber. No information is pulled and I'm not sure what to add to reference the information needed.
In the second one, I tried creating the function creatureDamge and referencing it within the damage property. This was to create a specific function for that specific creature instead of trying to create a function that I could use across multiple different creatures. When running the second one, it was still not pulling the information to the function from the object.
Upvotes: 0
Views: 107
Reputation: 92440
It sounds like you are looking for either a property that points to a function, which you would call with:
creature.damage()
var creature = {
name: "Creature",
description: "Discription goes here.",
health: 20,
damage(min, max){
return Math.floor(Math.random() * (max - min)) + min;
}
}
console.log(creature.damage(1, 100))
console.log(creature.damage(1, 100))
or a getter that calls the function anytime you try to access the property with creature.damage
. For example:
var creature = {
name: "Creature",
description: "Discription goes here.",
health: 20,
get damage(){
// get min and max from somewhere
return creatureDamage(1,100)
}
}
function creatureDamage(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
console.log(creature.damage) // random values
console.log(creature.damage)
Upvotes: 1
Reputation: 707
Just so you know, when you do this:
var creature = {
damage: creatureDamage(3,7)
}
The function creatureDamage is executed on the spot, with parameters 3 and 7, and the result is given to the damage property of your object. I don't think that's what you want. You probably want this function to be executed every time your creature does an attack, so the result may vary. In that case, you should create your object like this:
var creature = {
min_damage: 3,
max_damage: 7,
}
Then during the attack, run this:
damage_given = creatureDamage(creature.min_damage , creature.max_damage);
If you want the function called to be different from creature to creature, you could do something like this
function basicDamage(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
function extendedDamage(min, max) {
//the +9 is just something silly to make it different
return Math.floor(Math.random() * (max - min)) + min + 9;
}
var creature1 = {
min_damage: 3,
max_damage: 7,
damage_function = basicDamage
}
var creature2 = {
min_damage: 3,
max_damage: 7,
damage_function = extendedDamage
}
var damage1 = creature1.damage_function(creature1.min_damage, creature1.max_damage);
var damage2 = creature2.damage_function(creature2.min_damage, creature2.max_damage);
Since you assign "basic_damage" without the () to your damage_function, the function is not executed when you create your object. So the damage_function property points to your function, which you can then call later with your parameters. You could clean that code up a bit like this.
function get_damage(creature)
{
var damage = creature.damage_function(creature.min_damage, creature.max_damage);
return damage;
}
var damage1 = get_damage(creature1);
var damage2 = get_damage(creature2);
Or go a bit further so that you can run any damage function as long as your creature has the properties the function wants to use
basicDamage(creature) {
return Math.floor(Math.random() * (creature.max_damage - creature.min_damage)) + creature.min_damage;
}
function extendedDamage(creature) {
//now this function use an extra property which your creature needs to have
return Math.floor(Math.random() * (creature.max_damage - creature.min_damage)) + creature.min_damage + creature.extra_damage;
}
var creature1 = {
min_damage: 3,
max_damage: 7,
damage_function = basicDamage
}
var creature2 = {
min_damage: 3,
max_damage: 7,
extra_damage: 7,
damage_function = extendedDamage
}
function get_damage(creature)
{
var damage = creature.damage_function(creature);
return damage;
}
var damage1 = get_damage(creature1);
var damage2 = get_damage(creature2);
Upvotes: 0