Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems in developing a super simple js framework as an exercise with objects

I'm sure my question will be relatively simple to answer, but as a beginner I'm really struggling.

So I've learned (at least I think i have) from a variety of sources the basics of Javascript, however I'm really struggling with objects, classes and syntax. I completely understand the concept, I've been playing around with various programming languages at basic levels for years, however, I'm struggling in implementing what I've learned in JS.

Basically I'm attempting to develop, as an exercise to prove my learnings a simple framework to make visual novel games. So my entire framework can be simplified to two basic objects, a scene and a character. So I'm attempting to flesh out my character object like so:

function character (race,gender,age)
{
    this.race = race;
    this.gender = gender;
    this.age = age;
}

//lets make an elf race based on the character prototype
var elfRace = new character("elf","m",100);

Basically my objective is to accomplish the following hierarchy

Character

Race 1 Race 2 Race 3

ActualChar ActualChar ActualChar ActualChar ActualChar

so my final character will derive from the defined Race, which will derive from the character object.

Now my question is, I cant figure out how to set the Race to be derived from the Character. Do I declare character as the proto for Race? or do I

var newrace = new Character();

and how do I add additional "skills" or "parameters" to the races? so an elf race should have text: +5 and a demon race strength: +5, but these parameters should only be added at the race object level.

I've tried to keep it as to the point and brief as I can, but if you want more details about what I'm attempting to do, I've written a blog post about it too, if more details are required at: http://www.endangeredpixel.com/

hope I can get some help on the issue, I've tried researching more on objects, hierarchy and prototypes, but I either get overly simplistic explanations and tutorials, or rants, rambles and extremely advanced tutorials beyond my skill level. I'm struggling to find explanations here at the intermediate stage :(.

like image 544
Sharan Balani Avatar asked Dec 21 '25 06:12

Sharan Balani


1 Answers

Well you could do something like this.. Although you can make less or even more constructors depending how you prefer. Now I'm using Character, Race, and Player (actual character). You could use different Race constructors like ElfRace, DemonRace but for something simple like 2 races you could just use one like I did. This is the general idea and you can modify it to fit your specific needs.

function Character(gender, age) {
    this.gender = gender;
    this.age = age;
}

function Race(gender, age, race) {
    Character.call(this, gender, age); //inherit from Character
    this.race = race;
    if (race == 'elf') {
        this.dexterity = 5;
    } else if (race == 'demon') {
        this.strength = 5;   
    }; // you could check for more races here.
}

function Player(name, age, gender, race) {
    Race.call(this, gender, age, race); //inherit from Race
    this.name = name;
}

var player1 = new Player("Legolas", 100, "m", "elf");
alert(player1.name); //Legolas
alert(player1.age); //100 years old
alert(player1.gender); //m
alert(player1.race); //elf
alert(player1.dexterity); //5
alert(player1.strength); // undefined because elf's have dexterity not strength

In this example I created a player1 with the name of Legolas, age of 100, male and a race of elf. Here is also a jsfiddle

If you want to add more races then you have to update the if statement in the Race constructor to assign the skills for the specific race you want to add.

like image 91
Pete D Avatar answered Dec 22 '25 20:12

Pete D