Introduction to CraftyJS : Event Triggering


// When the DOM has loaded
$(document).ready(function() {

// Height and Width
var WIDTH = 500, HEIGHT = 320;

// Initialize Crafty
Crafty.init(WIDTH, HEIGHT);

// Background
Crafty.background("black");

// Create a player entity
var player1 = Crafty.e();

// Add some components to the entity
player1.addComponent("2D, DOM ,Mouse");

player1.attr({ x:20, y: 150, w:50, h:50 });

player1.addComponent("Color").color("red");

player1.bind("Click", function(e) {
alert('red');
Crafty.trigger('Myevent');
});

var player2 = Crafty.e();

// Add some components to the entity
player2.addComponent("2D, DOM ,Mouse");

player2.attr({ x:430, y: 150, w:50, h:50 });

player2.addComponent("Color").color("white");

player2.bind("Myevent", function(e) {
alert('white');
});

});

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s