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 Make a Hellish Looking Sky Box

I came across this problem while constructing my scene of Hell in a little project I've been working on, and could not find a reasonable sky box on the web for what I want. Maybe I was not looking hard enough, but I ended up making nice substitute. If you think the sky box looks familiar, then your right. The Sky box I'm using is already packaged with Unity3D! To import the sky boxes Unity has made for you,  simply go to Assets>Import Package>Skyboxes.  The sky boxes will appear in your projects tab under a folder named "Standard Assets". To make this sky box, first you must find the folder containing all the sky box materials and open it up. In it will be a list of sky boxes for your disposal. To get this skybox, I decided to tweak the "StarryNight Skybox" (But the "MoonShine Skybox" looks pretty cool also!).  Select the sky box and view it under the inspector tab. Underneath the properties there will be a tint color variable allowin...

How To Make A Gun Shot Sound (SFX On Unity 3D)

When it comes to audio in Unity, there are four components: Audio Clip , Audio Source , Audio Listener , and Audio Re-verb Zone . Audio Clips are the actual audio file imported into your game. Unity supports file formats: .aif, .wav, .mp3, and .ogg. When imported, you can compress them greatly, with the price of loosing some quality. You can do this by first selecting the audio clip, view it in the inspector. Under the Audio Importer component, you can switch the audio format from Native to the audio clip, to a compressed format applied by Unity. You can change how compressed the file is by dragging the bar at the bottom, then hitting apply. You can get plenty of free good SFX from a site called  freesound.org . All you have to do is create an account for free , and download all the sounds you want. I found a nice gun shot sound here . Simply download and load into your Project. Audio Source actually plays the audio clip in your scen...

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...