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

Making A Laser Pointer

Want a frieking lazer pointer? BOOM. Attach a Line Renderer component to the object you have the script below attached to. (Component> Miscellaneous>Line Renderer) Code: function Update () { var lineRenderer : LineRenderer = GetComponent(LineRenderer); lineRenderer.useWorldSpace = false; lineRenderer.SetVertexCount(2); var hit : RaycastHit; Physics.Raycast(transform.position,transform.forward,hit); if(hit.collider){ lineRenderer.SetPosition(1,Vector3(0,0,hit.distance)); } else{ lineRenderer.SetPosition(1,Vector3(0,0,5000)); } } @script RequireComponent(LineRenderer) Thank  3dDude for the script. Original Source here .

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 scene. They are an component, so it mu