Jamascript Bracket Reference();
Author: | Kramy
|
Submitted: | 30th May, 2004
|
Views: | 5164
| Rated: |
|
|
Jamascript Bracket Reference();
Ok, recently I've been quite horrified at some of the awful coding I've seen. This article aims to be a quick reference to Jamascript brackets in Functions. It's especially useful to anyone that can't remember where brackets go, or when to use them. The examples are uncommented, but have captions beneath them. You can insert any of them into Jamagic(with a while loop after them) to see them run.
*Events, Functions, and Objects are not explained in this article. Only their proper bracket usage.*
--Functions--
Var Beans = 10;
EatBeans(4);
Function EatBeans(pNum)
{
Beans -= pNum;
}
Brackets used in the function to list parameters, and when calling the function.
Var Peas = 10;
EatPeas();
Function EatPeas()
{
Peas -= 5;
}
No parameters. Brackets still included in the function and when calling it.
--Library Functions--
myWindow = New Window(640,480);
myWorld = New World();
myCamera = New Camera(myWorld,myWindow);
myObject = New Object(myWorld);
Var XAngle = myObject.GetXAngle();
Brackets used for all library functions, even if you can't pass anything to them(because they lack parameters). So in general, all functions you make, and all library functions need brackets.
--Events and Functions--
An event is something called when the user interacts with the program. Examples are clicking the mouse, a button, etc. Events interrupt the the program flow and run, then the program resumes interpretting where it left off.
Var MouseX = 0;
Var MouseY = 0;
myWindow = New Window(640,480);
myWindow.OnMouseDown = MouseDown; // Event and Function called
Function MouseDown(pWin,pMouseX,pMouseY,pMouseBtn) // function
{
If(pWin == myWindow)
{
MouseX = pMouseX;
MouseY = pMouseY;
myWindow.SetText(MouseX+" "+MouseY);
}
}
Brackets used to list function parameters. No brackets used on the event, "OnMouseDown", or the function called. All values provided by the Event. No need to specify them.
--Objects and Functions--
myWindow = New Window(320,240);
myBall = New Ball(5);
myBall.Bounce(12);
Function Ball(pSize)
{
This.Size = pSize; // Ball is now size 5
// Define Function to bounce ball
This.Bounce = Ball_Bounce;
}
Function Ball_Bounce(pHeight)
{
myWindow.SetText("Ball Bounces "+pHeight+" pixels high.");
}
Here we can observe several things. The keyword, "This" is used to specify data to remain attatched to the object. If we wanted we could call "myBall2 = New Ball(7);" and myBall would remain size 5.
Second, the brackets. The function Ball_Bounce has brackets that have a parameter in them. This function is then made into an internal function provided to the object(ball). No brackets are used when defining the function. Finally, the Ball.Bounce function is called, and a value is provided inside brackets.
You should also note that I have been using "p-something" for parameters. It is considered good Jamascript coding to put the p in to indicate it is a parameter. That way you can't get Jamagic confused it if encounters MouseX(variable) and MouseX(Parameter).
--Objects and Creation Flags--
// aWindow = New Window(width,height,x,y,backcolor,depth,caption,Menu,parentwindow,style,flags);
myWindow = New Window(640,480,0,0,0x000000,15,"Creating a Window",Window.NOSURFACE);
myChild = New Window(480,myWindow.GetPixelHeight(),0,0,0xFFFFFF,15,myWindow,Window.GAME);
This creates a draggable window with a child-game inside it. Brackets are not used when indicating a flag or style. Flags generally use the "type-of-object.flag" format, as in "Window.Game" for windows, or "Edit.MULTILINE" for an editbox.
The library function myWindow.GetPixelHeight(), which is used when creating the child window, has brackets.
Well, that's all the situations I can think up at the moment. I hope you learned something(or already knew this ), and keep this around as a quick-reference if you forget lots. Next I'll probably go into more detail about objects, so you don't have to figure things out with trial-and-error.
-Kramy
|
|
Kramy
Registered 08/06/2002
Points 1888
|