define(label,
callback)
label
argument is used to
determine if this code is to be run on this
Crafty instance. Iff this instance's
label
starts with the given
label
, the callback
is executed. This instance's label
is specified on the client / server creation.
label |
the label this instance's label has to start with in order for the callback to be executed |
callback |
the callback to execute if this instance's label starts with the given label |
// create an entity on the Crafty instance, whose label contains "CLIENT"
Crafty.define("CLIENT", function() {
Crafty.e("2D, DOM, Text")
.attr({ x: 100, y: 100 })
.text("Loading, please stand by.");
});
netBind(eventName,
callback)
eventName |
the event name |
callback |
the callback to execute upon event receipt |
Crafty.netBind("GameOver", function(data) {
console.log("Congratulations, you achieved " + data.score + " points!");
});
netTrigger(eventName,
eventObject, isVolatile)
isVolatile
parameter specifies
if the transmission should be guaranteed to be
delivered to the other end or not. If set to
true
, the event can get lost over
the network, but it increases the network
performance (useful for transmitting events
every frame, like position updates).
eventName |
the event name |
eventObject |
the event object |
isVolatile |
whether the
event should be sent reliably or not;
if set to true the event
will not be sent reliably |
Crafty.netTrigger("GameOver", {score: 11});
Crafty.netTrigger("GameOver", {score: 11}, false); // equivalent to the above
netUnbind(eventName,
callback)
eventName |
the event name |
callback |
the callback to execute upon event receipt |
var callback = function(data) {
console.log("Congratulations, you achieved " + data.score + " points!");
};
Crafty.netBind("GameOver", callback);
Crafty.netUnbind("GameOver", callback);
define(label,
callback)
label
argument is used to
determine if this code is to be run on this
Crafty instance. Iff this instance's
label
starts with the given
label
, the callback
is executed. This instance's label
is specified on the client / server creation
(see lib documentation).
label |
the label this instance's label has to start with in order for the callback to be executed |
callback |
the callback to execute if this instance's label starts with the given label |
// Example showing definition of a basic entity, which is extended depending on the label of the Crafty instance it belongs to.
//common features
Crafty.e("2D, Net")
.setName("Player1")
.attr({ x: x, y: y, w: w, h: h })
//this will be defined on all CLIENTS (e.g. CLIENT1, CLIENT2, ...)
.define("CLIENT", function() {
this.addComponent("DOM, Color")
.color('rgb(255,0,0)');
})
//this will defined on CLIENT1 only
.define("CLIENT1", function() {
this.addComponent("Keyboard")
.netTrigger("KeyDown", ...);
})
//this will be defined on SERVER only
.define("SERVER", function() {
this.addComponent("Collision")
.onHit("Component", ...)
});
netBind(eventName,
callback)
eventName |
the event name |
callback |
the callback to execute upon event receipt |
var ent = Crafty.e("Net")
.setName("MyEntity")
.netBind("Moved", function(data) {
this.x = data.x;
this.y = data.y;
});
netTrigger(eventName,
eventObject, isVolatile)
isVolatile
parameter specifies
if the transmission should be guaranteed to be
delivered to the other end or not. If set to
true
, the event can get lost over
the network, but it increases the network
performance (useful for transmitting events
every frame, like position updates).
eventName |
the event name |
eventObject |
the event object |
isVolatile |
whether the
event should be sent reliably or not;
if set to true the event
will not be sent reliably |
var ent = Crafty.e("Net")
.setName("MyEntity")
.netTrigger("Moved", {x: this.x, y: this.y});
netUnbind(eventName,
callback)
eventName |
the event name |
callback |
the callback to execute upon event receipt |
var callback = function(data) {
this.x = data.x;
this.y = data.y;
};
var ent = Crafty.e("Net").setName("MyEntity");
ent.netBind("Moved", callback);
ent.netUnbind("Moved", callback);
createInstance(label)
label
is used to determine
which code to execute.
label |
the label to use for this client instance |
var Client = require('npm_crafty')();
var Crafty = Client.createInstance("CLIENT");
setServer(Crafty,
socket)
socket
to the
Crafty client instance. The Crafty net features
will use this socket
to
communicate with the server.
Crafty |
the Crafty client instance to bind the socket to |
socket |
the socket or socket namespace to use for communication with server |
var Client = require('npm_crafty')();
var Crafty = Client.createInstance("CLIENT");
Client.setServer(Crafty, socket);
setupDefault(immediateFN,
connectFN, disconnectFN, serverAddress)
static'/'
namespace.
immediateFN |
will be called immediately once all libraries are loaded |
connectFN |
will be called when the client connects to the server |
disconnectFN |
will be called when the client disconnects from the server |
serverAddress |
the server address to connect to (auto-resolved usually and thus can be omitted usually) e.g. "http://localhost" |
io
property
var Crafty;
var Client = require('npm_crafty').setupDefault( function () { //immediate callback
Crafty = Client.createInstance("CLIENT");
}, function (socket) { //connect callback
Client.setServer(Crafty, socket);
}, function (socket) { //disconnect callback
});
unsetServer(Crafty,
socket)
socket
from the
Crafty client instance. The Crafty net features
will no longer use this socket
to
communicate with the server.
Crafty |
the Crafty client instance to unbind the socket from |
socket |
the socket or socket namespace to no longer use for communication with server |
var Client = require('npm_crafty')();
var Crafty = Client.createInstance("CLIENT");
Client.setServer(Crafty, socket);
Client.unsetServer(Crafty, socket);
sockets
parameter contains the
Socket.IO namespace to use for all Crafty related data.
addClient(Crafty,
socket)
socket
to the Crafty
server instance. The Crafty net features will
use this socket
to communicate
with the client.
Crafty |
the Crafty server instance to bind the socket to |
socket |
the socket to use for communication with a client |
var Server = require('npm_crafty')(io.sockets);
var Crafty = Server.createInstance("Room1");
Server.addClient(Crafty, socket);
createInstance(room)
room
name. Only client and server
instances within the same room
can
talk to each other."SERVER"
.
room |
the room to use for this server instance |
var Server = require('npm_crafty')(io.sockets);
var Crafty = Server.createInstance("Room1");
removeClient(Crafty,
socket)
socket
from the
Crafty server instance. The Crafty net features
will no longer use this socket
to
communicate with the client.
Crafty |
the Crafty server instance to unbind the socket from |
socket |
the socket to no longer use for communication with a client |
var Server = require('npm_crafty')(io.sockets);
var Crafty = Server.createInstance("Room1");
Server.addClient(Crafty, socket);
Server.removeClient(Crafty, socket);
setupDefault(immediateFN,
connectFN, disconnectFN, port)
static'/'
namespace.
immediateFN |
will be called immediately once all libraries are loaded |
connectFN |
will be called when a client connects to the server |
disconnectFN |
will be called when a client disconnects from the server |
port |
the port to use
(defaults to
process.env.PORT ; falls
back to standard HTTP port
80 ) |
app
, server
and
io
property
var Crafty;
var Server = require('npm_crafty').setupDefault( function () { //immediate callback
Crafty = Server.createInstance("Room1");
}, function (socket) { //connect callback
Server.addClient(Crafty, socket);
}, function (socket) { //disconnect callback
});