Updates to JSGameClient have been released. The important details are:
- Added Project Goals Listing
- Added more documentation
- Added an additional example
- JSGameClient now provides supports an event driven model and includes setInterval/setTimeout functions that work the same as in a web browser.
var text = new sf.Text(new sf.String("Hello World"));
var window = new sf.RenderWindow(new sf.VideoMode(1024, 768), "Game Client");
var event = new sf.Event();
var cnt = 0;
var loopid = time.setInterval(function() {
cnt += 1;
text.setString(new sf.String("Hello World: " + cnt));
if ( window.isOpen() ) {
window.clear();
window.draw(text);
window.display();
while ( window.pollEvent(event) ) {
if ( event.type == sf.Event.Closed ) {
window.close();
} else if ( event.type == sf.Event.KeyPressed ) {
if ( event.key.code == sf.Keyboard.I ) {
}
}
}
} else {
log("Clearing interval");
time.clearInterval(loopid);
}
}, 50);
The above is an event driven alternative to something like:
var text = new sf.Text(new sf.String("Hello World"));
var window = new sf.RenderWindow(new sf.VideoMode(1024, 768), "Game Client");
var event = new sf.Event();
var cnt = 0;
while (window.isOpen())
{
window.clear();
window.draw(text);
window.display();
while ( window.pollEvent(event) )
{
if ( event.type == sf.Event.Closed ) {
window.close();
}
}
}
I personally prefer the second example, but the two approaches can be combined as:
var text = new sf.Text(new sf.String("Hello World"));
var window = new sf.RenderWindow(new sf.VideoMode(1024, 768), "Game Client");
var event = new sf.Event();
var cnt = 0;
var loopid = time.setInterval('cnt += 50; text.setString(new sf.String("Hello World: " + cnt));', 50);
while (window.isOpen())
{
time.yield(); // Without this, the interval code will not be called.
window.clear();
window.draw(text);
window.display();
while ( window.pollEvent(event) )
{
if ( event.type == sf.Event.Closed ) {
window.close();
}
}
}
time.clearInterval(loopid);
Like in the browser the setInterval/setTimeout can use a function or a string containing JavaScript code.