Explanation:
- Code: Select all
(function () {
...
}());
Wrap all the code within your own scope.
This way your variable names won't collide with any other variable names within the same page.
- Code: Select all
'use strict';
Use the javascript strict mode. Not nessecairy, but makes catching errors in your script easier.
Full explanation:
http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/.
- Code: Select all
// Create a DOM element for displaying the username
// This element can be styled with CSS
var $userName = $('<div class="user"></div>');
Creates a DOM element (I.E. some custom html in the game) and saves it into the variable $userName.
This code is executed once while the game is starting. At the moment of creation its not attached to the webpage (we'll do this later).
It uses
jQuery ($) to create the element.
- Code: Select all
// When the game is loaded
WBA.on('init', null, function () {
// Add the username to the stats element
WBA.getDomElement('stats').show().append($userName);
});
The WBA object is the game engine. You can interact with this object to create custom behaviour.
The WBO.on() function listens to changes in the engine. In this case it listens to the 'init' (first parameter) event, which is fired when WBA is ready to customize.
The null parameter are the options, but its not used on the init event.
The last parameter is a function itself. This is the function that is executed when WBA sends the init signal.
Now we ask WBA to get the dom element named 'stats'. Its an element in the top-left corner (default, can be changed with css).
Next we show the 'stats' portion of the page to the user (it's default hidden, because there no stats visible by default)
Next we append $userName (your own username div, which is empty thus still not visible)
- Code: Select all
WBA.on('varChange', 'user', function () {
// When the username changes display the name in the DOM element
$userName.text(WBA.readVar('user'));
});
Next, we need to listen to changes in the username, and display the changed in the username to the player.
Again we use the WBA.on function, but this time we listen to the 'varChange' event.
This time we only want to listen if the 'user' variable changes. Were not interested in the other variables. So we put 'user' as our second parameter.
The thirth parameter is again the function that is executed each time the user variable changes.
Within the function:
$username.text() changes the text inside de user DOM element in the stats element in the top-left corner of the image.
Inside the text() is another WBA call. In this case WBA.readVar('user') which reads the user variable and returns the value of it. The value is inserted as the text.
With CSS the dom element can be styled so it looks seamless with the game looks.
All this code van be put in the custom.js file.
If you want to add more code. Remember that the (function () { }); , 'use strict'; and the WBA.init(); should be only once in the file. You can have multiple WBA.on('changeVar') events to listen to multiple variable changes.
A bit more ontopic to your question:
If you want to display the time as an image, you can create a var, for example 'time' and create .png files with clock_1.png, clock_2.png etc.
Change the varChange event as follows:
- Code: Select all
WBA.on('varChange', 'time', function () {
// When the time changes, display the image with the right time
$userName.html('<img src="img/clock_" + WBA.readVar('time') + ' />');
});
And the $userName variable can be changed to $clock.
My english is not that good, but i hope you get the point.
If you got stuck, plz let me know :-)