Skip to content
aron edited this page Sep 8, 2011 · 18 revisions

jj

This the global object (it stands for JavaScript Jungle) this is where the jungle environment is controlled. You can use it to get the size of the jungle, listen to global events or even fire your own. You can access this anywhere in your world.

jj.createCreature(name, func)

This is the main method you'll use to create new creatures. A creature is just a layer (<div>) in the jungle.

name - A name for your creature, it needs to be unique to the jungle.
func - A function that will be passed your creature object to work on.

Example

jj.createCreature('billy', function (creature) {
   // Give your creature a size.
   creature.size({width: 50, height: 50});
   
   // Center your creature in the world.
   var worldCenter = jj.center();
   creature.position({
     top:  worldCenter.top  - 25,
     left: worldCenter.left - 25 
   });
});

jj.size()

This grabs the size of the world and returns you an object with a width and height property {width: 1024, height: 780}.

Example

var worldSize = jj.size();

jj.center()

This returns the co-ordinates of the center of the world as a top and left property {left: 512, top: 365}.

Example

var center = jj.center();

jj.bind(topic, callback)

This lets you listen in to global events that can be fired using jj.trigger(). It works in the same way as jQuery.bind('click'). Check the whiteboard for a list of events.

topic    - The name of the topic that you want to subscribe to.
callback - A callback that will be called when the topic is published.
           Different topics will provide different arguments to the
           callback.

Example

jj.bind('tick', function (frame) {
  // This function will be called 30 times a second. You can use it
  // to animate your creatures.
  
  // Move a creature down the screen in 5px increments.
  creature.el.css('top', '+= 5px')
});

jj.trigger(topic, arg1, arg2)

This triggers a function on the global object so that any other creature can listen in (they can do this with jj.bind()).

Example

// Others can listen in.
jj.bind('arons-messages', function (message) {
  console.log('Aron says ' + message);
});

jj.trigger('arons-messages', 'hello guys!');
Clone this wiki locally