Gw Temp

Menu

Tutorial - 'Sphere JavaScript Cheat Sheet' by e_machinist

An item about Sphere posted on

Blurb

More like a general JavaScript tutorial, but aimed for new Sphere scripters, hence it's posted at Sphere tutorials section. Nice reference, check it out starters.

Body

Javascript Cheat Sheet
For new Sphere scripters
by MrFury
EMAIL: e_machinist@yahoo.com


This is for you to print out and use as a reference when scripting in sphere ( or any implementation of JavaScript I suppose, as nothing here strictly adheres to sphere)



JavaScript syntax

Every procedure in Javascript is a function.

function functionName(argument1, argument2) {
statement 1;
statement 2;
.
.
.
statement n;
}

Javascript is case sensitive, meaning that index, and INDEX are not the same.

In Javascript you indicate a one-line comment with two slashes:
// Comments go here
Javascript will ignore everything on the line, after the two slashes.

You can also have a comment that spans multiple lines:
/* Comments
Comments
Comments
*/

JavaScript Variables

Naming Conventions to follow when naming variables in JavaScript:
- The name must start with an alphabetic character (a-z, A-Z) or an Underscore ( _ )

- The rest of the name can contain any letter, any digit, or an underscore

- A variable name cannot contain a space or any punctuation marks other than an underscore ( _ )

- Avoid using Javascript reserved words as variable names, reserved words include:

abstract boolean break byte case
catch char class const continue
debugger default delete do double
else enum export extends false
final finally float for function
goto if implements import in
instanceof int interface long native
new null package private protected
public return short static super
switch synchronized this throw throws
transient true try typeof var
void volatile while with

Variables may also be Global or just for the use of a specific function. This is easy to determine. If your variables are declared outside of a function, then they may be used by any and all functions that would require them. However, if the variable is declared inside of a function, then it is for use only in that function.

Creating Variables

It is always a good practice to declare your variables, sometimes you can get away with not declaring your variables, especially in JavaScript, but you should always make it a habit to do so. Declare a variable with the var declaration:

var x = 10

JavaScript is what we call a Loosely Typed language meaning that you don't really need to specify the type of data your variable is going to hold. If you are used to some other programming languages or other scripting languages such as Visual Basic or VBScript you will notice that they declare a variable and then proceed to say an "... as Integer". That means that that variable in VB will only be able to contain a number that adheres to the rules for qualifying as an integer. But in JavaScript remember, we are a loosely typed language meaning that JavaScript will actually divine for us , if you will, what data type the variable is. There are three types of variables in JavaScript:

- Numeric: which contain numbers

- String: Which contains text between quotation marks ( " " )

- Boolean: Which can contain one of two values, true or false

So, a practical example would be:

var heroFirstName = "Matt";
var heroLastName = "Jones";
var entireName;
entireName = heroFirstName + heroLastName

This example would result in "MattJones" which isnt exactly how we would want it to go, so you could either create another variable with a " " as the value like

var nameSpace = " ";
and then:
entireName = heroFirstName + nameSpace + heroLastName
resulting in:
Matt Jones

Or you could do a concatenation of + " " + which would enter a space between the two variables

entireName = heroFirstName + " " + heroLastName

*Concatenation: is the combining or linking of strings. Where in a numeric situation it would add the two numbers, in this case the "+" operator will put the two or more strings together

Creating Objects

One of the nicest things about JavaScript is that it allows you to create new objects. I will only very briefly describe this process here, but maybe in another tutorial I will describe it better.

var variableName = new ObjectName;

A practicle way to actually make the template, if you will, for a new object would be something to the tune of this:

function Item(Name, Count)
{
this.Name = Name;
this.Count = Count;
}

You could then at this point make a new item:

var Mana = new Item("Mana", 1);

This would create a new Item according to the Item object you designed above. Simple!

Creating Arrays

If you don't want to create a gazillion items by declaring a new variable every single time, or make each and every character individually, you may choose to design an array structure for them, this would look like this:

var heroNames = new Array(); //You could also put a number in (...) if you know the exact number of how many players there are (3) if there are four for example
heroNames [0] = "Shikaka";
heroNames [1] = "Reaper";
heroNames [2] = "Killah";
//etc . . . .

That method above is pretty much the most common method employed, however you could also do a "Player.push = "Shikaka" etc, and that would just keep pushing each new entry into the array to the end of the list. Or you could do " var heroNames = new Array( "Shikaka", "Reaper", "Killah")

JavaScript Operators

Operators are used for comparisons, logical operations, and arithmetic.

Operator What it does

== Equal To

! = Inequal to

< Less Than

> Greater Than

<= Less than or equal to

=> Greater than or equal to

! NOT

&& AND

| | OR

+ Addition

- Subtraction

* Multiplication

/ Division

% Modulus

++ Increment

- - Decrement

- Negate

Here are some examples of how some javascript operators work:

Statement What it does

x += y x = x + y

x -= y x = x - y

x *= y x = x * y

x /= y x = x / y

x %= y x = x % y

x |= y x = x | y

x &= y x = x & y

x++ x increment

x -- x decrement

Program Flow

In other words, ways to choose alternatives to what can happen...

Switch

Switch allows you to make an expression, and then you would have cases ( or possible alternatives) for different values, here is the way you would write it out:

switch (keysCollected)
{
case "GoldKey":
statements;
break;
case "RedKey" :
statements;
break;
.
.
.
default:
statements;
break;
}

You should sometimes include a default to occur in case none of the values matches one of the indicated case values. Also you MUST include a break in every single case you make. Why? Well, if you don't your switch expression will run through the entire expression and do everything inside of every case regardless of if the value is an indicated value or not. The case value will be a match, and then the program will run through the statements and then hit the break, making it leave the switch and go elsewhere.

if

If is pretty straight forward:

if (conditional expression) {
statement;
statement;
}

In this example, if the conditional expression is evaluated as true, then the following statements will be executed, if not ... then nothing happens.

Else

Else extends the normal if statement by giving the conditional expression something to do if the statement is false, instead of doing nothing, you could give it something spiffy to do until the if condition is met:

if (conditional expression) {
statement;
statement;
}
else {
statement;
statement;
}

else if

Else If is usefull if you need multiple elses, you probably wont use these right away, but will in the future:

if (conditional expression) {
statement;
statement;
}
else if {
statement;
statement;
}
else if {
statement;
statement;
}

This if statement will keep running down through the statements until it either exits the list, or finds a statement to return the correct value

Making code Loop (or Repeat)

for

For is the most fundamental way to repeat a section of code. It looks like this:

for (starting expression or variable; condition to be met; way in which to update expression) {
code that will be repeated as long as the condition to be met is true
}

An example with some substance:
for ( i = 1; i < 20; i++) {
statements;
}

This will keep looping as long as i is less than 20, and the i++ means that the i variable will be incremented by 1 each time the loop is executed

while

Another loop, from the wording you should be able to get it:

while (conditional expression) {
statements;
}

In this loop, say the conditional expression is i < 20 again, well this expressions statements will keep repeating until its not true any longer, it would look briefly like this:

while (i < 20) {
statements;
}

do while

I consider this the odd ball loop of the bunch, but quite useful. You would use this one if you want the code to execute at least once for sure, and then keep repeating depending on the while statement which comes at the end of the script:

do {
statements;
}
while (conditional expression)

What this does is the do loop performs its statements once, then it passes control to the while. If the while conditional expression is true, then while will pass control back to the do again. Get it? do will happen once nomatter what, and then loop until the while condition is false. Like I said, this is kinda the odd ball of the loop crowd, but still quite useful.

Also, all loops may be quickly killed if you do a break; inside of the statements, and you may also do a continue; which will advance to another loop etc.

Well, this pretty much wraps up the basics to javascript. I really didn't go into any Sphere specifics like drawText and attachInput and all that good stuff. You'll have to figure that out on your own, but honestly, once you know Javascript, its really not very hard at all to start using the sphere implemented stuff available to you. Also, I didn't write this to teach JavaScript ( I suppose you could learn from this... lol) But this was written so that you could PRINT IT OUT, AND USE IT TO REFER TO OCCASIONALLY when you need to remember exactly how do I word a loop!! I hope this is beneficial, and if it is I would LOVE to have you e-mail me about whether it was useful. If it was then I will proceed to make some more cheat sheets and sphere tutorials. Also, keep in mind this is my first day of JavaScript ( I usually program in other languages) and if you find any wrong doings (misleading stuff only, no typos as those are easily figured out).

E-MAIL: e_machinist@yahoo.com or imac_dvguy@yahoo.com, I check both every day, and will get to you as soon as possible.