Skip to main content

Making Classes..

A dip into JavaScript.

Before I begin. var is a variable. You need to declare your variables before you start writing code so the code will execute properly. Declaring a function goes like this:

function Update(){
//Insert your code here....
}



Anything between {} would be executed when the function was called. The brackets need to all be closed by the end of the script or it will not execute properly
You usually want to end each line of code that's not declaring a function, class, or if statement with ;. So it could look something like this

function Update(){
transform.Translate(0, 0, 1);
}

You can insert little comments that you can read to keep up with your script and are not executed by beginning with //. So in the end...

//moving the object
function Update(){
transform.Translate(0, 0, 1);
}

Two more side notes... The transform is the object that the script is attached to. The Update function is called every frame that is rendered. Which for my little e machine computer, is around 35 times a second.

So I made a JavaScript for keeping up with the levels of the character. I'll explain it as best as I can.
Heres the code:


class BendingCapability {


var experience = 0.0;
var minimumExperience = 100;
var name = "";
var level = 0;


//getting name..
function BendingCapability(name) {
this.name  = name;
}

//debugging purposes
function getName() {
return name;
}


//checking exprience, leveling up.
function updateLevel() {
if(check()) {
minimumExperience += minimumExperience * 1.8;
level = level + 1;
}
}

//checking experience
function check() {
return experience >= minimumExperience;
}


}


//declaring different classes
var earthManipulation = new BendingCapability("Earth");
var fireManipulation = new BendingCapability("Fire");
var waterManipulation = new BendingCapability("Water");
var electricManipulation = new BendingCapability("Electric");
var airManipulation = new BendingCapability("Air");


//checking level up..
function Update(){

earthManipulation.updateLevel();
fireManipulation.updateLevel();
waterManipulation.updateLevel();
electricManipulation.updateLevel();
airManipulation.updateLevel();


}


So In the beginning I declared a class and named is BendingCapability. (Note:In JavaScript you need to make sure that you match up capitalization, spelling, spacing, and so on...) Creating the Class is almost like creating a template for each Level category to follow. I created several functions here that make up the template. But I first started with declaring the variables. Experience is how much experience the player has with the level. minimumExperience is how much experience is needed to level up. The name is just simply the name of the level. and level is the level of level category (earth, fire, ect.) So the function BendingCapability(name) got the name of the level. To avoid.. or lessen the confusion I will start calling it skill. Ignore the debugging purposses for now. The function updateLevel is called to see if the required needs are met to level up.

So...

function updateLevel(){
       if(check()){

The function has a nested if statement. The if requirements are called within the (). In this case it's calling a function further down the script.

function check(){
        return experience >= minimumExperience;
}

All this does is see if the experience is greater than or equal to the minimum Experience required to level up, and if it is, it returns it true.

So if the check returns true, then:

minimumExperience += minimumExperience * 1.8;
level = level +1;

This adds a level (level up) to the skill that  met the required experience. Then we added the minimumExperince to the existing minimum experience so it would not loop and continue to level up. The we multiplied whats being added to it by 1.8 so you would have to gather alittle more experience than you did last time to level up again.

Now to declare a skill with the class you say...

var earthManipulation = new BendingCapability("Earth");

what this does is creates a whole class just for Earth Manipulation. Which what we called earth.
the type of var = BendingCapability, which is a class.

Then lastly the Update function....

function Update(){
      earthManipulation.updateLevel;
}

Remember that the update function is called every frame. We say earthManipulation to look at its class, then within the class we call upon its updateLevel function. Which calls upon the check function to see if the skill has met the required experience to level up, and if it has, It executes the rest of the updateLevel function.

Very long I know.

Comment what you think, be a critic. I need those before I can have fans. Sorry I'm new to javascript so i might not be doing things the best they could be done. Sudgest or complain. correct me. Anything to make this blog better

Comments

Popular posts from this blog

How To Import and Use Fonts

-Made by Chrome Fx Films

To use different fonts in your game, your going to have to acquire some fonts. You can go somewhere like 1001 free fonts and download ones you like and want to use.

When you download the file, you'll need to extract the file if its in  a .zip

The text file should have the extension .ttf. If you text files have a FFIL extension, simply rename the font file (yourname.ttf).
Drag the text file into unity to import. By default the Character variable should be labeled Dynamic.

The Dynamic setting means  that Unity won't pre-generate the texture, so if you look at your material containing your font, It probably will be blank. (read more here)
Now your over all objective should be getting your font to look like this (unless you prefer dynamic):
Where the characters of the font are not jumbled up and visible.
Now usually all you have to do to achieve this is change the Character variable from Dynamic to Unicode.
and that should work.
Now what some people over…

Handling Music and Sound Effects In Your Games

Initiative  While developing Treva's Adventure I had to figure out a way to handle multiple music tracks and sound effects in a clean manner or suffer horribly.  What was going to help me achieve a simple solution was taking all the different sounds and centralizing them in a single class in order to black box them.  Any other code trying to play a sound wouldn't even know the sound file's name.  All code trying to play a music track would reference a enum that defines all the track names.
Defining The Class Creating The Enum When I first started defining types in my enumeration,  I was naming the types to be exactly like the file name.  For a scary sound effect I had found a file named "ghost breath".  So around my code would be scattered lines like SoundManager.Play(SoundEffectType.GhostBreath);  This was fine until I found a sound that better fit the situation it was being used in,  and decided to use "ghost breath" for a different situation like a …

Don't Destroy On Load..

So if you want to keep an object or script that keeps up variables (or for any other reason) when you go from scene to scene, you need to attach a don't destroy on load static function, which goes something like this:

function Awake () {
    DontDestroyOnLoad (transform.gameObject);
}

The Awake function is call only once, when all the objects in the scene have been created. Read more about it here.

DontDestroyOnLoad has what ever is in the ( ) to not be destroyed when creating a new scene.

(transform.gameObject) is what will not be destroyed when the new scene is loaded, in this case, it will be the game object and all it's children the script is attached to.